您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么使用Elasticsearch中的Match_phrase查詢”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用Elasticsearch中的Match_phrase查詢”吧!
新建索引: PUT test_phrase 設置索引mapping: PUT /test_phrase/_mapping/_doc { "properties": { "name": { "type":"text" } } } 結果: { "mapping": { "_doc": { "properties": { "name": { "type": "text" } } } } } 插入數據: PUT test_phrase/_doc/2 { "name":"我愛北京天安門" } 查詢數據: 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" : "<IDEOGRAPHIC>", "position" : 0 }, { "token" : "愛", "start_offset" : 1, "end_offset" : 2, "type" : "<IDEOGRAPHIC>", "position" : 1 }, { "token" : "北", "start_offset" : 2, "end_offset" : 3, "type" : "<IDEOGRAPHIC>", "position" : 2 }, { "token" : "京", "start_offset" : 3, "end_offset" : 4, "type" : "<IDEOGRAPHIC>", "position" : 3 }, { "token" : "天", "start_offset" : 4, "end_offset" : 5, "type" : "<IDEOGRAPHIC>", "position" : 4 }, { "token" : "安", "start_offset" : 5, "end_offset" : 6, "type" : "<IDEOGRAPHIC>", "position" : 5 }, { "token" : "門", "start_offset" : 6, "end_offset" : 7, "type" : "<IDEOGRAPHIC>", "position" : 6 } ] }
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" : "<IDEOGRAPHIC>", "position" : 0 } ] } 查詢分詞"我"的position位置是0,首先文檔"我愛北京天安門"的索引分詞中有"我"且position為0,符合短語查詢的要求,因此可以正確返回。
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" : "<IDEOGRAPHIC>", "position" : 0 }, { "token" : "愛", "start_offset" : 1, "end_offset" : 2, "type" : "<IDEOGRAPHIC>", "position" : 1 } ] } 查詢分詞"我愛"的position分別是"我"-0、"愛"-1,首先索引分詞中也存在"我"、"愛"詞項,其次"我"-0、"愛"-1的position也服務要求,因此可以正確返回。
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" : "<IDEOGRAPHIC>", "position" : 0 }, { "token" : "北", "start_offset" : 1, "end_offset" : 2, "type" : "<IDEOGRAPHIC>", "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" : "我愛北京天安門" } } ] } }
我們可以將一個簡單的 match
查詢作為一個 must
子句。 這個查詢將決定哪些文檔需要被包含到結果集中。 我們可以用 minimum_should_match
參數去除長尾。 然后我們可以以 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查詢”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。