# Count words/tokens in a field in a document

**URL:** <https://discuss.elastic.co/t/count-words-tokens-in-a-field-in-a-document/68811>\
**Category:** Elasticsearch\
**Created:** [December 13, 2016, 4:01am UTC](https://discuss.elastic.co/t/count-words-tokens-in-a-field-in-a-document/68811 "2016-12-13T04:01:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jimmy\_Oentung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimmy_oentung/32/13827_2.png) [@Jimmy\_Oentung](https://discuss.elastic.co/u/Jimmy_Oentung)\
**Post date:** [December 13, 2016, 4:01am UTC](https://discuss.elastic.co/t/count-words-tokens-in-a-field-in-a-document/68811/1 "2016-12-13T04:01:30Z")

</div>

Hi there,  
is there any convenient way to get the count of words/tokens in each field of a document?

I know I can use termvector and sum all term frequency in each field to get this number. but I was wondering if there is a faster way to do it.

for example:  
curl -XPUT '[http://localhost:9200/twitter/tweet/1?pretty=true](http://localhost:9200/twitter/tweet/1?pretty=true)' -d '{  
"fullname" : "John Doe",  
"text" : "twitter test test test "  
}'

then count words that I need from that document are:  
fullname: 2  
text: 4

One more thing, I need the total number of words that actually stored (i.e. after filtering the stopwords).

Thank you

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [December 13, 2016, 10:42am UTC](https://discuss.elastic.co/t/count-words-tokens-in-a-field-in-a-document/68811/2 "2016-12-13T10:42:58Z")

</div>

Hi,

have you looked at the [token\_count](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/token-count.html) datatype? It looks like it might be doing what you are trying to do. In order to retrieve the values calculated for the count field you might need to set it to `"store" : true`. If you follow the example in the reference, in order to retrieve the values you can use

```auto
GET my_index/_search?stored_fields=name.length

```

Also it seems to support analyzers.

---

<div class="post-metadata">

**Author:** ![Jimmy\_Oentung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimmy_oentung/32/13827_2.png) [@Jimmy\_Oentung](https://discuss.elastic.co/u/Jimmy_Oentung)\
**Post date:** [December 14, 2016, 4:09am UTC](https://discuss.elastic.co/t/count-words-tokens-in-a-field-in-a-document/68811/4 "2016-12-14T04:09:21Z")

</div>

Hi Christoph,  
thank you for your respond. Yeah, I tried the token\_count and it works. but it counts the original words count not the one after filtering out the stopwords.

Any idea how to get the count of words after removing stopwords?

Thank you

---

<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:** [January 11, 2017, 4:09am UTC](https://discuss.elastic.co/t/count-words-tokens-in-a-field-in-a-document/68811/5 "2017-01-11T04:09:40Z")

</div>

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