# Word count/frequency per field

**URL:** https://discuss.elastic.co/t/word-count-frequency-per-field/159910
**Category:** Elasticsearch
**Created:** [December 7, 2018, 10:37am UTC](https://discuss.elastic.co/t/word-count-frequency-per-field/159910 "2018-12-07T10:37:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![M.alsioufi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.alsioufi/32/52269_2.png) [@M.alsioufi](https://discuss.elastic.co/u/M.alsioufi)
#### Post date: [December 7, 2018, 10:37am UTC](https://discuss.elastic.co/t/word-count-frequency-per-field/159910/1 "2018-12-07T10:37:39Z")

</div>

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

for example:  
curl -XPUT '[http://localhost:9200/twitter/tweet/1?pretty=true](http://localhost:9200/twitter/tweet/1?pretty=true)' -d '{  
"text1" : "twitter, test, test, test ",  
"text2" : "test, test, man, two "  
}'

then count words that I need from that document are:  
text1:{  
"twitter":1,  
"test":3  
},  
text2: {  
"test":2,  
"man":1,  
"two":1  
}  
or something similar

I know I can use termvector but I could not really understand how this can help me.

Thank you

---

<div class="post-metadata">

### Author: ![SaskiaVola](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@SaskiaVola](https://discuss.elastic.co/u/SaskiaVola)
#### Post date: [December 10, 2018, 2:10pm UTC](https://discuss.elastic.co/t/word-count-frequency-per-field/159910/2 "2018-12-10T14:10:48Z")

</div>

Hey,

the [`_termvector` API](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-termvectors.html) is the best way to access information about term statistics in Elasticsearch after your data has been indexed.

If you want to get the length of a field in tokens, you can use the [type `token_count` in your mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html).

What problem are you trying to solve?

---

<div class="post-metadata">

### Author: ![M.alsioufi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.alsioufi/32/52269_2.png) [@M.alsioufi](https://discuss.elastic.co/u/M.alsioufi)
#### Post date: [December 13, 2018, 4:37pm UTC](https://discuss.elastic.co/t/word-count-frequency-per-field/159910/3 "2018-12-13T16:37:30Z")

</div>

Thanks for your answer.

I have tried termvector API it does almost what I expect however, I have to run it on one specific document I could not run it on my entire index. so following the same example I showed.  
My request looks like:  
GET my\_index/doc/someid123/\_termvectors  
{  
"fields": ["text1"]

}  
and then the reply I get:  
{  
"\_index": "my\_index",  
"\_type": "doc",  
"\_id": "someid123",  
"\_version": 2,  
"found": true,  
"took": 1,  
"term\_vectors": {  
"text1": {  
"field\_statistics": {  
"sum\_doc\_freq": 56,  
"doc\_count": 54,  
"sum\_ttf": 60  
},  
"terms": {  
"test": {  
"term\_freq": 3,  
"tokens": [  
{  
"position": 0,  
"start\_offset": 9,  
"end\_offset": 12  
},  
{  
"position": 1,  
"start\_offset": 15,  
"end\_offset": 19  
},  
{  
"position": 2,  
"start\_offset": 21,  
"end\_offset": 25  
}  
]  
},  
"twitter":{  
"term\_freq": 1,  
"tokens": [  
{  
"position": 0,  
"start\_offset": 0,  
"end\_offset": 6  
}  
]  
}  
}  
}  
}  
}

What I want to do is to be able to get this functionality among all my index not a special document, and to be able to show this data in a Kibana visualization

---

<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 10, 2019, 4:37pm UTC](https://discuss.elastic.co/t/word-count-frequency-per-field/159910/4 "2019-01-10T16:37:42Z")

</div>

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