Elasticsearch學(xué)習之Terms?set?查詢
什么是 terms set 查詢?
Terms set 查詢根據(jù)匹配給定字段的精確術(shù)語的最少數(shù)量返回文檔。
terms set 查詢與 term 查詢有何不同?
Terms set query 和 Terms query 之間的唯一區(qū)別是你可以提供必須匹配的最少數(shù)量的術(shù)語才能檢索特定文檔。
什么是 minimum_should_match_field 參數(shù)?
指向文檔的數(shù)字(numeric)字段名稱,其值應(yīng)用作要匹配的最少術(shù)語數(shù),以便返回文檔。
minimum_should_match_script 參數(shù)是什么?
一個自定義腳本,用于確定為了返回文檔而必須匹配的最少術(shù)語數(shù)。 如果你必須動態(tài)設(shè)置匹配所需的術(shù)語數(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 個文檔,因為 prod1 的最小術(shù)語匹配 (tags_count) 是 3,prod2 是 2,prod3 是 2,查詢中傳遞了總共 3 個術(shù)語("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
}
}
]
}`
用例二:下面的查詢將只返回一個文檔,因為查詢中只傳遞了 2 個術(shù)語,僅與 prod3 匹配。 prod1 將不會返回,因為 tags_count 值為 3 并且查詢中傳遞的總術(shù)語僅為 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 的動態(tài)值檢索相同的索引數(shù)據(jù)。
在下面的示例中,查詢中提供的術(shù)語總數(shù)的值將作為最小應(yīng)匹配值傳遞。 我們將使用 params.num_terms 來計算查詢中提供的術(shù)語數(shù)。 需要匹配的詞條數(shù)不能超過 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,因為 minimum_should_match 值將設(shè)置為 2,因為我們在查詢中僅傳遞了 2 個術(shù)語。上述命令的返回值為:
` "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
}
}
]
}`
讓我們考慮一個場景,你想要考慮 tags_count 的最小值或查詢中的術(shù)語數(shù); 在這種情況下,以下查詢會有所幫助:
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 客戶端實現(xiàn)術(shù)語集查詢。
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é)習之Terms set 查詢的詳細內(nèi)容,更多關(guān)于Elasticsearch Terms set 查詢的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot RestTemplate請求日志打印方式
這篇文章主要介紹了SpringBoot RestTemplate請求日志打印方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
通俗易懂學(xué)習java并發(fā)工具類-Semaphore,Exchanger
這篇文章主要介紹了java并發(fā)工具類-Semaphore,Exchanger,java并發(fā)工具類有很多,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,下面小編帶大家來一起學(xué)習一下吧2019-06-06
java 配置MyEclipse Maven環(huán)境具體實現(xiàn)步驟
這篇文章主要介紹了 java 配置MyEclipse Maven環(huán)境具體實現(xiàn)步驟的相關(guān)資料,具有一定的參考價值,需要的朋友可以參考下2016-11-11
SpringBoot整合Mail輕松實現(xiàn)郵件自動推送功能
在項目中經(jīng)常會遇到SpringBoot推送消息的業(yè)務(wù),除了站內(nèi)推送通知,郵件推送也是一種常見的方式,本文小編就給大家介紹了SpringBoot整合Mail輕松實現(xiàn)郵件自動推送功能,需要的朋友可以參考下2024-12-12
Java實現(xiàn)世界上最快的排序算法Timsort的示例代碼
Timsort?是一個混合、穩(wěn)定的排序算法,簡單來說就是歸并排序和二分插入排序算法的混合體,號稱世界上最好的排序算法。本文將詳解Timsort算法是定義與實現(xiàn),需要的可以參考一下2022-07-07
springboot ApplicationContextInitializer的三種使用方法小結(jié)
這篇文章主要介紹了關(guān)于ApplicationContextInitializer的三種使用方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot讀取外部的配置文件的代碼實現(xiàn)
這篇文章主要介紹了SpringBoot讀取外部的配置文件的代碼實現(xiàn),文中通過代碼示例給大家講解的非常詳細,對大家的學(xué)習或工作有一定的幫助,需要的朋友可以參考下2024-11-11

