进行聚合查询时Elasticsearch返回文档ID

时间:2020-05-22 11:35:09

标签: elasticsearch

是否可以按分组方式(例如,

)获取Elasticsearch文档ID的数组

当前输出

"aggregations": {,
        "types": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Text Document",
                    "doc_count": 3310
                },
                {
                    "key": "Unknown",
                    "doc_count": 15
                },
                {
                    "key": "Document",
                    "doc_count": 13
                }
            ]
        }
    }

所需的输出

"aggregations": {,
        "types": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Text Document",
                    "doc_count": 3310,
                    "ids":["doc1","doc2", "doc3"....]
                },
                {
                    "key": "Unknown",
                    "doc_count": 15,
                    "ids":["doc11","doc12", "doc13"....]
                },
                {
                    "key": "Document",  
                    "doc_count": 13
                    "ids":["doc21","doc22", "doc23"....]
                }
            ]
        }
    }

不确定在Elasticsearch中是否可行, 以下是我的汇总查询:

{
    "size": 0,
    "aggs": {
        "types": {
            "terms": {
                "field": "docType",
                "size": 10
            }
        }
    }
}

Elasticsearch版本: 6.3.2

1 个答案:

答案 0 :(得分:1)

您可以使用top_hits aggregation来返回汇总下的所有文档。使用源过滤,您可以选择匹配项下的字段

查询:

  "aggs": {
    "district": {
      "terms": {
        "field": "docType",
        "size": 10
      },
      "aggs": {
        "docs": {
          "top_hits": {
            "size": 10,
            "_source": ["ids"]
          }
        }
      }
    }
  }