Elasticsearch學(xué)習(xí)之Terms?set?查詢
什么是 terms set 查詢?
Terms set 查詢根據(jù)匹配給定字段的精確術(shù)語(yǔ)的最少數(shù)量返回文檔。
terms set 查詢與 term 查詢有何不同?
Terms set query 和 Terms query 之間的唯一區(qū)別是你可以提供必須匹配的最少數(shù)量的術(shù)語(yǔ)才能檢索特定文檔。
什么是 minimum_should_match_field 參數(shù)?
指向文檔的數(shù)字(numeric)字段名稱,其值應(yīng)用作要匹配的最少術(shù)語(yǔ)數(shù),以便返回文檔。
minimum_should_match_script 參數(shù)是什么?
一個(gè)自定義腳本,用于確定為了返回文檔而必須匹配的最少術(shù)語(yǔ)數(shù)。 如果你必須動(dòng)態(tài)設(shè)置匹配所需的術(shù)語(yǔ)數(shù),那么它將很有幫助。
示例
讓我們首先創(chuàng)建索引:
` PUT product { "mappings": { "properties": { "name": { "type": "keyword" }, "tags": { "type": "keyword" }, "tags_count": { "type": "long" } } } } `
讓我們索引樣本文件:
` POST product/_doc/prod1 { "name":"Iphone 13", "tags":["apple","iphone","mobile"], "tags_count":3 } POST product/_doc/prod2 { "name":"Iphone 12", "tags":["apple","iphone"], "tags_count":2 } POST product/_doc/prod3 { "name":"Iphone 11", "tags":["apple","mobile"], "tags_count":2 } `
使用 minimum_should_match_field 參數(shù)查詢:
用例 1:下面的查詢將返回所有 3 個(gè)文檔,因?yàn)?prod1 的最小術(shù)語(yǔ)匹配 (tags_count) 是 3,prod2 是 2,prod3 是 2,查詢中傳遞了總共 3 個(gè)術(shù)語(yǔ)("apple", "iphone", "mobile")。
POST product/_search { "query": { "terms_set": { "tags": { "terms": [ "apple", "iphone", "mobile" ], "minimum_should_match_field": "tags_count" } } } }
上述查詢的結(jié)果是:
` "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.4010588, "hits": [ { "_index": "product", "_id": "prod1", "_score": 1.4010588, "_source": { "name": "Iphone 13", "tags": [ "apple", "iphone", "mobile" ], "tags_count": 3 } }, { "_index": "product", "_id": "prod2", "_score": 0.7876643, "_source": { "name": "Iphone 12", "tags": [ "apple", "iphone" ], "tags_count": 2 } }, { "_index": "product", "_id": "prod3", "_score": 0.7876643, "_source": { "name": "Iphone 11", "tags": [ "apple", "mobile" ], "tags_count": 2 } } ] }`
用例二:下面的查詢將只返回一個(gè)文檔,因?yàn)椴樵冎兄粋鬟f了 2 個(gè)術(shù)語(yǔ),僅與 prod3 匹配。 prod1 將不會(huì)返回,因?yàn)?tags_count 值為 3 并且查詢中傳遞的總術(shù)語(yǔ)僅為 2。
POST product/_search { "query": { "terms_set": { "tags": { "terms": [ "apple", "mobile" ], "minimum_should_match_field": "tags_count" } } } }
上述查詢的結(jié)果為:
` "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 0.5007585, "hits": [ { "_index": "product", "_id": "prod3", "_score": 0.5007585, "_source": { "name": "Iphone 11", "tags": [ "apple", "mobile" ], "tags_count": 2 } } ] }`
minimum_should_match_script 示例:
現(xiàn)在讓我們看看如何使用 minimum should match 的動(dòng)態(tài)值檢索相同的索引數(shù)據(jù)。
在下面的示例中,查詢中提供的術(shù)語(yǔ)總數(shù)的值將作為最小應(yīng)匹配值傳遞。 我們將使用 params.num_terms 來(lái)計(jì)算查詢中提供的術(shù)語(yǔ)數(shù)。 需要匹配的詞條數(shù)不能超過(guò) params.num_terms,即 terms 字段中提供的詞條數(shù)。
POST product/_search { "query": { "terms_set": { "tags": { "terms": ["apple","iphone"], "minimum_should_match_script": { "source": "params.num_terms" } } } } }
它將返回 prod1 和 prod2,因?yàn)?minimum_should_match 值將設(shè)置為 2,因?yàn)槲覀冊(cè)诓樵冎袃H傳遞了 2 個(gè)術(shù)語(yǔ)。上述命令的返回值為:
` "hits": [ { "_index": "product", "_id": "prod2", "_score": 0.5007585, "_source": { "name": "Iphone 12", "tags": [ "apple", "iphone" ], "tags_count": 2 } }, { "_index": "product", "_id": "prod1", "_score": 0.5007585, "_source": { "name": "Iphone 13", "tags": [ "apple", "iphone", "mobile" ], "tags_count": 3 } } ] }`
讓我們考慮一個(gè)場(chǎng)景,你想要考慮 tags_count 的最小值或查詢中的術(shù)語(yǔ)數(shù); 在這種情況下,以下查詢會(huì)有所幫助:
POST product/_search { "query": { "terms_set": { "tags": { "terms": ["apple","iphone"], "minimum_should_match_script": { "source": "Math.min(params.num_terms, doc['tags_count'].value)" } } } } }
上述查詢的結(jié)果為:
` "hits": [ { "_index": "product", "_id": "prod2", "_score": 0.61233616, "_source": { "name": "Iphone 12", "tags": [ "apple", "iphone" ], "tags_count": 2 } }, { "_index": "product", "_id": "prod1", "_score": 0.61233616, "_source": { "name": "Iphone 13", "tags": [ "apple", "iphone", "mobile" ], "tags_count": 3 } } ] }`
Terms set 查詢 Elasticsearch Java 客戶端
下面的代碼將有助于使用 Elasticsearch Java 客戶端實(shí)現(xiàn)術(shù)語(yǔ)集查詢。
Using new Java API Client (8.x)
` List<String> tags = new ArrayList<String>(); tags.add("apple"); tags.add("iphone"); // Using minimum_should_match_field param Query query1 = Query.of(q -> q.bool(BoolQuery.of(bq -> bq.must(ts -> ts.termsSet( TermsSetQuery.of(tq -> tq.field("tags").minimumShouldMatchField("tags_count").terms(tags))))))); //Using minimum_should_match_script param Map<String, JsonData> param = new HashMap<String, JsonData>(); Query query1 = Query .of(q -> q.bool(BoolQuery.of(bq -> bq.must(ts -> ts.termsSet(TermsSetQuery.of(tq -> tq.field("tags") .minimumShouldMatchScript(sc -> sc.inline(in -> in.lang("painless").source("params.num_terms").params(param))) .terms(tags))))))); `
使用 Java High Level 客戶端(已棄用)
` Map<String, Object> param = new HashMap<String, Object>(); Script script = new Script(ScriptType.INLINE, "painless", "params.num_terms", param); List<String> tags = new ArrayList<String>(); tags.add("apple"); tags.add("iphone"); // Using minimum_should_match_field QueryBuilder query = QueryBuilders.boolQuery() .must(new TermsSetQueryBuilder("tags", tags).setMinimumShouldMatchField("tags_count")); // Using minimum_should_match_script Map<String, Object> param = new HashMap<String, Object>(); Script script = new Script(ScriptType.INLINE, "painless", "params.num_terms", param); QueryBuilder query = QueryBuilders.boolQuery() .must(new TermsSetQueryBuilder("tags", tags).setMinimumShouldMatchScript(script)); `
以上就是Elasticsearch學(xué)習(xí)之Terms set 查詢的詳細(xì)內(nèi)容,更多關(guān)于Elasticsearch Terms set 查詢的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Lombok時(shí)@JsonIgnore注解失效解決方案
這篇文章主要為大家介紹了使用Lombok時(shí)@JsonIgnore注解失效問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)登錄接口整合阿里云短信詳情
這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)登錄接口整合阿里云短信詳情,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-04-04java讀取PHP接口數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇java讀取PHP接口數(shù)據(jù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08SpringBoot配置加載,各配置文件優(yōu)先級(jí)對(duì)比方式
這篇文章主要介紹了SpringBoot配置加載,各配置文件優(yōu)先級(jí)對(duì)比方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java中利用反射調(diào)用另一類的private方法的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java中利用反射調(diào)用另一類的private方法的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06