弹性搜索:搜索属于桶的文档 [英] ElasticSearch: retriving documents belonging to buckets

查看:96
本文介绍了弹性搜索:搜索属于桶的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索过去一年的文档,分别存储在1个月的大桶中。我将把每个1个月桶的文件,然后进一步分析(我的问题在这里的范围之外)。从描述来看,似乎是Bucket Aggregation是要走的路,但是在bucket响应中,我只收到每个bucket中的文档数,而不是原始文档本身。我缺少什么?

I am trying to retrieve documents for the past year, bucketed into 1 month wide buckets each. I will take the documents for each 1 month bucket, and then further analyze them (out of scope of my problem here). From the description, it seems "Bucket Aggregation" is the way to go, but in the "bucket" response, I am getting only the count of documents in each bucket, and not the raw documents itself. What am I missing?

GET命令

{
    "aggs" : {
        "DateHistogram" : {
            "date_histogram" : {
                "field" : "timestamp",
                "interval": "month"
            }
        }
    }, 
    "size" : 0
}

结果输出

{
  "took" : 138,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1313058,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "DateHistogram" : {
      "buckets" : [ {
        "key_as_string" : "2015-02-01T00:00:00.000Z",
        "key" : 1422748800000,
        "doc_count" : 270
      }, {
        "key_as_string" : "2015-03-01T00:00:00.000Z",
        "key" : 1425168000000,
        "doc_count" : 459
      }, 
      (...and all the other months...)
      {
        "key_as_string" : "2016-03-01T00:00:00.000Z",
        "key" : 1456790400000,
        "doc_count" : 136009
      } ]
    }
  }
} 


推荐答案

你几乎在那里,你只需要添加一个 top_hits 子聚合,以便为每个存储桶检索一些文档:

You're almost there, you simply need to add the a top_hits sub-aggregation in order to retrieve some documents for each bucket:

POST /your_index/_search
{
    "aggs" : {
        "DateHistogram" : {
            "date_histogram" : {
                "field" : "timestamp",
                "interval": "month"
            },
            "aggs": {                  <--- add this
                "docs": {
                    "top_hits": {
                        "size": 10
                    }
                }
            }
        }
    }, 
    "size" : 0
}

这篇关于弹性搜索:搜索属于桶的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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