# Dynamic scoring based on doc field

**URL:** <https://discuss.elastic.co/t/dynamic-scoring-based-on-doc-field/151458>\
**Category:** Elasticsearch\
**Created:** [October 8, 2018, 1:12pm UTC](https://discuss.elastic.co/t/dynamic-scoring-based-on-doc-field/151458 "2018-10-08T13:12:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guess11](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@guess11](https://discuss.elastic.co/u/guess11)\
**Post date:** [October 8, 2018, 1:12pm UTC](https://discuss.elastic.co/t/dynamic-scoring-based-on-doc-field/151458/1 "2018-10-08T13:12:58Z")

</div>

Hi I am using elasticsearch 6.4 and I was trying to find any workable solution for my problem. So my problem is I have doc and I am trying to score depending on id from query:

`{ "rates": [{ "id": 1, "score_rate": 4.0}, { "id": 2, "score_rate": 5.0}, { "id": 3, "score_rate": 0.0}] }`

- The rates field is nested. What I was trying to achieve is based on the query boost score. initially I used script\_function:  
`{"script_score": {"script": {"params": {"id": 1, "min": 0.0}, "id": "secondary_rate"}}`

Where secondary\_rate is script in painless

```
double min_threshold = (double) params.min;
double score = min_threshold;

for (int i = 0; i < params._source.rates.length; ++i){
     def rate = params._source.rates[i];
     if (rate.id == params.id){
        c_score = category.score_rate;
        break;
}
}
return c_score;

```

But this doesn't work because nested field can't accessed in script throughout doc['rates'] and \_source field is not available anymore ([Painless null pointer exception](https://discuss.elastic.co/t/painless-null-pointer-exception/128245)) in script\_functions.

- In second attempt I tried to use combination of NestedQuery and FieldValueFactor  
something that look like: [Nested value on function score](https://discuss.elastic.co/t/nested-value-on-function-score/29935/2). But unfortunately NestedQuery make second query and join (I can't control how joined is performed) results of it with root query, and if root query is empty Result of NestedQuery just added to root results. This behavior is unacceptable for my business logic.

- On third attempt I tried to reindex rates field as array and encoded info about id in to array index so field from example above will look like:  
`{ "rates": [0, 4.0, 5.0, 0.0]}`  
Again I used script function, but there is no guarantee for order of elements. To have elements in the  
same order as in index time I need to use \_source field (that is not available in 6.4).

So I am kind of stuck on this problem and have no ideas how to solve it. So any suggestions or hints are very welcome. Thank you in advance

---

<div class="post-metadata">

**Author:** ![guess11](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@guess11](https://discuss.elastic.co/u/guess11)\
**Post date:** [October 12, 2018, 12:47pm UTC](https://discuss.elastic.co/t/dynamic-scoring-based-on-doc-field/151458/2 "2018-10-12T12:47:58Z")

</div>

Probably no one will reply. So in case you have the same problem I opened discussion on StackOverflow:

> <https://stackoverflow.com/questions/52715775/elastic-search-dynamic-scoring-based-on-doc-field>

---

<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 9, 2018, 12:54pm UTC](https://discuss.elastic.co/t/dynamic-scoring-based-on-doc-field/151458/3 "2018-11-09T12:54:02Z")

</div>

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