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

My original post:

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.

$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

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.

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

enter image description here

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?

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