Search code examples
elasticsearch

Elasticsearch return document ids while doing aggregate query


Is it possible to get an array of elasticsearch document id while group by, i.e

Current output

"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
                }
            ]
        }
    }

Desired output

"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"....]
                }
            ]
        }
    }

Not sure if this is possible in elasticsearch or not, below is my aggregation query:

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

Elasticsearch version: 6.3.2


Solution

  • You can use top_hits aggregation which will return all documents under an aggregation. Using source filtering you can select fields under hits

    Query:

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