美文网首页elasticsearch
ElasticSearch(三):SearchAPI

ElasticSearch(三):SearchAPI

作者: 采风JS | 来源:发表于2018-11-21 16:48 被阅读58次

一 字段类查询

字段类查询包括全文匹配和单词匹配,全文匹配针对text类型的字段进行全文检索,会对查询语句做分词,如match/match_phrase等query类型;

  • match
    针对查询语句做分词后对text字段做单词全文检索;
##通过operator参数可以控制单词间匹配关系,选项为or和and
##默认为or,查询username中包含alfred或者way的文档
GET test_search_index/_search
{
  "explain":true,
  "query": {
    "match": {
      "username": "alfred way"
    }
  }
}
##and,查询username中同时包含alfred和way的文档
GET test_search_index/_search
{
  "query": {
    "match": {
      "username": {
        "query": "alfred way",
        "operator": "and"
      }
    }
  }
}
##通过minimum_should_match控制需要匹配的单词数
GET test_search_index/_search
{
  "query": {
    "match": {
      "job": {
        "query": "java ruby engineer",
        "minimum_should_match": "3"
      }
    }
  }
}
  • match_phrase
    针对查询语句对text字段做词组的全文检索,可以通过slop控制单词间隔;
GET test_search_index/_search
{
  "query": {
    "match_phrase": {
      "job": "java engineer"
    }
  }
}
##job字段中包含java word word engineer形式
GET test_search_index/_search
{
  "query": {
    "match_phrase": {
      "job": {
        "query": "java engineer",
        "slop": "2"
      }
    }
  }
}
  • query-string
##username中同时包含alfred和way
GET test_search_index/_search
{
  "profile":true,
  "query":{
    "query_string": {
      "default_field": "username",
      "query": "alfred AND way"
    }
  }
}
##username和job字段中包含alfred或者同时包含java和ruby
GET test_search_index/_search
{
  "profile":true,
  "query": {
    "query_string": {
      "fields": [
        "username",
        "job"
      ],
      "query": "alfred OR (java AND ruby)"
    }
  }
}
  • simple-query-string
GET test_search_index/_search
{
  "profile":true,
  "query":{
    "simple_query_string": {
     "query": "alfred +way \"java",
     "fields": ["username"]
    }
  }
}

单词匹配不做分词,直接匹配字段倒排索引,如term/terms/range等query类型;

  • term/terms
    不对查询语句做分词处理;
GET test_search_index/_search
{
  "query":{
    "term":{
      "username":"alfred way"
    }
  }
}
GET test_search_index/_search
{
  "query": {
    "terms": {
      "username": [
        "alfred",
        "way"
      ]
    }
  }
}
  • range
    针对数值和日期类型做范围查询;
GET test_search_index/_search
{
  "query":{
    "range": {
      "age": {
        "gte": 10,
        "lte": 30
      }
    }
  }
}
GET test_search_index/_search
{
  "query":{
    "range": {
      "birth": {
        "gte": "now-30y"
      }
    }
  }
}

二 复合查询

复合查询是指包含字段类查询或复合查询类型;

  • constant_score query
    设置相关性得分为1或者boost值;

  • bool filter
    只过滤符合条件的文档,不会进行相关性得分;

GET test_search_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "username": "alfred"
          }
        }
      ]
    }
  }
}
  • bool must/must not
    必须或者必须不包含,影响相关性得分;
##username中必须包含alfred,job中必须包含specialist
GET test_search_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "username": "alfred"
          }
        },
        {
          "match": {
            "job": "specialist"
          }
        }
      ]
    }
  }
}
  • bool 只包含should不包含must
    文档必须至少满足一个条件,minimum_should_match可以控制满足条件的个数和百分比;
GET test_search_index/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"job": "java"}},
        {"term": {"job": "ruby"}},
        {"term": {"job": "specialist"}}
      ],
      "minimum_should_match": 2
    }
  }
}
  • bool 包含must不必满足should,满足则增加相关性得分;
GET test_search_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "username": "alfred"
          }
        }
      ],
      "should": [
        {
          "term": {
            "job": "ruby"
          }
        }
      ]
    }
  }
}
  • _count
    获取符合条件的文档数;
GET test_search_index/_count
{
  "query":{
    "match":{
      "username": "alfred"
    }
  }
}
  • _source
    过滤返回结果中_source字段;
GET test_search_index/_search
{
  "_source": ["username","age"]
}

相关文章

网友评论

    本文标题:ElasticSearch(三):SearchAPI

    本文链接:https://www.haomeiwen.com/subject/cvjcqqtx.html