# How to return all documents where a string occurs in the document at least N times

**URL:** <https://discuss.elastic.co/t/how-to-return-all-documents-where-a-string-occurs-in-the-document-at-least-n-times/44619>\
**Category:** Elasticsearch\
**Created:** [March 16, 2016, 8:32pm UTC](https://discuss.elastic.co/t/how-to-return-all-documents-where-a-string-occurs-in-the-document-at-least-n-times/44619 "2016-03-16T20:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunger87](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@hunger87](https://discuss.elastic.co/u/hunger87)\
**Post date:** [March 16, 2016, 8:32pm UTC](https://discuss.elastic.co/t/how-to-return-all-documents-where-a-string-occurs-in-the-document-at-least-n-times/44619/1 "2016-03-16T20:32:12Z")

</div>

If I wanted to return all documents that contain the term `beetlejuice`, I could use a query like

```
{
    "bool":{
        "should":[
            {
                "term":{
                    "description":"beetlejuice"
                }
            }
        ]
    }
}

```

What's not clear is how to return all documents where the `description` field contains the string `beetlejuice` at least 3 times within it. I see `minimum_should_match`, but I think that is to be used for separate queries in a `bool`. How can I craft a query to match when a word occurs at least N times within the document's `description` field?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 18, 2016, 3:27pm UTC](https://discuss.elastic.co/t/how-to-return-all-documents-where-a-string-occurs-in-the-document-at-least-n-times/44619/2 "2016-03-18T15:27:34Z")

</div>

Have a look at [this](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html).

---

<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, 11:07pm UTC](https://discuss.elastic.co/t/how-to-return-all-documents-where-a-string-occurs-in-the-document-at-least-n-times/44619/3 "2017-07-05T23:07:09Z")

</div>


