Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find parent documents based on child doc value

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"
 }
like image 907
kannanrbk Avatar asked Jan 29 '18 19:01

kannanrbk


1 Answers

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"
  }
}
like image 186
Jon Cluff Avatar answered Oct 21 '22 12:10

Jon Cluff