ElasticSearch从数组字段过滤聚合 [英] ElasticSearch Filtering aggregations from array field

查看:579
本文介绍了ElasticSearch从数组字段过滤聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对数组中的值进行聚合,还可以过滤前缀返回的存储区。不确定是否有可能或我滥用过滤桶。



3个文件:

  {color:[red,black,blue]} 
{colors:[red,black]}
{颜色:[red]}

目标是获得有颜色以字母B开头:

  {
size:0,
aggs {
colors:{
filter:{prefix:{colors:b}},
aggs:{
颜色:{terms:{field:colors}}
}
}
}
}
/ pre>

结果回来包括红色不幸。显然,因为红色的文档仍然是由蓝色和/或黑色匹配的。

 聚合:{
颜色:{
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:{
前缀:{
colors:b
}
},
aggs:{
top-colors:{
条款:{
field:colors,
include:{
pattern:b。*
}
}
}
}
}
}
}


I am trying to do an aggregation on values in an array and also filter the buckets that are returned by a prefix. Not sure if this is possible or I am misusing the filter bucket.

3 documents:

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

The goal is to get a count of documents that have a color starting with the letter B:

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

The results that come back include Red unfortunately. Obviously because the documents with red still match by filter because they also have blue and/or black.

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

Is there a way to filter just the bucket results?

解决方案

Try this, it will filter the values the buckets themselves are created for:

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

这篇关于ElasticSearch从数组字段过滤聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