Nested Query doesn't yield expected result

I'm trying to understand some inconsistencies in search results. I'm attempting give all the necessary details. This post looks long (my apologies) due to JSON content. First the mapping:

{  
   "_all":{  
      "enabled":false
   },
   "properties":{  
      "edgeCount":{  
         "type":"long"
      },
      "nodeCount":{  
         "type":"long"
      },
      "degree":{  
         "type":"nested",
         "properties":{  
            "size":{  
               "type":"long"
            },
            "count":{  
               "type":"long"
            }
         }
      },
      "sequence":{  
         "type":"nested",
         "properties":{  
            "size":{  
               "type":"long"
            },
            "count":{  
               "type":"long"
            }
         }
      },
      "cycle":{  
         "type":"nested",
         "properties":{  
            "size":{  
               "type":"long"
            },
            "count":{  
               "type":"long"
            }
         }
      }
   }
}

The document

{  
   "nodeCount":3,
   "edgeCount":2,
   "degree":[  
      {  
         "size":1,
         "count":2
      },
      {  
         "size":2,
         "count":1
      }
   ],
   "sequence":[  
      {  
         "size":3,
         "count":1
      }
   ]
}

The query below fails to match the above document. What's wrong here? If I remove one of the conditions in the nested degree conditions ({"term":{"degree.size":2}},{"term":{"degree.count":1}) it works.

{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "term":{  
                  "edgeCount":2
               }
            },
            {  
               "term":{  
                  "nodeCount":3
               }
            },
            {  
               "nested":{  
                  "path":"sequence",
                  "query":{  
                     "bool":{  
                        "must":[  
                           {  
                              "term":{  
                                 "sequence.size":3
                              }
                           },
                           {  
                              "term":{  
                                 "sequence.count":1
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {  
               "nested":{  
                  "path":"degree",
                  "query":{  
                     "bool":{  
                        "must":[  
                           {  
                              "term":{  
                                 "degree.size":1
                              }
                           },
                           {  
                              "term":{  
                                 "degree.count":2
                              }
                           },
                           {  
                              "term":{  
                                 "degree.size":2
                              }
                           },
                           {  
                              "term":{  
                                 "degree.count":1
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

The query actually works for document with edgeCount 3 and nodeCount 4.

Any insight? Thanks in advance.

I think that the problem is that your nested document do not satisfy the nested bool query :

In the must having on "nested":{ "path":"degree", "query":{ ...
all your conditions are in the same "must" array and none of your nested degree have: size = 1 AND count = 2 AND size = 2 AND count = 1

You should have a bool query should (minimunshouldmatch 1) with a should (size = 1 AND count = 2) and another should (size = 2 AND count = 1 )

So that the nested query will be : (size = 1 AND count = 2) OR (size = 2 AND count = 1)

Hope this help

xavierfacq

I appreciate your input.

Degree is a nested document, in this example multiple degrees (instances of degrees) are related to the parent document. When the condition "range":{"degree.size":{ "gte":2}} is met I'm assuming the search context narrows down to that nested document (1) and now it can only see the count: 2. The document (2) is not in its context.

"degree": [
  { <!-- Nested document 1-->
	 "size": 1,
	 "count": 2
  },
  { <!-- Nested document 2-->
	 "size": 2,
	 "count": 1
  }
]

When I corrected the nested condition as below it worked as expected.

{  
   "nested":{  
	  "path":"degree",
	  "query":{  
		 "bool":{  
			"must":[  
			   {  
				  "range":{  
					 "degree.size":{  
						"gte":1
					 }
				  }
			   },
			   {  
				  "range":{  
					 "degree.count":{  
						"gte":2
					 }
				  }
			   }
			]
		 }
	  }
   }
},
{  
   "nested":{  
	  "path":"degree",
	  "query":{  
		 "bool":{  
			"must":[  
			   {  
				  "range":{  
					 "degree.size":{  
						"gte":2
					 }
				  }
			   },
			   {  
				  "range":{  
					 "degree.count":{  
						"gte":1
					 }
				  }
			   }
			]
		 }
	  }
   }
}

Thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.