• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Elasticsearch:Terms set 查询

武飞扬头像
Elasticsearch
帮助44

什么是 terms set 查询?

Terms set 查询根据匹配给定字段的精确术语的最少数量返回文档。

terms set 查询与 term 查询有何不同?

Terms set query 和 Terms query 之间的唯一区别是你可以提供必须匹配的最少数量的术语才能检索特定文档。

什么是 minimum_should_match_field 参数?

指向文档的数字(numeric)字段名称,其值应用作要匹配的最少术语数,以便返回文档。

minimum_should_match_script 参数是什么?

一个自定义脚本,用于确定为了返回文档而必须匹配的最少术语数。 如果你必须动态设置匹配所需的术语数,那么它将很有帮助。

示例

让我们首先创建索引:

`

1.  PUT product
2.  {
3.    "mappings": {
4.      "properties": {
5.        "name": {
6.          "type": "keyword"
7.        },
8.        "tags": {
9.          "type": "keyword"
10.        },
11.        "tags_count": {
12.          "type": "long"
13.        }
14.      }
15.    }
16.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

让我们索引样本文件:

`

1.  POST product/_doc/prod1
2.  {
3.    "name":"Iphone 13",
4.    "tags":["apple","iphone","mobile"],
5.    "tags_count":3
6.  }

8.  POST product/_doc/prod2
9.  {
10.    "name":"Iphone 12",
11.    "tags":["apple","iphone"],
12.    "tags_count":2
13.  }

15.  POST product/_doc/prod3
16.  {
17.    "name":"Iphone 11",
18.    "tags":["apple","mobile"],
19.    "tags_count":2
20.  }

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

使用 minimum_should_match_field 参数查询:

用例 1:下面的查询将返回所有 3 个文档,因为 prod1 的最小术语匹配 (tags_count) 是 3,prod2 是 2,prod3 是 2,查询中传递了总共 3 个术语("apple", "iphone", "mobile")。



1.  POST product/_search
2.  {
3.    "query": {
4.      "terms_set": {
5.        "tags": {
6.          "terms": [ "apple", "iphone", "mobile" ],
7.          "minimum_should_match_field": "tags_count"
8.        }
9.      }
10.    }
11.  }


