繁体   English   中英

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

[英]Elasticsearch aggregations filtered result is not working properly

  1. 两个样本文件

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}
            ]
        }
    ]
}
  1. 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"
    }
  }
}
  1. 结果

     { "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版中的reducer将引入一些在服务器端操作存储桶的方法。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM