# Is it possible to use lucene expressions in elasticsearch scripts to use tfidf similarities of specific fields as a part of custom scoring function

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-use-lucene-expressions-in-elasticsearch-scripts-to-use-tfidf-similarities-of-specific-fields-as-a-part-of-custom-scoring-function/170904>\
**Category:** Elasticsearch\
**Created:** [March 5, 2019, 1:06pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-lucene-expressions-in-elasticsearch-scripts-to-use-tfidf-similarities-of-specific-fields-as-a-part-of-custom-scoring-function/170904 "2019-03-05T13:06:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olga\_Gorun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olga_gorun/32/41560_2.png) [@Olga\_Gorun](https://discuss.elastic.co/u/Olga_Gorun)\
**Post date:** [March 5, 2019, 1:06pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-lucene-expressions-in-elasticsearch-scripts-to-use-tfidf-similarities-of-specific-fields-as-a-part-of-custom-scoring-function/170904/1 "2019-03-05T13:06:19Z")

</div>

I want to build custom Elasticsearch scoring function and see that combines TF-IDF similarities of some document fields with relative given parameters with the help. As Elasticsearch scripts provide access to some Lucene functionality with Lucene expressions language, I hope it's possible to use Lucene implementation of a TF-IDF.

Unfortunately, I see almost no documentation and examples for lucene expression scripts for scoring functions. Is it possible? And how?

---

<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:** [March 9, 2019, 8:06pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-lucene-expressions-in-elasticsearch-scripts-to-use-tfidf-similarities-of-specific-fields-as-a-part-of-custom-scoring-function/170904/2 "2019-03-09T20:06:22Z")

</div>

This won't be possible in expressions; they only have access to the fields of a document and parameters, along with a few static helper functions. You would need to write a native java script. See the [advanced scripting example](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-engine.html) in our docs.

---

<div class="post-metadata">

**Author:** ![Olga\_Gorun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olga_gorun/32/41560_2.png) [@Olga\_Gorun](https://discuss.elastic.co/u/Olga_Gorun)\
**Post date:** [March 9, 2019, 8:24pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-lucene-expressions-in-elasticsearch-scripts-to-use-tfidf-similarities-of-specific-fields-as-a-part-of-custom-scoring-function/170904/3 "2019-03-09T20:24:31Z")

</div>

@rjernst Thank you for response. I am actually implementing it on painless. It's feasible (though not convenient). Do you see any advantage to implement it on Java (especially from time performance point of view)?

---

<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:** [April 6, 2019, 8:24pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-lucene-expressions-in-elasticsearch-scripts-to-use-tfidf-similarities-of-specific-fields-as-a-part-of-custom-scoring-function/170904/4 "2019-04-06T20:24:34Z")

</div>

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