# Count the occurrence of words in ElasticSearch

**URL:** https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595
**Category:** Elasticsearch
**Tags:** elastic-stack-monitoring, elastic-stack-alerting, docker
**Created:** [December 13, 2021, 6:34am UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595 "2021-12-13T06:34:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ashrut](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Ashrut](https://discuss.elastic.co/u/Ashrut)
#### Post date: [December 13, 2021, 6:34am UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595/1 "2021-12-13T06:34:31Z")

</div>

Hello Everyone,  
I am a newbie in Elasticsearch.  
I was wondering if there is any technique to search for the count of occurrence of a word (or an array of words) in a document.

For e.g. if I want to search for the array of words ["to", "as", "cash"], in many documents (say 100000), and at my batch size is of 25 documents. I want to count how many times, these word(s) occurred in the document and show that count on my view. i.e. I want that count (not \_score) for each document and sent that value with my response in the query so that I can use it or store it for later use.

---

<div class="post-metadata">

### Author: ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)
#### Post date: [December 13, 2021, 12:40pm UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595/2 "2021-12-13T12:40:36Z")

</div>

Perhaps look into using the [term vectors API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html#docs-termvectors-api-term-stats)

```auto
Setting `term_statistics` to `true` (default is `false` ) will return

* total term frequency (how often a term occurs in all documents)

* document frequency (the number of documents containing the current term)

By default these values are not returned since term statistics can have a serious performance impact.

```

---

<div class="post-metadata">

### Author: ![Ashrut](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Ashrut](https://discuss.elastic.co/u/Ashrut)
#### Post date: [December 13, 2021, 2:58pm UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595/3 "2021-12-13T14:58:59Z")

</div>

Hi, thanks for the reply. I want the term frequency for every single document and it is going to return me the term frequency in all documents (in my case 25). So, please can you suggest a way to deduce term frequency for each document. separately, like the \_score is for each document.

---

<div class="post-metadata">

### Author: ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)
#### Post date: [December 13, 2021, 3:20pm UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595/4 "2021-12-13T15:20:40Z")

</div>

Just specify the document in the API call. For example:

```auto
POST termstest/_doc/1
{
  "message": "Hi, thanks for the reply. I want the term frequency for every single document and it is going to return me the term frequency in all documents (in my case 25). So, please can you suggest a way to deduce term frequency for each document. separately, like the _score is for each document."
}

```

and then

```auto
GET termstest/_termvectors/1?fields=message

```

You'll see the term frequency of every term - and see that the word `document` is found 3 times

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/8/9899ecef5fb66996679b67f642ce6083e4640e30.png)

---

<div class="post-metadata">

### Author: ![Ashrut](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Ashrut](https://discuss.elastic.co/u/Ashrut)
#### Post date: [December 14, 2021, 5:20am UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595/5 "2021-12-14T05:20:31Z")

</div>

Thank You so much for the solution Rich. I am working on Elasticsearch 5.6 and have integrated it with rails. I will reply here after it gets implemented and work successfully. Thanks Again.

---

<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: [January 11, 2022, 5:20am UTC](https://discuss.elastic.co/t/count-the-occurrence-of-words-in-elasticsearch/291595/6 "2022-01-11T05:20:54Z")

</div>

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