簡體   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