新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
怎么使用Elasticsearch中的Match_phrase查詢
本篇內容主要講解“怎么使用Elasticsearch中的Match_phrase查詢”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用Elasticsearch中的Match_phrase查詢”吧!
創(chuàng)新互聯(lián)于2013年開始,先為田東等服務建站,田東等地企業(yè),進行企業(yè)商務咨詢服務。為田東企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。
數(shù)據(jù)準備階段
新建索引: PUT test_phrase 設置索引mapping: PUT /test_phrase/_mapping/_doc { "properties": { "name": { "type":"text" } } } 結果: { "mapping": { "_doc": { "properties": { "name": { "type": "text" } } } } } 插入數(shù)據(jù): PUT test_phrase/_doc/2 { "name":"我愛北京天安門" } 查詢數(shù)據(jù): POST test_phrase/_search { "query": {"match_all": {}} } 結果: { "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "test_phrase", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "我愛北京天安門" } }, { "_index" : "test_phrase", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "王乃康" } } ] } } 查看分詞詞項: POST test_phrase/_analyze { "field": "name", "text": "我愛北京天安門" } 結果: { "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "", "position" : 0 }, { "token" : "愛", "start_offset" : 1, "end_offset" : 2, "type" : " ", "position" : 1 }, { "token" : "北", "start_offset" : 2, "end_offset" : 3, "type" : " ", "position" : 2 }, { "token" : "京", "start_offset" : 3, "end_offset" : 4, "type" : " ", "position" : 3 }, { "token" : "天", "start_offset" : 4, "end_offset" : 5, "type" : " ", "position" : 4 }, { "token" : "安", "start_offset" : 5, "end_offset" : 6, "type" : " ", "position" : 5 }, { "token" : "門", "start_offset" : 6, "end_offset" : 7, "type" : " ", "position" : 6 } ] }
測試階段
1.關鍵詞"我"
POST test_phrase/_search { "query": { "match_phrase": { "name": { "query": "我" } } } } 結果: { "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.2876821, "hits" : [ { "_index" : "test_phrase", "_type" : "_doc", "_id" : "2", "_score" : 0.2876821, "_source" : { "name" : "我愛北京天安門" } } ] } } 分析: POST test_phrase/_analyze { "field": "name", "text": "我" } { "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "", "position" : 0 } ] } 查詢分詞"我"的position位置是0,首先文檔"我愛北京天安門"的索引分詞中有"我"且position為0,符合短語查詢的要求,因此可以正確返回。
2.關鍵詞"我愛"
POST test_phrase/_search { "query": { "match_phrase": { "name": { "query": "我愛" } } } } 結果: { "took" : 4, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.5753642, "hits" : [ { "_index" : "test_phrase", "_type" : "_doc", "_id" : "2", "_score" : 0.5753642, "_source" : { "name" : "我愛北京天安門" } } ] } } 分析: POST test_phrase/_analyze { "field": "name", "text": "我愛" } { "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "", "position" : 0 }, { "token" : "愛", "start_offset" : 1, "end_offset" : 2, "type" : " ", "position" : 1 } ] } 查詢分詞"我愛"的position分別是"我"-0、"愛"-1,首先索引分詞中也存在"我"、"愛"詞項,其次"我"-0、"愛"-1的position也服務要求,因此可以正確返回。
3.關鍵詞"我北"
POST test_phrase/_search { "query": { "match_phrase": { "name": { "query": "我北" } } } } 結果: { "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : null, "hits" : [ ] } } 分析: POST test_phrase/_analyze { "field": "name", "text": "我北" } { "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "", "position" : 0 }, { "token" : "北", "start_offset" : 1, "end_offset" : 2, "type" : " ", "position" : 1 } ] } 查詢分詞中"我"的position是0,"北"的position是1,索引分詞中"我"的position是0,"北"的position是2, 雖然查詢分詞的詞項在索引分詞的詞項中都存在,但是position并未匹配要求,導致搜索結果不能正確返回。 修正:"slop": 1 POST test_phrase/_search { "query": { "match_phrase": { "name": { "query": "我北", "slop": 1 } } } } { "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.37229446, "hits" : [ { "_index" : "test_phrase", "_type" : "_doc", "_id" : "2", "_score" : 0.37229446, "_source" : { "name" : "我愛北京天安門" } } ] } }
補充階段
1.使用鄰近度提高相關度
我們可以將一個簡單的 match
查詢作為一個 must
子句。 這個查詢將決定哪些文檔需要被包含到結果集中。 我們可以用 minimum_should_match
參數(shù)去除長尾。 然后我們可以以 should
子句的形式添加更多特定查詢。 每一個匹配成功的都會增加匹配文檔的相關度。
GET /my_index/my_type/_search { "query": { "bool": { "must": { "match": { #must 子句從結果集中包含或者排除文檔 "title": { "query": "quick brown fox", "minimum_should_match": "30%" } } }, "should": { "match_phrase": { #should 子句增加了匹配到文檔的相關度評分。 "title": { "query": "quick brown fox", "slop": 50 } } } } } }
到此,相信大家對“怎么使用Elasticsearch中的Match_phrase查詢”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
分享文章:怎么使用Elasticsearch中的Match_phrase查詢
標題鏈接:http://www.dlmjj.cn/article/iioied.html