# Count of phrase matches per document

**URL:** <https://discuss.elastic.co/t/count-of-phrase-matches-per-document/96762>\
**Category:** Elasticsearch\
**Created:** [August 11, 2017, 11:29am UTC](https://discuss.elastic.co/t/count-of-phrase-matches-per-document/96762 "2017-08-11T11:29:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![swleighton](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@swleighton](https://discuss.elastic.co/u/swleighton)\
**Post date:** [August 11, 2017, 11:29am UTC](https://discuss.elastic.co/t/count-of-phrase-matches-per-document/96762/1 "2017-08-11T11:29:42Z")

</div>

Hi,

I'm using this query to search a field for occurrences of phrases.

```
"query": {
    "match_phrase": {
       "content": "my test phrase"
  }
 }

```

I need to calculate how many matches occurred for each phrase per document (if this is even possible?)

I've considered aggregators but think these don't meet the requirements as these will give me the number of matches over the whole index not per document. `enter code here

Thanks.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 15, 2017, 2:27pm UTC](https://discuss.elastic.co/t/count-of-phrase-matches-per-document/96762/2 "2017-08-15T14:27:59Z")

</div>

I don't think there's a straightforward way to get those phrase counts from Elasticsearch.

If it's just the occasional query for which you would want to know this, you could add `"explain": true` to your search request (on the same level as your `"query"`). The response will contain a `phraseFreq`, which is the phrase frequency, i.e. the number of times the phrase occurs in the `content` field of this document.

Keep in mind though that adding `"explain"` to your search request will slow down your queries significantly, so it's definitely not something you want to do for all search requests. But for the occasional request, it's perfectly fine.

If you need to know the phrase frequency for a lot of requests, you may want to look into custom scoring as documented here: [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/modules-scripting-engine.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/modules-scripting-engine.html) . You could calculate a score that is purely based on the phrase frequency. This is however quite advanced.

---

<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 12, 2017, 2:28pm UTC](https://discuss.elastic.co/t/count-of-phrase-matches-per-document/96762/3 "2017-09-12T14:28:01Z")

</div>

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