2

I'm using Elasticsearch (version 2.3) to search in an index with several document types. I want to limit the number of results to the top X search hits for each document type. How can that be achieved?

The query is very plain and simple:

{
  "sort": [
    {
      "_type": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "query_string": {
      "query": "[some search query here]"
    }
  }
}

I found the answer at https://stackoverflow.com/a/27720049/467650 , but it seems like the syntax may have changed since version 1.4.0 as I'm only getting the following error message:

"reason": {
    "type": "search_parse_exception",
    "reason": "Found two aggregation type definitions in [types]: [terms] and [hits]",
    "line": 1,
    "col": 44
}
Community
  • 1
  • 1
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76

1 Answers1

2

I would do it with a terms aggregation on the type meta field and then use a top_hits sub-aggregation, like this:

{
  "size": 0,
  "aggs": {
    "types": {
      "terms": {
        "field": "_type"
      },
      "aggs": {
        "topten": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360