# Get count of matches in documents

**URL:** <https://discuss.elastic.co/t/get-count-of-matches-in-documents/18871>\
**Category:** Elasticsearch\
**Created:** [July 24, 2014, 3:22pm UTC](https://discuss.elastic.co/t/get-count-of-matches-in-documents/18871 "2014-07-24T15:22:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gian\_Maria\_Ricci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gian_maria_ricci/32/1377_2.png) [@Gian\_Maria\_Ricci](https://discuss.elastic.co/u/Gian_Maria_Ricci)\
**Post date:** [July 24, 2014, 3:22pm UTC](https://discuss.elastic.co/t/get-count-of-matches-in-documents/18871/1 "2014-07-24T15:22:14Z")

</div>

I can search in documents for a term and get the count of that term in the  
document with

{  
"query": {  
"query\_string": {  
"query": "myterm"  
}  
},  
"script\_fields": {  
"term\_count": {  
"script": "\_index['text']['myterm'].tf()"  
}  
}  
}

Now suppose that the query is something like "term1 term2"~4 (term1 and  
term2 with no more than 4 words between them), is that a way to obtain the  
number of match of this combination inside the documents? I can count  
occurrence of both terms with

{  
"query": {  
"query\_string": {  
"query": ""term1 term2"~1"  
}  
},  
"script\_fields": {  
"term1\_count": {  
"script": "\_index['text']['term1'].tf()"  
},  
"term2\_count": {  
"script": "\_index['text']['term2"].tf()"  
}  
}  
}

But this does not tells me if they are one near the other. I though of  
asking for highlight and count number of higlights but I think this is not  
so correct.

Any clue?

Thanks in advance for any help.

--  
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/48f5dc53-17bb-483e-b4d7-2e0613729fe8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/48f5dc53-17bb-483e-b4d7-2e0613729fe8%40googlegroups.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:13am UTC](https://discuss.elastic.co/t/get-count-of-matches-in-documents/18871/2 "2017-07-06T01:13:25Z")

</div>


