【问题标题】:ElasticSearch Filtering aggregations from array fieldElasticSearch 从数组字段中过滤聚合
【发布时间】:2015-01-07 05:37:03
【问题描述】:

我正在尝试对数组中的值进行聚合,并过滤由前缀返回的存储桶。不确定这是否可行,或者我误用了过滤器桶。

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

有没有办法只过滤存储桶结果?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    试试这个,它将过滤桶本身创建的值:

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

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2021-04-22
      • 2015-09-17
      • 2014-02-02
      相关资源
      最近更新 更多