Sunday, February 11, 2018

Find parent documents based on child doc value

Leave a Comment

We are using haschild query to find the parent documents based on the condition.

We have two types

  1. funnels
  2. pages

funnels sample doc

 {    "funnel_id": "12345",    "path": "a -> b -> c"  }   {    "funnel_id": "56789",    "path": "a -> d"  } 

** pages sample doc**

{   "_parent": "12345",   "visited_page": "/home" }  {   "_parent": "12345",   "visited_page": "/cart" }  {   "_parent": "12345",   "visited_page": "/cart" } 

Condition1:

Find parent doc based child doc "visited_page" value contains "home".

"must" : {   "has_child" : {     "query" : {       "regexp" : {         "url" : {           "value" : ".*home.*",           "flags_value" : 65535         }       }     },     "child_type" : "session_pages"   } } 

It works perfectly.

Condition2

Find parent doc based child doc "visited_page" value does NOT contains "home".

"must_not" : {   "has_child" : {     "query" : {       "regexp" : {         "url" : {           "value" : ".*home.*",           "flags_value" : 65535         }       }     },     "child_type" : "session_pages"   } } 

But this query returned wrong results.

Output of the query

  {   "funnel_id": "12345",   "path": "a -> b -> c"  }   {    "funnel_id": "56789",    "path": "a -> d"  } 

You can see the parent id(funnel_id:12345) child doc contains visited page with value "home". But that also returns.

Expected Result

  {    "funnel_id": "56789",    "path": "a -> d"  } 

1 Answers

Answers 1

I believe you are "must_not"ing in the wrong spot try:

    "must" : {   "has_child" : {     "query" : {       "regexp" : {         "url" : {           "must_not": {                "value" : ".*home.*"                        },           "flags_value" : 65535         }       }     },     "child_type" : "session_pages"   } } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment