# Unable to retrieve field value from Doc Values in nested function\_score query

**URL:** <https://discuss.elastic.co/t/unable-to-retrieve-field-value-from-doc-values-in-nested-function-score-query/80052>\
**Category:** Elasticsearch\
**Created:** [March 26, 2017, 7:57pm UTC](https://discuss.elastic.co/t/unable-to-retrieve-field-value-from-doc-values-in-nested-function-score-query/80052 "2017-03-26T19:57:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junjun\_Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junjun_zhang/32/45613_2.png) [@Junjun\_Zhang](https://discuss.elastic.co/u/Junjun_Zhang)\
**Post date:** [March 26, 2017, 7:57pm UTC](https://discuss.elastic.co/t/unable-to-retrieve-field-value-from-doc-values-in-nested-function-score-query/80052/1 "2017-03-26T19:57:14Z")

</div>

Hi all,

We are using function\_score query to sort search results. The score takes into account the value of a field in the indexed document. However, it appears the field value can not be properly retrieved from Doc Values, it always returns '0' although the actual value is otherwise.

[Here](https://gist.github.com/junjun-zhang/b28c62f9273f6ba8a7073a33a95e20bf) I prepared some documents and query to illustrate the problem, you may try it out in [CONSOLE](http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://gist.githubusercontent.com/junjun-zhang/b28c62f9273f6ba8a7073a33a95e20bf/raw/1d6419ddf64169548257806b40d26ef4ef41a8ca/Test%2520in%2520Elasticsearch%2520CONSOLE).

As the example shows the field 'reward\_factor' in the indexed document is populated with non zero value, however, when it's retrieved using doc['reward\_factor'].value, it is alway '0'. Tried other ways to retrieve the value, such as \_fields (as documented [here](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting-fields.html)), no success.

One thing maybe special here is that the function\_score query is under a 'nested' query, in such case, are we not able to retrieve the field value? No documentation seems indicating so.

Much appreciated if someone could help with this.

Junjun

PS: Elasticsearch version: 5.1.1

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [March 27, 2017, 8:15am UTC](https://discuss.elastic.co/t/unable-to-retrieve-field-value-from-doc-values-in-nested-function-score-query/80052/2 "2017-03-27T08:15:04Z")

</div>

hi,

> [@Junjun\_Zhang](#):
>
> One thing maybe special here is that the function\_score query is under a 'nested' query, in such case, are we not able to retrieve the field value? No documentation seems indicating so.

I would say that the function\_score is only applied on the nested documents, so the reward\_factor is not accessible

see if it can helps:

> [@Nested value on function score](https://discuss.elastic.co/t/nested-value-on-function-score/29935/8):
>
> Hello, I have a related question that I couldn't get from the previous discussion. I want to boost by a nested object field but a filtered one. Let's say I have this documents: POST /test/doc/1 { "title": "doc 1", "queries": [{ "hash": "102a", "value": 0.9 }, { "hash": "101a", "value": 0.9 }] } POST /test/doc/2 { "title": "doc 2", "queries": [ { "hash": "102a", "value": 1.0 }, {…

> [@Is it possible getting parent doc's score in nested query?](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/11):
>
> Queries wrapped by the nested query can't access the score of the parent document, because at the time of scoring the parent document hasn't been matched yet and therefor its score hasn't been computed yet.

bye,  
Xavier

---

<div class="post-metadata">

**Author:** ![Junjun\_Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junjun_zhang/32/45613_2.png) [@Junjun\_Zhang](https://discuss.elastic.co/u/Junjun_Zhang)\
**Post date:** [March 28, 2017, 12:53am UTC](https://discuss.elastic.co/t/unable-to-retrieve-field-value-from-doc-values-in-nested-function-score-query/80052/3 "2017-03-28T00:53:56Z")

</div>

Thank you Xavier for your answer. It makes sense although I do wish there is a way to get the field value from parent docs. I guess I have a walk-around that is to include 'reward\_factor' field in each of the nested subdocuments. Not very elegant, but it should work.  
Cheers,  
Junjun

---

<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 25, 2017, 12:54am UTC](https://discuss.elastic.co/t/unable-to-retrieve-field-value-from-doc-values-in-nested-function-score-query/80052/4 "2017-04-25T00:54:07Z")

</div>

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