疲劳是最舒适的枕头,努力工作吧。

使用terms等bucket aggregations进行聚合后,如何看到bucket内文档的全部字段?

Elasticsearch | 作者 liyh122 | 发布于2018年04月23日 | 阅读数:4589

以terms为例,进行如下聚合:
GET /_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "srcIP": {
      "terms": {
        "field": "src_ip",
        "size": 1
      }
    }
  }
}
 
得到以下结果:
{
  "took": 400,
  "timed_out": false,
  "_shards": {
    "total": 60,
    "successful": 60,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 24984543,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "srcIP": {
      "doc_count_error_upper_bound": 107450,
      "sum_other_doc_count": 22814696,
      "buckets": [
        {
          "key": "1.2.3.4",
          "doc_count": 2153964
        }
      ]
    }
  }
}
 
默认在bucket中看到src_ip字段为1.2.3.4文档的数量,同时也可以对这些文档进行子聚合,但如何看到这些src_ip为1.2.3.4的文档的其他字段?比如我现在的需求就是将src_ip为1.2.3.4的文档全部字段插入数据库。
请大家指点,在线等~
已邀请:

strglee

赞同来自: liyh122 王社英

top_hits 可以满足你的需求 
GET /_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"srcIP": {
"terms": {
"field": "src_ip",
"size": 1
},
"aggs": {
"top_ip_hits": {
"top_hits": {
"size": 10
}
}
}
}
}
}

但是如果你需要全部的文档 比如上面 1.2.3.4 的文档数是2153964,还是进行二次查询吧 

liyh122

赞同来自:

谢谢

要回复问题请先登录注册