# Counting the frequency of a term

**URL:** <https://discuss.elastic.co/t/counting-the-frequency-of-a-term/22660>\
**Category:** Elasticsearch\
**Created:** [March 13, 2015, 6:31pm UTC](https://discuss.elastic.co/t/counting-the-frequency-of-a-term/22660 "2015-03-13T18:31:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reza\_sadoddin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reza_sadoddin/32/3437_2.png) [@reza\_sadoddin](https://discuss.elastic.co/u/reza_sadoddin)\
**Post date:** [March 13, 2015, 6:31pm UTC](https://discuss.elastic.co/t/counting-the-frequency-of-a-term/22660/1 "2015-03-13T18:31:59Z")

</div>

I am almost new to ElasticSearch, and need to do a simple task. I want to  
count the frequency of a word in a single document (not the number of  
documents containing a term). Assume that my index consists of a single  
document.

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/8902602c-d32c-407d-a180-368587d9fc46%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8902602c-d32c-407d-a180-368587d9fc46%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Babadofar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/babadofar/32/44803_2.png) [@Babadofar](https://discuss.elastic.co/u/Babadofar)\
**Post date:** [March 14, 2015, 9:41am UTC](https://discuss.elastic.co/t/counting-the-frequency-of-a-term/22660/2 "2015-03-14T09:41:06Z")

</div>

You can do this, but it involves scripting and is perhaps not very simple.  
The frequency of a term in a document is given as  
\_index['FIELD']['TERM'].tf()

> **[Scripting | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html)**

Combine this with a script field:

> **[Request body search | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-script-fields)**

If you are willing to take the risk to enable dynamic scripting on your  
box it can be done like this

{

"query": {

```
  "match_all": {}

```

},

"script\_fields": {

```
  "qcount": {

     "script": "_index['FIELDNAME']['QUERYTERM'].tf()"

  },

  "qfield": {

     "script": "doc['FIELDNAME']"

  }

```

}

}

fredag 13. mars 2015 19.32.00 UTC+1 skrev reza sadoddin følgende:

> I am almost new to Elasticsearch, and need to do a simple task. I want to  
> count the frequency of a word in a single document (not the number of  
> documents containing a term). Assume that my index consists of a single  
> document.
> 
> 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/57ec332c-1df7-46d6-9999-87da9115f28e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/57ec332c-1df7-46d6-9999-87da9115f28e%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:26am UTC](https://discuss.elastic.co/t/counting-the-frequency-of-a-term/22660/3 "2017-07-06T00:26:45Z")

</div>


