# Get the number of occurrences of one of matched keywords

**URL:** https://discuss.elastic.co/t/get-the-number-of-occurrences-of-one-of-matched-keywords/373294
**Category:** Elastic Search
**Created:** [January 16, 2025, 9:02pm UTC](https://discuss.elastic.co/t/get-the-number-of-occurrences-of-one-of-matched-keywords/373294 "2025-01-16T21:02:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mogabounce](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mogabounce/32/140723_2.png) [@mogabounce](https://discuss.elastic.co/u/mogabounce)
#### Post date: [January 16, 2025, 9:02pm UTC](https://discuss.elastic.co/t/get-the-number-of-occurrences-of-one-of-matched-keywords/373294/1 "2025-01-16T21:02:52Z")

</div>

Hello, there

I have the following use case:

Say I have a text field "OCR\_TEXT" and I want to search documents containing the phrase "Apple Tree" and then count the number of the occurrences of the word "Apple" (but not "Apple Tree") in each match document as the score.

I tested "\_termStats.termFreq()" introduced in 8.15 and it seems to only return the sum of frequencies of both "Apple" and "Tree". In earlier days (\<5.0), we can access term frequency of each term but we can not do so at this moment.

```auto
{
   "query": {
        "script_score": {
            "query":{
                "bool":{
                    "should": [
                        {
                            "match_phrase" : {
                                "OCR_TEXT" : {
                                    "query": "Apple Tree",
                                    "slop": 0
                                }
                            }
                        }
                    ]
                }
            },
            "script": {
                "source": "_termStats.termFreq().getSum()"
            }
        }
    }
} 

```

The example above is to use ""script\_score" as a top level query. I also tested to use "script\_score" under "function\_score" and there are two issues to supply a different query for scoring purpose. The score returned does not make sense if we do not specify a "query" field under "script\_score". An error is returned if we specify a "query" clause under "script\_socre".

Can you have a look and let us know if there is a solution? Will ELK development team consider expanding the \_termStats to contain a dictionary of all matched words so the user can freely use the frequency info of each word.

```auto
{
  "query": {
       "function_score":{
           "query":{
               "bool":{
                   "should": [
                       {
                           "match_phrase" : {
                               "OCR_TEXT" : {
                                   "query": "Apple Tree",
                                   "slop": 0
                               }
                           }
                       }
                   ]
               }
           },
           "script_score": {
               "script": {
                   "source": "_termStats.termFreq().getSum()" 
               }
           }
       }
   }
} 

```

Thank you,  
SD

---

<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: [February 13, 2025, 9:03pm UTC](https://discuss.elastic.co/t/get-the-number-of-occurrences-of-one-of-matched-keywords/373294/2 "2025-02-13T21:03:10Z")

</div>

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