上述查询的结果是:

 `1.    "hits": {
2.      "total": {
3.        "value": 3,
4.        "relation": "eq"
5.      },
6.      "max_score": 1.4010588,
7.      "hits": [
8.        {
9.          "_index": "product",
10.          "_id": "prod1",
11.          "_score": 1.4010588,
12.          "_source": {
13.            "name": "Iphone 13",
14.            "tags": [
15.              "apple",
16.              "iphone",
17.              "mobile"
18.            ],
19.            "tags_count": 3
20.          }
21.        },
22.        {
23.          "_index": "product",
24.          "_id": "prod2",
25.          "_score": 0.7876643,
26.          "_source": {
27.            "name": "Iphone 12",
28.            "tags": [
29.              "apple",
30.              "iphone"
31.            ],
32.            "tags_count": 2
33.          }
34.        },
35.        {
36.          "_index": "product",
37.          "_id": "prod3",
38.          "_score": 0.7876643,
39.          "_source": {
40.            "name": "Iphone 11",
41.            "tags": [
42.              "apple",
43.              "mobile"
44.            ],
45.            "tags_count": 2
46.          }
47.        }
48.      ]
49.    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

用例二:下面的查询将只返回一个文档,因为查询中只传递了 2 个术语,仅与 prod3 匹配。 prod1 将不会返回,因为 tags_count 值为 3 并且查询中传递的总术语仅为 2。



1.  POST product/_search
2.  {
3.    "query": {
4.      "terms_set": {
5.        "tags": {
6.          "terms": [ "apple", "mobile" ],
7.          "minimum_should_match_field": "tags_count"
8.        }
9.      }
10.    }
11.  }


上述查询的结果为:

 `1.    "hits": {
2.      "total": {
3.        "value": 1,
4.        "relation": "eq"
5.      },
6.      "max_score": 0.5007585,
7.      "hits": [
8.        {
9.          "_index": "product",
10.          "_id": "prod3",
11.          "_score": 0.5007585,
12.          "_source": {
13.            "name": "Iphone 11",
14.            "tags": [
15.              "apple",
16.              "mobile"
17.            ],
18.            "tags_count": 2
19.          }
20.        }
21.      ]
22.    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

minimum_should_match_script 示例:

现在让我们看看如何使用 minimum should match 的动态值检索相同的索引数据。

在下面的示例中,查询中提供的术语总数的值将作为最小应匹配值传递。 我们将使用 params.num_terms 来计算查询中提供的术语数。 需要匹配的词条数不能超过 params.num_terms,即 terms 字段中提供的词条数。



1.  POST product/_search
2.  {
3.    "query": {
4.      "terms_set": {
5.        "tags": {
6.          "terms": ["apple","iphone"],
7.          "minimum_should_match_script": {
8.            "source": "params.num_terms"
9.          }
10.        }
11.      }
12.    }
13.  }


它将返回 prod1 和 prod2,因为 minimum_should_match 值将设置为 2,因为我们在查询中仅传递了 2 个术语。上述命令的返回值为:

 `1.      "hits": [
2.        {
3.          "_index": "product",
4.          "_id": "prod2",
5.          "_score": 0.5007585,
6.          "_source": {
7.            "name": "Iphone 12",
8.            "tags": [
9.              "apple",
10.              "iphone"
11.            ],
12.            "tags_count": 2
13.          }
14.        },
15.        {
16.          "_index": "product",
17.          "_id": "prod1",
18.          "_score": 0.5007585,
19.          "_source": {
20.            "name": "Iphone 13",
21.            "tags": [
22.              "apple",
23.              "iphone",
24.              "mobile"
25.            ],
26.            "tags_count": 3
27.          }
28.        }
29.      ]
30.    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

让我们考虑一个场景,你想要考虑 tags_count 的最小值或查询中的术语数; 在这种情况下,以下查询会有所帮助:



1.  POST product/_search
2.  {
3.    "query": {
4.      "terms_set": {
5.        "tags": {
6.          "terms": ["apple","iphone"],
7.          "minimum_should_match_script": {
8.            "source": "Math.min(params.num_terms, doc['tags_count'].value)"
9.          }
10.        }
11.      }
12.    }
13.  }


上述查询的结果为:

 `1.      "hits": [
2.        {
3.          "_index": "product",
4.          "_id": "prod2",
5.          "_score": 0.61233616,
6.          "_source": {
7.            "name": "Iphone 12",
8.            "tags": [
9.              "apple",
10.              "iphone"
11.            ],
12.            "tags_count": 2
13.          }
14.        },
15.        {
16.          "_index": "product",
17.          "_id": "prod1",
18.          "_score": 0.61233616,
19.          "_source": {
20.            "name": "Iphone 13",
21.            "tags": [
22.              "apple",
23.              "iphone",
24.              "mobile"
25.            ],
26.            "tags_count": 3
27.          }
28.        }
29.      ]
30.    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

Terms set 查询 Elasticsearch Java 客户端

下面的代码将有助于使用 Elasticsearch Java 客户端实现术语集查询。

Using new Java API Client (8.x)

`

1.  List<String> tags = new ArrayList<String>();
2.  tags.add("apple");
3.  tags.add("iphone");

5.  // Using minimum_should_match_field param
6.  Query query1 = Query.of(q -> q.bool(BoolQuery.of(bq -> bq.must(ts -> ts.termsSet(
7.  		TermsSetQuery.of(tq -> tq.field("tags").minimumShouldMatchField("tags_count").terms(tags)))))));

9.  //Using minimum_should_match_script param
10.  Map<String, JsonData> param = new HashMap<String, JsonData>();

12.  Query query1 = Query
13.  		.of(q -> q.bool(BoolQuery.of(bq -> bq.must(ts -> ts.termsSet(TermsSetQuery.of(tq -> tq.field("tags")
14.  				.minimumShouldMatchScript(sc -> sc.inline(in -> in.lang("painless").source("params.num_terms").params(param)))
15.  				.terms(tags)))))));

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

使用 Java High Level 客户端(已弃用)

`

1.  Map<String, Object> param = new HashMap<String, Object>();
2.  Script script = new Script(ScriptType.INLINE, "painless", "params.num_terms", param);

4.  List<String> tags = new ArrayList<String>();
5.  tags.add("apple");
6.  tags.add("iphone");

8.  // Using minimum_should_match_field
9.  QueryBuilder query = QueryBuilders.boolQuery()
10.  		.must(new TermsSetQueryBuilder("tags", tags).setMinimumShouldMatchField("tags_count"));

12.  // Using minimum_should_match_script
13.  Map<String, Object> param = new HashMap<String, Object>();
14.  Script script = new Script(ScriptType.INLINE, "painless", "params.num_terms", param);
15.  QueryBuilder query = QueryBuilders.boolQuery()
16.  		.must(new TermsSetQueryBuilder("tags", tags).setMinimumShouldMatchScript(script));

`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfbfeb
系列文章
更多 icon
同类精品
更多 icon
继续加载