# Nested Query doesn't yield expected result

**URL:** <https://discuss.elastic.co/t/nested-query-doesnt-yield-expected-result/97489>\
**Category:** Elasticsearch\
**Created:** [August 17, 2017, 9:12pm UTC](https://discuss.elastic.co/t/nested-query-doesnt-yield-expected-result/97489 "2017-08-17T21:12:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [August 17, 2017, 9:12pm UTC](https://discuss.elastic.co/t/nested-query-doesnt-yield-expected-result/97489/1 "2017-08-17T21:12:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [August 18, 2017, 8:11am UTC](https://discuss.elastic.co/t/nested-query-doesnt-yield-expected-result/97489/2 "2017-08-18T08:11:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [August 18, 2017, 3:56pm UTC](https://discuss.elastic.co/t/nested-query-doesnt-yield-expected-result/97489/3 "2017-08-18T15:56:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 15, 2017, 3:57pm UTC](https://discuss.elastic.co/t/nested-query-doesnt-yield-expected-result/97489/4 "2017-09-15T15:57:04Z")

</div>

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