Elastic Search 如何按日期分组(当嵌套在数组中时)

Elastic Search how to group by date (when nested in arrays)

我的数据库结构如下:

{
    name:"string",
    ....
    arrayData: [ 
        { price:100, date: "yyyy-mm-dd" },
        { price:120, date: "yyyy-mm-dd" } ,
        { price:150, date: "yyyy-mm-dd" } ,
        { price:250, date: "yyyy-mm-dd" } 
    ]
}

如何获得 2015 年 的平均价格?

我目前的方法是使用此过滤器汇总价格:

{
  "range": {
    "arrayData.date": {
      "gte": "2015-01-01",
      "lt": "2016-01-01"
    }
  }
}

测试聚合:

"aggs": {
    "2": {
      "date_histogram": {
        "field": "arrayData.date",
        "interval": "1y",
        "time_zone": "Europe/Helsinki",
        "min_doc_count": 1
      },
      "aggs": {
        "1": {
          "avg": {
            "field": "arrayData.price"
          }
        }
      }
    }
  }

但这也将在 arrayData 中获得具有匹配日期的文档中其他年份的平均值。

这也应该与 Kibana 一起使用,因为最后我必须将它添加到仪表板。

使用术语聚合你会得到你的结果。

       {
            "aggs": {
                 "date": {
                 "terms": {
                 "field": "date"
       },
           "aggs": {
                 "average": {
                 "avg": {
                 "field": "price"
                        }
                         }
                  }
                  }
                  }
                   }

看来我需要将数组中的对象映射为 nested

根据 Elastic 文档:

Arrays of inner object fields do not work the way you may expect. Lucene has no concept of inner objects, so Elasticsearch flattens object hierarchies into a simple list of field names and values. For instance, the following document:

{
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

The user field is dynamically added as a field of type object and would be transformed internally into a document that looks more like this:

{
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}