聚合数字数组字段时的Elasticsearch预期行为

时间:2016-08-16 18:28:35

标签: elasticsearch

我有一个聚合查询,根据数值数组字段中的值将我的数据合并到直方图存储桶中。该数组几乎总是长度为1,但我无法保证。 在作为数组的字段上进行binning时的预期行为是什么。

据我所知,如果这个查询是必须的,我们可能需要修改架构,但仍想了解弹性在这里的表现。

示例文件:

    {
      "begin": "100",
      "total": 20,
      "fractions": [ 10 ]
    }

示例查询:

GET /index-2/_search
{
  "size": 10, 
  "aggs": {
    "buckets": {
      "histogram": {
        "field": "begin",
        "interval": 1000
      },
      "aggs": {
        "fractions": {
          "histogram": {
            "field": "fractions",
            "interval": 10
          }
        }
        "totals": {
          "histogram": {
            "field": "totals",
            "interval": 10
          }
        }
      }
    }
  }
}

在400到500万个文档上运行此查询时,我没有遇到任何错误。下面的示例响应代码段:

"aggregations": {
  "buckets": {
    "buckets": [
    {
      "key": 0,
      "doc_count": 1235,
      "fractions": {
        "buckets": [
          {
            "key": 0,
            "doc_count": 402
          },
          {
            "key": 10,
            "doc_count": 176
          },

          ...

          {
            "key": 480,
            "doc_count": 0
          },
          {
            "key": 490,
            "doc_count": 1
          }
        ]
      },
      "totals": {
        "buckets": [
          {
            "key": 0,
            "doc_count": 271
          },
          {
            "key": 10,
            "doc_count": 117
          },

          ...

          {
            "key": 550,
            "doc_count": 0
          },
          {
            "key": 560,
            "doc_count": 1
          }
        ]
      }
    },
    ...

1 个答案:

答案 0 :(得分:0)

数组中的值将有助于为该时间间隔创建存储桶。如果与查询匹配的文档的fractions值为10, 50, 90, 100,并且其中一个文档的[10, 20, 150]fractions,则这些值将基本上增加输出桶的“可用”值数组。 fractions聚合将涵盖从10150的所有广告资源。

例如,像{"begin":100,"total":20,"fractions":[10,35,55]}这样的聚合文件(如"fractions": {"histogram": {"field": "fractions", interval": 5}}这样的聚合生成聚合结果,如

           "fractions": {
              "buckets": [
                 {
                    "key": 10,
                    "doc_count": 1
                 },
                 {
                    "key": 15,
                    "doc_count": 0
                 },
                 {
                    "key": 20,
                    "doc_count": 0
                 },
                 {
                    "key": 25,
                    "doc_count": 0
                 },
                 {
                    "key": 30,
                    "doc_count": 0
                 },
                 {
                    "key": 35,
                    "doc_count": 1
                 },
                 {
                    "key": 40,
                    "doc_count": 0
                 },
                 {
                    "key": 45,
                    "doc_count": 0
                 },
                 {
                    "key": 50,
                    "doc_count": 0
                 },
                 {
                    "key": 55,
                    "doc_count": 1
                 }
              ]
           }

基本上,值列表只是聚合集中的一组附加术语。