# Count the number of occurence of my keywords from all the retrieved documents

**URL:** https://discuss.elastic.co/t/count-the-number-of-occurence-of-my-keywords-from-all-the-retrieved-documents/197452
**Category:** Elasticsearch
**Created:** [August 30, 2019, 7:01am UTC](https://discuss.elastic.co/t/count-the-number-of-occurence-of-my-keywords-from-all-the-retrieved-documents/197452 "2019-08-30T07:01:14Z")
**Posts on this page:** 2
**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: [August 30, 2019, 7:01am UTC](https://discuss.elastic.co/t/count-the-number-of-occurence-of-my-keywords-from-all-the-retrieved-documents/197452/1 "2019-08-30T07:01:14Z")

</div>

My original post:

> <https://stackoverflow.com/questions/57707099/count-the-number-of-occurrence-of-my-keywords-from-all-the-retrieved-documents>

Applications I'm using:  
Elasticsearch: v7.1  
PHP: v7.2  
Laravel: v5.8  
Elasticsearch/elasticsearch, official API of Elasticsearch to PHP

Hello and good day! Is it possible to do an Elasticsearch query to count the number of occurrence a keyword had from all the retrieved documents?

I mean for example, you have a `query_string` query, then you're looking for the word "Dog" from all 1000 gathered documents, is there a query so we can count how many times the word "Dog" appeared from those 1000 documents?

**Tried solutions**

**SOLUTION 1**  
Tried to create a recursive function that will retrieve details from the **explanation** (I added `"explain" => true"` in my params) but the problem here is, it will be memory intensive 'coz the "explanation" appears IN EVERY DOCUMENT/HITS. So I would not dare into making a loop just to sum the frequencies, especially if I had more than a thousand list of documents.

```auto
$params = [
    'index' => $index,
    'type' => 'index',
    'explain' => true
    'body' => [ ...
    ]
]

```

**SOLUTION 2**  
Also tried to use **highlights** but it has the same approach with the **explanation** , it appears IN EVERY DOCUMENT, so this option isn't recommended too

[![Sample output](https://us1.discourse-cdn.com/elastic/original/3X/b/b/bb470a7fea9d364f8c4d9a06f848fa7d10b5d507.png)](https://i.stack.imgur.com/0RlZv.png)

**SOLUTION 3**  
Tried to use **termvectors** API, everything is perfect and working well, HOWEVER, the **termvectors** API is asking for the specific IDs of the documents to be observed from. So theoretically, it's the same with the 2 options above, but this time, needs to indicate ALL IDs of the documents that were fetched by my query.

```auto
$search = $client->termvectors($params);

```

[![enter image description here](https://us1.discourse-cdn.com/elastic/original/3X/d/5/d596cacc6bb2936d73d04fe9d965d95999d14461.png)](https://i.stack.imgur.com/481pT.png)

What I'm looking for is an API or a function perhaps from the Elasticsearch whereas I can code just like the behavior of the **aggs / aggregations** so I can determine the NUMBER OF OCCURRENCE of my keywords throughout/from the documents that have been retrieved by my query

Is it possible to do it with the current API list of Elasticsearch?

---

<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: [September 27, 2019, 7:02am UTC](https://discuss.elastic.co/t/count-the-number-of-occurence-of-my-keywords-from-all-the-retrieved-documents/197452/2 "2019-09-27T07:02:20Z")

</div>

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