【问题标题】:Elasticsearch Date histogram QueryElasticsearch 日期直方图查询
【发布时间】:2015-08-07 06:57:25
【问题描述】:

我是 Elasticsearch 的新手,正在研究按日期对照片索引进行聚类。特别是,我想将 1.5 小时内拍摄的照片分组。

我知道 Elasticsearch 具有 Date Histogram Aggregation 属性,但它只返回“doc_count”。我需要查看索引上的项目,而不仅仅是数字。

什么样的查询可以满足这种需求?

以下查询供您参考:

GET /account_index/_search?


"aggs":{ 
    "zamanlar":{
        "date_histogram" : {
            "field" : "EXIF DateTimeOriginal",
            "interval" : "1.5h"
        }
    }
}

返回:

{
"took": 6,
"timed_out": false,
"_shards": {
  "total": 5,
  "successful": 5,
  "failed": 0
},
 "hits": {
   "total": 1688,
   "max_score": 0,
   "hits": []
},
 "aggregations": {
  "zamanlar": {
     "buckets": [
        {
           "key_as_string": "2007:08:11 15:00:00",
           "key": 1186844400000,
           "doc_count": 7
        },
        {
           "key_as_string": "2007:08:11 18:00:00",
           "key": 1186855200000,
           "doc_count": 1
        },
        {
           "key_as_string": "2007:08:12 00:00:00",
           "key": 1186876800000,
           "doc_count": 7
        }]}}}

我不想要 doc_count,它只是一个数字。我需要查看实际的“组成员”。提前致谢。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以为每个存储桶使用top_hits 子聚合。这样您就可以获得每个日期间隔的点击次数。

    curl -XGET localhost:9200/account_index/_search -d '{
      "aggs":{ 
        "zamanlar":{
            "date_histogram" : {
                "field" : "EXIF DateTimeOriginal",
                "interval" : "1.5h"
            },
            "aggs": {
                "hits": {
                    "top_hits": {
                        "size": 10,              <--- you can change the size
                        "sort": {"size":"desc"}  <--- and the sorting, too
                    }
                }
            }
        }
      }
    }'
    

    【讨论】:

    • 非常感谢!另外,“top_hits”会接受“过滤器”吗?
    • 是的,您只需要将 zamanlar 聚合包装到 filter 聚合中。