简体   繁体   English

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

[英]Elasticsearch aggregations filtered result is not working properly

  1. two sample documents 两个样本文件

POST /aggstest/test/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}
            ]
        }
    ]
}

POST /aggstest/test/2 开机自检/ 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 query aggs查询

GET /aggstest/test/_search 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. result 结果

     { "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 } ] } } } } 

======================== ========================

Expected aggs result set should not include "sunny" and "vibe" because it's "magazine" type. 预期的aggs结果集不应包含“ sunny”和“ vibe”,因为它是“ magazine”类型。

I used filter query and post_filter, but I couldn't get only "book" type aggs result. 我使用了过滤器查询和post_filter,但是我无法仅获得“ book”类型的aggs结果。

All the filters you apply (in-query and in-aggregation) still return the whole categories document. 您应用的所有过滤器(查询中和汇总中)仍会返回整个categories文档。 And this document, which contains all 4 words, is a scope for aggregation. 本文档包含所有四个词,是汇总的范围。 Hence you always get all 4 buckets. 因此,您将始终获得所有4个存储桶。 As far as I understand, some way to manipulate buckets on server-side would be introduced with reducers in version 2.0 of Elasticsearch. 据我了解,Elasticsearch 2.0版中的reducer将引入一些在服务器端操作存储桶的方法。

What you may use now is changing the mapping so that categories is nested object . 您现在可以使用的是更改映射,以使categories成为嵌套对象 Hence you'll be able to query them independently and aggregate accordingly using nested aggregation. 因此,您将能够独立查询它们,并使用嵌套聚合进行相应的聚合。 Changing object type to nested requires reindexing. 将对象类型更改为嵌套需要重新索引。

Also please note that post-filters are not applied to aggregation whatsoever. 还请注意, 后过滤器不会应用于聚合。 They are used to filter the original query without affecting the aggregation when you need to aggregate on wider scope than returned hits. 当您需要在比返回的匹配更大的范围内进行聚合时,它们用于过滤原始查询而不影响聚合。

And one more thing, if you already have filter in query there's no need to put it in aggregation, scope is already filtered. 还有一件事,如果查询中已经有过滤器,则无需将其放在聚合中,作用域已被过滤。

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

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