[Query] Limit the number of result per field content

Hello,

I have an ES index configured like this :

{
    "settings": {
        "analysis": {
            "filter": {
                "name_ngram": {
                    "type":     "ngram",
                    "min_gram": 3,
                    "max_gram": 10
                }
            },
            "analyzer": {
                "autocomplete_analyzer": {
                    "type":      "custom",
                    "tokenizer": "standard",
                    "filter":   [
                        "lowercase",
                        "name_ngram"
                    ]
                }
            }
        }
    },
    "mappings": {
        "my_type": {
            "_all": {
                "enabled": false
            },
            "properties": {
                "name": {
                    "type":     "string",
                    "analyzer": "autocomplete_analyzer",
                    "search_analyzer": "standard",
                    "index_options": "docs"
                },
                "type": {
                    "type" : "string",
                    "analyzer": "autocomplete_analyzer",
                    "search_analyzer": "standard"
                }
            }
        }
    }
}

The Type field/property can take one of the following values : {POP, ROCK, ELECTRO, CLASSICAL, JAZZ, OTHERS}

    {
      "query": {
        "multi_match": {
          "query": "clap",
          "type": "cross_fields",
          "operator": "and",
          "fields": [
            "name",
            "type"
          ]
        }
      }
    }

When I run a query query above I get for instance :

  • 40 results of type POP
  • 20 results of type JAZZ
  • 2800 results of type OTHERS.

I was wondering if there is a way to limit the number of results for the type OTHERS to 200.

Try the sampler aggregation with a nested top_hits aggregation to gather results. The sampler agg has the option to diversify on a choice of field - in your case music genre.

I've have tried many times to perform this type of query but It didn't succede.

Can you please tell me how I should add it to my multi_match query ?

Will the autocomplete feature still work after that ?

Thanks.