Custom_score in has_child


(Luiz Carlos Jr) #1

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.
To view this discussion on the web visit 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.


(Binh Ly) #2

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.
To view this discussion on the web visit 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.


(system) #3