gpt4 book ai didi

json - Elasticsearch聚合过滤结果无法正常工作

转载 作者:行者123 更新时间:2023-12-02 23:36:46 25 4
gpt4 key购买 nike

  • 两个样本文件

  • POST / aggstest / test / 1
    {
    "categories": [
    {
    "type": "book",
    "words": [
    {"word":"storm","count":277},
    {"word":"pooh","count":229}
    ]
    },
    {
    "type": "magazine",
    "words": [
    {"word":"vibe","count":100},
    {"word":"sunny","count":50}
    ]
    }
    ]
    }

    开机自检/ aggstest / test / 2
    {
    "categories": [
    {
    "type": "book",
    "words": [
    {"word":"rain","count":160},
    {"word":"jurassic park","count":150}
    ]
    },
    {
    "type": "megazine",
    "words": [
    {"word":"tech","count":200},
    {"word":"homes","count":30}
    ]
    }
    ]
    }
  • aggs查询

  • GET / aggstest / test / _search
    {
    "size": 0,
    "query": {
    "filtered": {
    "filter": {
    "bool": {
    "must": [
    {
    "term": {
    "categories.type": "book"
    }
    },
    {
    "term": {
    "categories.words.word": "storm"
    }
    }
    ]
    }
    }
    }
    },
    "aggs": {
    "filtered": {
    "filter": {
    "bool": {
    "must": [
    {
    "term": {
    "categories.type": "book"
    }
    }
    ]
    }
    },
    "aggs": {
    "book_category": {
    "terms": {
    "field": "categories.words.word",
    "size": 10
    }
    }
    }
    }
    },
    "post_filter": {
    "term": {
    "categories.type": "book"
    }
    }
    }
  • 结果
    {  
    "took": 5,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
    "hits": {
    "total": 1,
    "max_score": 0,
    "hits": []
    },
    "aggregations": {
    "filtered": {
    "doc_count": 1,
    "book_category": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
    "key": "pooh",
    "doc_count": 1
    },
    {
    "key": "storm",
    "doc_count": 1
    },
    {
    "key": "sunny",
    "doc_count": 1
    },
    {
    "key": "vibe",
    "doc_count": 1
    }
    ]
    }
    }
    }
    }

  • ========================

    预期的aggs结果集不应包含“sunny”和“vibe”,因为它是“magazine”类型。

    我使用了过滤器查询和post_filter,但我无法仅获得“book”类型的aggs结果。

    最佳答案

    您应用的所有过滤器(查询中和聚合中)仍将返回整个categories文档。本文档包含所有四个词,是汇总的范围。因此,您将始终获得所有4个存储桶。
    据我了解,Elasticsearch 2.0版中的reducers将介绍一些在服务器端操作存储桶的方法。

    您现在可以使用的是更改映射,以便categoriesnested object。因此,您将能够独立查询它们,并使用嵌套聚合进行相应的聚合。将对象类型更改为嵌套需要重新索引。

    另外请注意,post-filters不会应用于聚合。当您需要在比返回的匹配更大的范围内进行聚合时,它们可用于过滤原始查询而不会影响聚合。

    还有一件事,如果查询中已经有过滤器,则无需将其放在聚合中,作用域已被过滤。

    关于json - Elasticsearch聚合过滤结果无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118094/

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