# Scripted Similarity - how to get average field length

**URL:** <https://discuss.elastic.co/t/scripted-similarity-how-to-get-average-field-length/146387>\
**Category:** Elasticsearch\
**Created:** [August 28, 2018, 3:20pm UTC](https://discuss.elastic.co/t/scripted-similarity-how-to-get-average-field-length/146387 "2018-08-28T15:20:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjuppe](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@jjuppe](https://discuss.elastic.co/u/jjuppe)\
**Post date:** [August 28, 2018, 3:20pm UTC](https://discuss.elastic.co/t/scripted-similarity-how-to-get-average-field-length/146387/1 "2018-08-28T15:20:25Z")

</div>

Hello,

I am trying to reconstruct the Okapi BM25 default similarity in Elasticsearch.

For my specific setting we do not need the inverse document frequency for score calculation. In my research I haven't found a flag that could turn off idf like that so I turned to scripting a custom similarity.

My scripted similarity currently looks like this:  
{ "similarity": {  
"custom\_similarity": {  
"type": "scripted",  
"script": {  
"source": "double tf\_n = (doc.freq \* (1.2 +1)) / (doc.freq + 1.2\* (1 - 0.7 + 0.7 \* (doc.length/ _average field length_))); return  
tf\_n \* query.boost; "  
}  
}  
}  
}

How can I insert the average field length into that formula?

Thanks for any help.

Janis

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [August 30, 2018, 4:05pm UTC](https://discuss.elastic.co/t/scripted-similarity-how-to-get-average-field-length/146387/2 "2018-08-30T16:05:54Z")

</div>

You could compute the average field length by doing `double avgLen = (double) field.sumTotalTermFreq / field.docCount;`

---

<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:** [September 27, 2018, 4:05pm UTC](https://discuss.elastic.co/t/scripted-similarity-how-to-get-average-field-length/146387/3 "2018-09-27T16:05:57Z")

</div>

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