# How to to count occurrences of a phrase within field text

**URL:** <https://discuss.elastic.co/t/how-to-to-count-occurrences-of-a-phrase-within-field-text/22601>\
**Category:** Elasticsearch\
**Created:** [March 10, 2015, 2:50pm UTC](https://discuss.elastic.co/t/how-to-to-count-occurrences-of-a-phrase-within-field-text/22601 "2015-03-10T14:50:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Harth](https://avatars.discourse-cdn.com/v4/letter/g/3da27b/32.png) [@George\_Harth](https://discuss.elastic.co/u/George_Harth)\
**Post date:** [March 10, 2015, 2:50pm UTC](https://discuss.elastic.co/t/how-to-to-count-occurrences-of-a-phrase-within-field-text/22601/1 "2015-03-10T14:50:22Z")

</div>

Is there a way to ask for the number of occurrences of a given phrase  
within a single field? The phrase can contain spaces.

I have been able to count instances of a single word using the terms  
aggregation, but that doesn't seem to work for phrases. I tried including  
double quotes around the phrase inside the "include" field of the terms  
aggregation, but that didn't seem to help.

For example, this will count occurrences of the word "language".

{  
"query": {  
"query\_string": {  
"query": "DocumentText:("language skills")"  
}  
},  
"aggs" : {  
"count\_terms": {  
"terms" : {  
"field" : "DocumentText", "include":"language"  
}  
}  
}  
}

But what I really need is to count of the occurrences of the full phrase  
"language skills" since "language" can occur without being followed by  
"skills", but the below doesn't work.

{  
"query": {  
"query\_string": {  
"query": "DocumentText:("language skills")"  
}  
},  
"aggs" : {  
"count\_terms": {  
"terms" : {  
"field" : "DocumentText", "include":""language skills""  
}  
}  
}  
}

Thanks...

--  
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/f0640b7f-1265-411f-884b-40c2bb0f55bf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f0640b7f-1265-411f-884b-40c2bb0f55bf%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, 12:27am UTC](https://discuss.elastic.co/t/how-to-to-count-occurrences-of-a-phrase-within-field-text/22601/2 "2017-07-06T00:27:36Z")

</div>


