# Custom\_score in has\_child

**URL:** <https://discuss.elastic.co/t/custom-score-in-has-child/15762>\
**Category:** Elasticsearch\
**Created:** [February 12, 2014, 5:40pm UTC](https://discuss.elastic.co/t/custom-score-in-has-child/15762 "2014-02-12T17:40:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luiz\_Carlos\_Jr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz_carlos_jr/32/1812_2.png) [@Luiz\_Carlos\_Jr](https://discuss.elastic.co/u/Luiz_Carlos_Jr)\
**Post date:** [February 12, 2014, 5:40pm UTC](https://discuss.elastic.co/t/custom-score-in-has-child/15762/1 "2014-02-12T17:40:38Z")

</div>

Hi all, I have the following parent/child/grandchild documents on my ES:

DOC. TYPE: /users/user (parent is none)

"\_source": {  
"prefix": "TST\_LUIZ",  
"id": "c0338fde-981c-478a-bcbe-2548ab967dce",  
"cookieids": [  
"b6d3c8b4-a075-49a3-9493-f1bf0f56312e"  
]  
}

DOC. TYPE: /users/browser\_TST\_LUIZ (parent is /users/user)

"\_source": {  
"TST\_LUIZ\_browser\_user\_agent\_dictc": [  
"Chrome"  
],  
"TST\_LUIZ\_browser\_user\_agent": "Mozilla/5.0 (X11; Linux x86\_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36",  
"id": "Yzk4ZTVmY2QyZjZhNWYxZjI0YTI5MGZkN2E1ZjdlNTJlYmIyYWFhNTg3ODM2MTA3ZTEyNzIxNDgwYmEwOTVlZQ==",  
"TST\_LUIZ\_browser\_user\_agent\_dictv": 48  
}

DOC. TYPE: /users/browser\_TST\_LUIZ\_fr (parent is /users/browser\_TST\_LUIZ and grandparent is /users/user)

"\_source": {  
"date": 20140204,  
"id": "Yzk4ZTVmY2QyZjZhNWYxZjI0YTI5MGZkN2E1ZjdlNTJlYmIyYWFhNTg3ODM2MTA3ZTEyNzIxNDgwYmEwOTVlZQ==20140204",  
"name": "Yzk4ZTVmY2QyZjZhNWYxZjI0YTI5MGZkN2E1ZjdlNTJlYmIyYWFhNTg3ODM2MTA3ZTEyNzIxNDgwYmEwOTVlZQ==",  
"date\_list": [  
20140204122423000,  
20140204122500000,  
20140204122501000,  
20140204122510000,  
20140204122450000  
],  
"frequency": 5  
}

"\_source": {  
"date": 20140205,  
"id": "Yzk4ZTVmY2QyZjZhNWYxZjI0YTI5MGZkN2E1ZjdlNTJlYmIyYWFhNTg3ODM2MTA3ZTEyNzIxNDgwYmEwOTVlZQ==20140204",  
"name": "Yzk4ZTVmY2QyZjZhNWYxZjI0YTI5MGZkN2E1ZjdlNTJlYmIyYWFhNTg3ODM2MTA3ZTEyNzIxNDgwYmEwOTVlZQ==",  
"date\_list": [  
20140205122423000,  
20140205122500000,  
20140205122501000  
],  
"frequency": 3  
}

I need to find all users that has a determinate value on TST\_LUIZ\_browser\_user\_agent field, but with an additional check: the document in browser\_TST\_LUIZ must have childrens on browser\_TST\_LUIZ\_fr where the date in browser\_TST\_LUIZ\_fr is in a range AND the SUM of frequency is greater than X.

The only step remaining is "AND the SUM of frequency is greater than X".

I tried to use custom\_score to achieve my goals and did this tests:

## QUERY 1

GET /users/browser\_TST\_LUIZ\_fr/\_search  
{  
"min\_score": 6,  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"boost": "1",  
"script": "doc['frequency'].value"  
}  
}  
}

## QUERY 2

GET /users/browser\_TST\_LUIZ/\_search  
{  
"min\_score": 6,  
"query": {  
"has\_child": {  
"type": "browser\_TST\_LUIZ\_fr",  
"score\_type": "sum",  
"boost": 1,  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"boost": "1",  
"script": "doc.\_source['frequency']"  
}  
}  
}  
}  
}

QUERY1 runs fine. QUERY2 raises this exception:

{  
"took": 6,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 4,  
"failed": 1,  
"failures": [  
{  
"index": "users",  
"shard": 0,  
"status": 500,  
"reason": "QueryPhaseExecutionException[[users][0]: query[filtered(ChildrenQuery[browser\_TST\_LUIZ\_fr/browser\_TST\_LUIZ](function score (ConstantScore(_:_),function=script[doc.\_source['frequency']], params [null])))-\>cache(\_type:browser\_TST\_LUIZ)],from[0],size[10]: Query Failed [failed to execute context rewrite]]; nested: PropertyAccessException[[Error: null pointer: doc.\_source['frequency']]\n[Near : {... doc.\_source['frequency'] ....}]\n ^\n[Line: 1, Column: 1]]; nested: NullPointerException; "  
}  
]  
},  
"hits": {  
"total": 0,  
"max\_score": null,  
"hits": []  
}  
}

Finally, my question is: is there a way to do this query ? Is possible to use custom\_score inside a has\_child query ?

Any tip can be a greatly help. Thanks!  
Best regards,

Luiz Carvalho

--  
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/cfc6d42f-0211-4cef-889f-58d56d14d804%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cfc6d42f-0211-4cef-889f-58d56d14d804%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 12, 2014, 8:01pm UTC](https://discuss.elastic.co/t/custom-score-in-has-child/15762/2 "2014-02-12T20:01:13Z")

</div>

Maybe try something like this (assuming frequency is available for all  
children)?

GET /users/browser\_TST\_LUIZ/\_search  
{  
"min\_score": 6,  
"query": {  
"has\_child": {  
"type": "browser\_TST\_LUIZ\_fr",  
"score\_type": "sum",  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "doc['browser\_TST\_LUIZ\_fr.frequency'].value"  
}  
}  
}  
}  
}

--  
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/6f5b76d7-d64a-4635-a7df-bacc30108d85%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6f5b76d7-d64a-4635-a7df-bacc30108d85%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:50am UTC](https://discuss.elastic.co/t/custom-score-in-has-child/15762/3 "2017-07-06T01:50:41Z")

</div>


