# Count number of keyword occurrences with query\_string

**URL:** <https://discuss.elastic.co/t/count-number-of-keyword-occurrences-with-query-string/190132>\
**Category:** Elasticsearch\
**Created:** [July 12, 2019, 3:54am UTC](https://discuss.elastic.co/t/count-number-of-keyword-occurrences-with-query-string/190132 "2019-07-12T03:54:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [July 12, 2019, 3:54am UTC](https://discuss.elastic.co/t/count-number-of-keyword-occurrences-with-query-string/190132/1 "2019-07-12T03:54:34Z")

</div>

Hello and good day ElasticSearch Team and Developers!

This is in relation with my StackOverflow post: [https://stackoverflow.com/questions/56932330/elasticsearch-count-number-of-keyword-occurences-in-a-document](https://stackoverflow.com/questions/56932330/elasticsearch-count-number-of-keyword-occurences-in-a-document)

I'm searching through my index named `web` with a query\_string query as:

```
[
    "query_string" => [
        "default_field" => $content,
        "query" => $keywords
    ]
]

```

Where as the `$keywords` variable contains:

```
("MCU" OR "Marvel" OR "Spiderman")

```

Meaning, I'm looking for documents throughout my Index that has those `$keywords` mentioned.

I'm satisfied with the fetched/retrieved documents, however, my dilemma is that I need to **COUNT THE NUMBER OF OCCURRENCE** those `$keywords` appeared per document.

Let's say I have a document with an ID of #17. I want to determine how many times either **MCU** or **Marvel** or **Spiderman** appeared in that document's `content` field

Any chances for this to work on Elasticsearch?

I'm using:  
Elasticsearch: v7.2  
Application: Laravel v5.7

Thank you very much!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 12, 2019, 6:16am UTC](https://discuss.elastic.co/t/count-number-of-keyword-occurrences-with-query-string/190132/2 "2019-07-12T06:16:52Z")

</div>

Would that help?

- [https://www.elastic.co/guide/en/elasticsearch/reference/7.2/docs-termvectors.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/docs-termvectors.html)
- [https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-request-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-request-explain.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:** [August 9, 2019, 6:17am UTC](https://discuss.elastic.co/t/count-number-of-keyword-occurrences-with-query-string/190132/3 "2019-08-09T06:17:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
