ElasticSearch如何显示所有匹配日期范围聚合的文档

ElasticSearch how display all documents matching date range aggregation

以下弹性文档: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

问题:

如何进行日期范围聚合并显示与相关日期桶匹配的所有文档,而不是 doc_count

聚合:

{
    "aggs" : {
        "articles_over_time" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "1M",
                "format" : "yyyy-MM-dd" 
            }
        }
    }
}

回复:

        {
            "aggregations": {
                "articles_over_time": {
                    "buckets": [
                        {
                            "key_as_string": "2013-02-02",
                            "key": 1328140800000,
                            "doc_count": 1
                        },
                        {
                            "key_as_string": "2013-03-02",
                            "key": 1330646400000,
                            "doc_count": 2  //how display whole json ??
                 
                    [ .. Here i want to display 
                           all document with array based 
                           NOT only doc_count:2.......... ]

                        },
                        ...
                    ]
                }
            }
        }

也许我需要做一些子聚合或其他事情?

有什么想法吗?

您必须对 date-histogram 聚合执行 top_hits 子聚合。所有选项都可以从 here.

中读取

您的最终聚合结果如下所示

{
  "aggs": {
    "articles_over_time": {
      "date_histogram": {
        "field": "date",
        "interval": "1M",
        "format": "yyyy-MM-dd"
      },
      "aggs": {
        "documents": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

不过,正如 Sumit 所说,我认为您真正想要的是创建一个具有日期范围的过滤器:

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-range-query.html#ranges-on-dates

这样您就可以过滤掉不在日期范围内的文档,只保留正确的文档。比你可以用结果做任何你想做的事。