簡體   English   中英

ElasticSearch從數組字段中過濾聚合

[英]ElasticSearch Filtering aggregations from array field

我試圖對數組中的值進行聚合,並過濾前綴返回的桶。 不確定這是否可行或我是否濫用過濾器桶。

3份文件:

{ "colors":["red","black","blue"] }
{ "colors":["red","black"] }
{ "colors":["red"] }

目標是獲取具有以字母B開頭的顏色的文檔計數:

{
  "size":0,
  "aggs" : {
    "colors" : {
      "filter" : { "prefix" : { "colors" : "b" } },
      "aggs" : {
        "top-colors" : { "terms" : { "field":"colors" } }
      }
    }
  }
}

不幸的是,回來的結果包括Red。 顯然因為帶有紅色的文檔仍然匹配過濾器,因為它們也有藍色和/或黑色。

"aggregations": {
"colors": {
  "doc_count": 2,
  "top-colors": {
    "buckets": [
      {
        "key": "black",
        "doc_count": 2
      },
      {
        "key": "red",
        "doc_count": 2
      },
      {
        "key": "blue",
        "doc_count": 1
      }
    ]
  }
}
}

有沒有辦法過濾桶結果?

試試這個,它將過濾為自己創建的桶本身的值:

{
  "size": 0,
  "aggs": {
    "colors": {
      "filter": {
        "prefix": {
          "colors": "b"
        }
      },
      "aggs": {
        "top-colors": {
          "terms": {
            "field": "colors",
            "include": {
              "pattern": "b.*"
            }
          }
        }
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM