您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么使用Elasticsearch中的Match_phrase查詢”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用Elasticsearch中的Match_phrase查詢”吧!
新建索引: PUT test_phrase 設(shè)置索引mapping: PUT /test_phrase/_mapping/_doc { "properties": { "name": { "type":"text" } } } 結(jié)果: { "mapping": { "_doc": { "properties": { "name": { "type": "text" } } } } } 插入數(shù)據(jù): PUT test_phrase/_doc/2 { "name":"我愛北京天安門" } 查詢數(shù)據(jù): POST test_phrase/_search { "query": {"match_all": {}} } 結(jié)果: { "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" : "王乃康" } } ] } } 查看分詞詞項(xiàng): POST test_phrase/_analyze { "field": "name", "text": "我愛北京天安門" } 結(jié)果: { "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": "我" } } } } 結(jié)果: { "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": "我愛" } } } } 結(jié)果: { "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,首先索引分詞中也存在"我"、"愛"詞項(xiàng),其次"我"-0、"愛"-1的position也服務(wù)要求,因此可以正確返回。
POST test_phrase/_search { "query": { "match_phrase": { "name": { "query": "我北" } } } } 結(jié)果: { "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, 雖然查詢分詞的詞項(xiàng)在索引分詞的詞項(xiàng)中都存在,但是position并未匹配要求,導(dǎo)致搜索結(jié)果不能正確返回。 修正:"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" : "我愛北京天安門" } } ] } }
我們可以將一個(gè)簡(jiǎn)單的 match
查詢作為一個(gè) must
子句。 這個(gè)查詢將決定哪些文檔需要被包含到結(jié)果集中。 我們可以用 minimum_should_match
參數(shù)去除長(zhǎng)尾。 然后我們可以以 should
子句的形式添加更多特定查詢。 每一個(gè)匹配成功的都會(huì)增加匹配文檔的相關(guān)度。
GET /my_index/my_type/_search { "query": { "bool": { "must": { "match": { #must 子句從結(jié)果集中包含或者排除文檔 "title": { "query": "quick brown fox", "minimum_should_match": "30%" } } }, "should": { "match_phrase": { #should 子句增加了匹配到文檔的相關(guān)度評(píng)分。 "title": { "query": "quick brown fox", "slop": 50 } } } } } }
到此,相信大家對(duì)“怎么使用Elasticsearch中的Match_phrase查詢”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。