# How do I access term frequency in painless scripting?

**URL:** <https://discuss.elastic.co/t/how-do-i-access-term-frequency-in-painless-scripting/200976>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 25, 2019, 7:27am UTC](https://discuss.elastic.co/t/how-do-i-access-term-frequency-in-painless-scripting/200976 "2019-09-25T07:27:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Wilde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ben_wilde/32/36314_2.png) [@Ben\_Wilde](https://discuss.elastic.co/u/Ben_Wilde)\
**Post date:** [September 25, 2019, 7:27am UTC](https://discuss.elastic.co/t/how-do-i-access-term-frequency-in-painless-scripting/200976/1 "2019-09-25T07:27:34Z")

</div>

I want to do an aggregation where I measure the average frequency of a given term. The only way I can see of doing this is via a painless script. But according to the docs, I only have `doc` available which has all the fields and terms, but I don't know how to access the actual term frequencies that have already been calculated and stored in the index via the script.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 4, 2019, 3:43am UTC](https://discuss.elastic.co/t/how-do-i-access-term-frequency-in-painless-scripting/200976/2 "2019-10-04T03:43:40Z")

</div>

Term frequencies are not available within painless. However, this can be accomplished through [advanced scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-engine.html).

---

<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:** [November 1, 2019, 3:43am UTC](https://discuss.elastic.co/t/how-do-i-access-term-frequency-in-painless-scripting/200976/3 "2019-11-01T03:43:41Z")

</div>

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