天天看点

ElasticSearch 匹配查询(match、match_phrase)ElasticSearch 匹配查询(match、match_phrase)

ElasticSearch 匹配查询(match、match_phrase)

match查询属于全文查询,在查询时,ES会先分析查询字符串,然后根据分词构建查询。
match_phrase在查询时也会先分析查询字符串,然后对这些词项进行搜索,不同的是match_phrase查询只会保留包含全部查询字符串的文档

1、先向ES查询俩个文档,以便测试:

PUT test2/_doc/1
{
  "name": "宝马法拉利兰博基尼"
}

PUT test2/_doc/2
{
  "name": "宝马兰博基尼布加迪威龙"
}
           

2、使用match查询

GET test2/_search
{
  "query": {
    "match": {
      "name": "宝马法拉利"
    }
  }
}
           

查询结果为全部文档:

"hits" : {
    "total" : 2,
    "max_score" : 1.4384104,
    "hits" : [
      {
        "_index" : "test2",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.4384104,
        "_source" : {
          "name" : "宝马法拉利兰博基尼"
        }
      },
      {
        "_index" : "test2",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "宝马兰博基尼布加迪威龙"
        }
      }
    ]
  }
           

3、使用match_phrase查询

GET test2/_search
{
  "query": {
    "match_phrase": {
      "name": "宝马法拉利"
    }
  }
}
           

查询结果只保留了包含“宝马法拉利”的文档,虽然第二个文档也有这俩词,但不是连着的

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.4384104,
    "hits" : [
      {
        "_index" : "test2",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.4384104,
        "_source" : {
          "name" : "宝马法拉利兰博基尼"
        }
      }
    ]
  }
}
           

继续阅读