gpt4 book ai didi

elasticsearch - 从汇总中排除文档

转载 作者:行者123 更新时间:2023-12-03 02:01:11 26 4
gpt4 key购买 nike

我正在尝试从索引中获取经过过滤的结果集。

{"group_id": 123, "type" : 1},
{"group_id": 123, "type" : 3},
{"group_id": 123, "type" : 2},
{"group_id": 423, "type" : 3},
{"group_id": 423, "type" : 1},
{"group_id": 231, "type" : 1}

现在,我要获取所有文档,但要排除group_id包含类型= 2的文档。因此,在这种情况下,我要获取group_id = 423和group_id = 231的所有文档,但要排除group_id = 123的所有文档。

我正在尝试过滤 bool(boolean) 查询:
{
"query": {
"bool": {
"must_not": [
{
"term": {
"type": 2
}
}
]
}
}
}

但这仅排除一个文档。

任何提示都欢迎!

最佳答案

您可以使用两个Elasticsearch搜索请求来实现此目的:

首先,获得“group_id”的所有值,“type”的对应值是2。为此,您需要使用Terms Aggregation

POST <index name>/<type name>/_search
{
"size": 0,
"query": {
"filtered": {
"filter": {
"term": {
"type": 2
}
}
}
},
"aggs": {
"group_ids_type_2": {
"terms": {
"field": "group_id",
"size": 0
}
}
}
}

保存从上述请求中接收到的“group_id”字段的值列表。

现在,使用带有 must_not 过滤器的查询来获取所有文档,以使它们的“group_id”的值不出现在上面获得的列表中。您需要在此处使用 Terms Filter
POST <index name>/<type name>/_search
{
"query": {
"bool": {
"must_not": [
{
"terms": {
"group_id": [
"123" <-- Replace this with a comma separated list of all group_id values received from first search request
]
}
}
]
}
}
}

关于elasticsearch - 从汇总中排除文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com