# Native scoring script: iterate over indexed terms of a field

**URL:** <https://discuss.elastic.co/t/native-scoring-script-iterate-over-indexed-terms-of-a-field/20750>\
**Category:** Elasticsearch\
**Created:** [November 14, 2014, 3:07pm UTC](https://discuss.elastic.co/t/native-scoring-script-iterate-over-indexed-terms-of-a-field/20750 "2014-11-14T15:07:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![janek\_sendrowski](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@janek\_sendrowski](https://discuss.elastic.co/u/janek_sendrowski)\
**Post date:** [November 14, 2014, 3:07pm UTC](https://discuss.elastic.co/t/native-scoring-script-iterate-over-indexed-terms-of-a-field/20750/1 "2014-11-14T15:07:05Z")

</div>

Hi,

I'm trying to calculate a metric distance using a native scoring script on  
my Elasticsearch matches between my query string terms and the found field  
term terms of my indexed field.  
Therefore I have to retrieve _all_ the indexed field terms.

But it seems to me than I can just get them _separately_ and only by index  
of the actual term which I don not know if certain term it not present in  
my query string:

This is the method to retrieve the terms separately. I have not seen  
another one  
[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/search/lookup/IndexField.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/search/lookup/IndexField.java)  
class: org.elasticsearch.search.lookup.IndexField;

Method get() from class _IndexField_:

public IndexFieldTerm get(Object key, int flags) { String termString = (  
String) key;  
IndexFieldTerm indexFieldTerm = terms.get(termString);  
// see if we initialized already...  
if (indexFieldTerm == null) {  
indexFieldTerm = new IndexFieldTerm(termString, fieldName,  
indexLookup, flags);  
terms.put(termString, indexFieldTerm);  
}  
indexFieldTerm.validateFlags(flags);  
return indexFieldTerm;  
}

So is there any way to retrieve all the terms and iterate over them?  
The groovy plugin provides this possibility, but I like to use a _native  
script_, because it's faster.

Or is my approach simply not very good and there is a much easier way to  
calculate my own metric distance?

Moreover I like to know if there is a way to calculate the base score  
without using the _tf_ and _idf_.  
I know there is a _constant\_score_ query, which provides this option, but  
the score is always 1, regardless of the number of _term occurences_.

I cannot use the way proposed over here:  
[http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/ignoring-tfidf.html](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/ignoring-tfidf.html)  
I just have too many terms.

I hope there is someone who can answer my questions.  
Thank you!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5c9dc373-ea47-40b5-9699-ef2dfd95aea6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c9dc373-ea47-40b5-9699-ef2dfd95aea6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:49am UTC](https://discuss.elastic.co/t/native-scoring-script-iterate-over-indexed-terms-of-a-field/20750/2 "2017-07-06T00:49:59Z")

</div>


