# Accessing fields of nested doc in custom score script may cause documents missing in query result

**URL:** <https://discuss.elastic.co/t/accessing-fields-of-nested-doc-in-custom-score-script-may-cause-documents-missing-in-query-result/11915>\
**Category:** Elasticsearch\
**Created:** [May 10, 2013, 4:38pm UTC](https://discuss.elastic.co/t/accessing-fields-of-nested-doc-in-custom-score-script-may-cause-documents-missing-in-query-result/11915 "2013-05-10T16:38:16Z")\
**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:** [May 10, 2013, 4:38pm UTC](https://discuss.elastic.co/t/accessing-fields-of-nested-doc-in-custom-score-script-may-cause-documents-missing-in-query-result/11915/1 "2013-05-10T16:38:16Z")

</div>

Hi,

I recently ran into a problem of missing documents in query result when  
custom score script is used. After some testing, I found that the problem  
seems occur when the script tries to access a field in a nested doc where a  
particular root document does not contain any such nested doc.

To reproduce the problem, test data and queries can be found here:  
[http://goo.gl/iHOc5](http://goo.gl/iHOc5). The example may not make much sense in real world, but  
the idea is to sort products by average rate from users' review. One  
particular requirement is to always treat anonymous user's rate as 3 and  
assign rate as 3 for products with no reviews.

We can determine whether a user is anonymous or not by  
checking review.user.member\_id field is empty or not:  
doc['review.user.member\_id'].empty, this seems work fine except that  
products with no reviews are dropped out in the result as the first query  
example shows. Is this a bug? As there is no query/filter that excludes  
documents, shouldn't all documents be returned?

Also, there seems no way to determine whether a review exists or not. The  
second query example shows doc['review'].empty does not work, this makes  
sense because indeed, there is not such field as 'review' under the  
'product' index, 'review' is a nested document. However, the question  
remains: is there a way to determine the existence of a nested doc?

Any help will be greatly appreciated!

Best regards,

Junjun

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![btiernay](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@btiernay](https://discuss.elastic.co/u/btiernay)\
**Post date:** [May 13, 2013, 11:06pm UTC](https://discuss.elastic.co/t/accessing-fields-of-nested-doc-in-custom-score-script-may-cause-documents-missing-in-query-result/11915/2 "2013-05-13T23:06:04Z")

</div>

I wonder if this is a direct result of how nested docs work (  
[Nested and Parent/Child Docs in ElasticSearch | PPT](http://www.slideshare.net/AnneVeling/nested-and-parentchild-docs-in-elasticsearch)).  
Still, it looks as though from an api perspective your queries should be  
honored. I think at the very least this should be documented, but I would  
file an issue with elasticsearch on github regardless.

Not sure if this is related:

> <https://github.com/elastic/elasticsearch/issues/3020>
>
> Reuse the missing support for fields (number based fields) inside nested object …when \`nested\_filter\` doesn't match a inner object.

Cheers,

Bob

On Friday, 10 May 2013 12:38:16 UTC-4, Junjun Zhang wrote:

> Hi,
> 
> I recently ran into a problem of missing documents in query result when  
> custom score script is used. After some testing, I found that the problem  
> seems occur when the script tries to access a field in a nested doc where a  
> particular root document does not contain any such nested doc.
> 
> To reproduce the problem, test data and queries can be found here:  
> [http://goo.gl/iHOc5](http://goo.gl/iHOc5). The example may not make much sense in real world,  
> but the idea is to sort products by average rate from users' review. One  
> particular requirement is to always treat anonymous user's rate as 3 and  
> assign rate as 3 for products with no reviews.
> 
> We can determine whether a user is anonymous or not by  
> checking review.user.member\_id field is empty or not:  
> doc['review.user.member\_id'].empty, this seems work fine except that  
> products with no reviews are dropped out in the result as the first query  
> example shows. Is this a bug? As there is no query/filter that excludes  
> documents, shouldn't all documents be returned?
> 
> Also, there seems no way to determine whether a review exists or not. The  
> second query example shows doc['review'].empty does not work, this makes  
> sense because indeed, there is not such field as 'review' under the  
> 'product' index, 'review' is a nested document. However, the question  
> remains: is there a way to determine the existence of a nested doc?
> 
> Any help will be greatly appreciated!
> 
> Best regards,
> 
> Junjun

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brunomiranda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brunomiranda/32/746_2.png) [@brunomiranda](https://discuss.elastic.co/u/brunomiranda)\
**Post date:** [December 23, 2015, 7:25am UTC](https://discuss.elastic.co/t/accessing-fields-of-nested-doc-in-custom-score-script-may-cause-documents-missing-in-query-result/11915/3 "2015-12-23T07:25:27Z")

</div>

Did you ever find a resolution to this issue? I am running into something similar.

---

<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 5, 2017, 11:29pm UTC](https://discuss.elastic.co/t/accessing-fields-of-nested-doc-in-custom-score-script-may-cause-documents-missing-in-query-result/11915/4 "2017-07-05T23:29:07Z")

</div>


