# Generating Correct Nested Filter Aggregation

**URL:** <https://discuss.elastic.co/t/generating-correct-nested-filter-aggregation/94021>\
**Category:** Elasticsearch\
**Created:** [July 20, 2017, 9:38pm UTC](https://discuss.elastic.co/t/generating-correct-nested-filter-aggregation/94021 "2017-07-20T21:38:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![panda2004](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/panda2004/32/19473_2.png) [@panda2004](https://discuss.elastic.co/u/panda2004)\
**Post date:** [July 20, 2017, 9:38pm UTC](https://discuss.elastic.co/t/generating-correct-nested-filter-aggregation/94021/1 "2017-07-20T21:38:57Z")

</div>

Hello,

I have a technical question about using nested data type, as expressed in the Query DSL.

Unfortunately, only today I discovered that i'm not building my nested queries as I should be.  
I was generating the following **wrong** query:

```
GET my_index / blogpost / _search {
	"query" : {
		"bool" : {
			"must" : [{
					"query" : {
						"nested" : {
							"path" : "person",
							"query" : {
								"term" : {
									"person.name" : "george"
								}
							}
						}
					}
				}, {
					"query" : {
						"nested" : {
							"path" : "person",
							"query" : {
								"term" : {
									"person.itemsBought" : {
										"gt" : 5
									}
								}
							}
						}
					}
				}
			]
		}
	}
}

```

instead of producing the following **correct** query:

```
GET my_index / blogpost / _search {
	"query" : {
		"nested" : {
			"path" : "person",
			"query" : {
				"bool" : {
					"must" : [{
							"term" : {
								"person.name" : "george"
							}
						}, {
							"range" : {
								"person.itemsBought" : {
									"gt" : 5
								}
							}
						}
					]
				}
			}
		}
	}
}

```

Obviously, the second option is suitable, right? Since it guarantees that the filters would be made on the **SAME** nested document rather than 2 different nested documents.

My real question is about building the correct equivalent nested aggregation query.  
Is there a different between the following aggregations below?

**Aggregation 1:**

```
GET my_index / blogpost / _search {
	"aggs" : {
		"nested" : {
			"path" : "person"
		},
		"aggs" : {
			"filterByName" : {
				"filter" : {
					"term" : {
						"person.name" : "george"
					}
				},
				"aggs" : {
					"filterByItemsCount" : {
						"filter" : {
							"range" : {
								"person.itemsBought" : {
									"gt" : 5
								}
							}
						}
					}
				}
			}
		}
	}
}

```

**Aggregation 2:**

```
GET my_index / blogpost / _search {
	"aggs" : {
		"nested" : {
			"path" : "person"
		},
		"aggs" : {
			"filterPerson" : {
				"filter" : {
					"bool" : {
						"must" : [{
								"term" : {
									"person.name" : "george"
								}
							}, {
								"range" : {
									"person.itemsBought" : {
										"gt" : 5
									}
								}
							}
						]
					}
				}
			}
		}
	}
}

```

I believe aggregation 1 might be less optimized, but the results from both Aggregation 1 and Aggregation 2 should be same, right? After all, I believe there is an AND relationship between the 2 filter aggregations in Aggregation 1.

By the way, the full aggregation query involves reverse\_nested aggregations and terms aggregation, but yet again - I don't believe it matters involving them into Aggregation 1 nor Aggregation 2 when they are all sub aggregations of each other (and therefore AND related).

---

<div class="post-metadata">

**Author:** ![panda2004](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/panda2004/32/19473_2.png) [@panda2004](https://discuss.elastic.co/u/panda2004)\
**Post date:** [July 22, 2017, 5:51pm UTC](https://discuss.elastic.co/t/generating-correct-nested-filter-aggregation/94021/2 "2017-07-22T17:51:00Z")

</div>

Anyone?

---

<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:** [August 19, 2017, 5:51pm UTC](https://discuss.elastic.co/t/generating-correct-nested-filter-aggregation/94021/3 "2017-08-19T17:51:07Z")

</div>

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