gpt4 book ai didi

elasticsearch - 过滤汇总结果

转载 作者:行者123 更新时间:2023-12-02 23:07:26 24 4
gpt4 key购买 nike

这个问题是this question的子问题。作为单独的问题发布以引起注意。
示例文档:

{
"id":1,
"product":"p1",
"cat_ids":[1,2,3]
}
{
"id":2,
"product":"p2",
"cat_ids":[3,4,5]
}
{
"id":3,
"product":"p3",
"cat_ids":[4,5,6]
}
询问:获取属于特定类别的产品。例如cat_id = 3
查询:
GET product/_search 
{
"size": 0,
"aggs": {
"cats": {
"terms": {
"field": "cats",
"size": 10
},"aggs": {
"products": {
"terms": {
"field": "name.keyword",
"size": 10
}
}
}
}
}
}
问题:
  • 如何在此处过滤cat_id = 3的汇总结果。我也尝试了bucket_selector,但是它不起作用。
    注意:由于cat_ids过滤具有多个值,因此聚合无法正常工作
  • 最佳答案

    您可以filter values,基于将创建哪些存储桶。

    It is possible to filter the values for which buckets will be created.This can be done using the include and exclude parameters which arebased on regular expression strings or arrays of exact values.Additionally, include clauses can filter using partition expressions.


    添加带有索引数据,搜索查询和搜索结果的工作示例
    索引数据:
    {
    "id":1,
    "product":"p1",
    "cat_ids":[1,2,3]
    }
    {
    "id":2,
    "product":"p2",
    "cat_ids":[3,4,5]
    }
    {
    "id":3,
    "product":"p3",
    "cat_ids":[4,5,6]
    }
    搜索查询:
    {
    "size": 0,
    "aggs": {
    "cats": {
    "terms": {
    "field": "cat_ids",
    "include": [ <-- note this
    3
    ]
    },
    "aggs": {
    "products": {
    "terms": {
    "field": "product.keyword",
    "size": 10
    }
    }
    }
    }
    }
    }
    搜索结果:
    "aggregations": {
    "cats": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
    "key": 3,
    "doc_count": 2,
    "products": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
    "key": "p1",
    "doc_count": 1
    },
    {
    "key": "p2",
    "doc_count": 1
    }
    ]
    }
    }
    ]
    }

    关于elasticsearch - 过滤汇总结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64229609/

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