《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.3.Search通过Kibana(12) https://developer.aliyun.com/article/1231058
比较 match_phrase 与 match 区别
l match_phrase
l 将查询条件的中的信息看做一个整体,如下面的 “goods t” 必须 goods 在前 t 在后。
l match
l 将查询中的条件做分词处理后,再去做查询。
#查询不到任何数据,因为不存在'goods t'的词组 GET /my_goods/_search { "query": { "match_phrase": { "goodsName": "goods t" } } } #能查询到数据,因为文档中包含goods和t的词组 GET /my_goods/_search { "query": { "match": { "goodsName": "goods t" } } }
在 match_phrase 中,可以通过 slop 来控制单词中间的间隔,默认为 0,下面举例说明:
GET /my_goods/_search { "query": { "match_phrase": { "goodsName": { "query": "apple test", "slop": 1 } } } } #返回 { "took" : 10, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 3.08089, "hits" : [ { "_index" : "my_goods", "_type" : "_doc", "_id" : "21", "_score" : 3.08089, "_source" : { "goodsName" : "apple goods test", "skuCode" : "skuCode3", "brandName" : "美国苹果", "closeUserCode" : [ "0" ], "channelType" : "cloudPlatform", "shopCode" : "sc00001", "publicPrice" : "8388.88", "groupPrice" : null, "boxPrice" : [ { "boxType" : "box1", "boxUserCode" : [ "htd003", "uc004" ], "boxPriceDetail" : 4388.88 }, { "boxType" : "box2", "boxUserCode" : [ "uc005", "uc0010" ], "boxPriceDetail" : 5388.88 } ], "boostValue" : 1.2 } } ] } }
可以看到,我们设置了 1 个词条,apple 与 test 之间间隔一个词条,故能查询到。
Match phrase prefix query
返回文档包含给定查询条件的文档,文档中必须包含给定条件的内容,且是按照 prefix 来进行匹配的,如 "apple goods test" ,商品名称包含 apple goods test 的数据将被查询到返回。
新增一条测试数据
POST my_goods/_bulk {"index":{"_id":13}} {"goodsName":"apple and goods product ","skuCode":"skuCode3","brandName":"美国苹果","closeUserCode":["0"],"channelType":"cloudPlatform","shopCode":"sc00001","publicPrice":"8388.88","groupPrice":null,"boxPrice":[{"boxType":"box1","boxUserCode":["htd003","uc004"],"boxPriceDetail":4388.88},{"boxType":"box2","boxUserCode":["uc005","uc0010"],"boxPriceDetail":5388.88}],"boostValue":1.2} {"index":{"_id":21}} {"goodsName":"apple goods test","skuCode":"skuCode3","brandName":"美国苹果","closeUserCode":["0"],"channelType":"cloudPlatform","shopCode":"sc00001","publicPrice":"8388.88","groupPrice":null,"boxPrice":[{"boxType":"box1","boxUserCode":["htd003","uc004"],"boxPriceDetail":4388.88},{"boxType":"box2","boxUserCode":["uc005","uc0010"],"boxPriceDetail":5388.88}],"boostValue":1.2} #只返回goodsName : apple goods test的数据 GET /my_goods/_search { "query": { "match_phrase_prefix": { "goodsName": "apple goods t" } } }
总结比较 match 这四种查询:
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.3.Search通过Kibana(14) https://developer.aliyun.com/article/1231056