Search code examples
elasticsearchelasticsearch-7

Elasticsearch terms query size to include some terms


Elasticsearch docs shows below example for size and includes

GET /_search
{
  "aggs": {
    "JapaneseCars": {
      "terms": {
        "field": "make",
        "size": 10
        "include": [ "mazda", "honda" ]
      }
    }
  }
}

But here "include" only includes "mazda" and "honda" in results, i want result to include those 2 as well other results based on doc_count since i am using size in query, is there any way to achieve this.


Solution

  • terms aggregation always return buckets with highest number of documents.

    You cannot define an aggregation to always include buckets for some specified keys AND other top buckets.

    But you could define two separate aggregations and merge buckets in your application

    GET /_search
    {
      "aggs": {
        "JapaneseCars": {
          "terms": {
            "field": "make",
            "include": [ "mazda", "honda" ]
          }
        },
        "OtherCars": {
          "terms": {
            "field": "make",
            "exclude": [ "mazda", "honda" ]
          }
        },
      }
    }