kannanrbk
kannanrbk

Reputation: 7144

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"
 }

Upvotes: 8

Views: 222

Answers (1)

Jon Cluff
Jon Cluff

Reputation: 109

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"
  }
}

Upvotes: 1

Related Questions