# \[Query\] Limit the number of result per field content

**URL:** https://discuss.elastic.co/t/query-limit-the-number-of-result-per-field-content/53795
**Category:** Elasticsearch
**Created:** [June 23, 2016, 3:35pm UTC](https://discuss.elastic.co/t/query-limit-the-number-of-result-per-field-content/53795 "2016-06-23T15:35:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mehdif](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mehdif](https://discuss.elastic.co/u/mehdif)
#### Post date: [June 23, 2016, 3:35pm UTC](https://discuss.elastic.co/t/query-limit-the-number-of-result-per-field-content/53795/1 "2016-06-23T15:35:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [June 23, 2016, 4:21pm UTC](https://discuss.elastic.co/t/query-limit-the-number-of-result-per-field-content/53795/2 "2016-06-23T16:21:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mehdif](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mehdif](https://discuss.elastic.co/u/mehdif)
#### Post date: [June 24, 2016, 8:53am UTC](https://discuss.elastic.co/t/query-limit-the-number-of-result-per-field-content/53795/3 "2016-06-24T08:53:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:40pm UTC](https://discuss.elastic.co/t/query-limit-the-number-of-result-per-field-content/53795/4 "2017-07-05T22:40:48Z")

</div>


