# How to get the count of matched search term in all the fields in each resulting document

**URL:** <https://discuss.elastic.co/t/how-to-get-the-count-of-matched-search-term-in-all-the-fields-in-each-resulting-document/19454>\
**Category:** Elasticsearch\
**Created:** [August 26, 2014, 5:40am UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-matched-search-term-in-all-the-fields-in-each-resulting-document/19454 "2014-08-26T05:40:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manoj\_Acharya](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@Manoj\_Acharya](https://discuss.elastic.co/u/Manoj_Acharya)\
**Post date:** [August 26, 2014, 5:40am UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-matched-search-term-in-all-the-fields-in-each-resulting-document/19454/1 "2014-08-26T05:40:22Z")

</div>

Hello,

We have a web application, where we are providing a global search feature  
through Elasticsearch. One can use this feature to search any text across  
all the documents (and on all fields). We are using '\_all' in fields while  
querying elastic search. This yields us the desired results perfectly.  
Below is the sense example.  
{  
"query": {  
"query\_string": {  
"query": anysearchterm,  
"fields": ["\_all"]  
}  
}  
}

Now what we want next is the precise relevance of the search result. We  
want to know,\* in which all fields, within each of the resulting document,  
did elastic search found the match for the search term\*. Thus we want to  
know the occurrence of the search term, _with respect to individual fields_,  
for all the resulting documents.

We have been searching for this for quiet some time. We have tried 'explain'  
option while querying, but this does not give field specific count of the  
search term.

Any kind of help is highly appreciated. We would be really greatfull if  
anybody can put some insights on this.

Thank you,  
Manoj

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8ce38b12-f6c3-4ffb-b32c-64a6c738bc20%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8ce38b12-f6c3-4ffb-b32c-64a6c738bc20%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [August 26, 2014, 11:53am UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-matched-search-term-in-all-the-fields-in-each-resulting-document/19454/2 "2014-08-26T11:53:31Z")

</div>

Hello Manoj ,

The explain might give what you are looking for , but i need to look into  
it.  
Have you tried highlighting for this ?  
You can go through the number of fragments or number of highlighted terms  
to find this.

Anyway following will yield you the result -  
{  
"script\_fields": {  
"test1": {  
"script": "\_index['message']['term'].tf()"  
}  
}  
}

Thanks  
Vineeth

On Tue, Aug 26, 2014 at 11:10 AM, Manoj Acharya [manoj.acharya15@gmail.com](mailto:manoj.acharya15@gmail.com)  
wrote:

> Hello,
> 
> We have a web application, where we are providing a global search feature  
> through Elasticsearch. One can use this feature to search any text across  
> all the documents (and on all fields). We are using '\_all' in fields while  
> querying Elasticsearch. This yields us the desired results perfectly.  
> Below is the sense example.  
> {  
> "query": {  
> "query\_string": {  
> "query": anysearchterm,  
> "fields": ["\_all"]  
> }  
> }  
> }
> 
> Now what we want next is the precise relevance of the search result. We  
> want to know,\* in which all fields, within each of the resulting  
> document, did Elasticsearch found the match for the search term\*. Thus  
> we want to know the occurrence of the search term, _with respect to  
> individual fields_, for all the resulting documents.
> 
> We have been searching for this for quiet some time. We have tried  
> 'explain' option while querying, but this does not give field specific  
> count of the search term.
> 
> Any kind of help is highly appreciated. We would be really greatfull if  
> anybody can put some insights on this.
> 
> Thank you,  
> Manoj
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/8ce38b12-f6c3-4ffb-b32c-64a6c738bc20%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8ce38b12-f6c3-4ffb-b32c-64a6c738bc20%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/8ce38b12-f6c3-4ffb-b32c-64a6c738bc20%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8ce38b12-f6c3-4ffb-b32c-64a6c738bc20%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mAynB84MWUe7ZQBidufi3Qu5TYqnhV%2BGLLN0VzcNowqg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mAynB84MWUe7ZQBidufi3Qu5TYqnhV%2BGLLN0VzcNowqg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:06am UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-matched-search-term-in-all-the-fields-in-each-resulting-document/19454/3 "2017-07-06T01:06:31Z")

</div>


