# Nested Filter Aggregation

**URL:** <https://discuss.elastic.co/t/nested-filter-aggregation/82639>\
**Category:** Elasticsearch\
**Created:** [April 17, 2017, 11:36pm UTC](https://discuss.elastic.co/t/nested-filter-aggregation/82639 "2017-04-17T23:36:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arsi](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@arsi](https://discuss.elastic.co/u/arsi)\
**Post date:** [April 17, 2017, 11:36pm UTC](https://discuss.elastic.co/t/nested-filter-aggregation/82639/1 "2017-04-17T23:36:20Z")

</div>

Hi,

Let's say I have an index with multiple documents in it. Documents could have a field called "points" that is a list of objects. For example, say I have 2 documents in my\_index

my\_index/type/doc1: "points": [{"x": 3, "y":2}, {"x": -4, "y": 17}, {"x": 9, "y": 8}].  
my\_index/type/doc2: "points": [{"x": 10, "y":8}, {"x": 7, "y": 4}, {"x": -12, "y": 17}]

I would like to preserve the relation between x and y in each object so I use a nested type. What I want to do is be able to do is a 2-dimensional histogram: so given xInterval and a yInterval find the count of all points in a buckets of size xInterval by yInterval.

There is no 2 dimensional histogram supported by ES as far as I know, so I wanted to do a filter on the points, and then do an aggregation (So I could filter all the points where points.x is in range [x1, x2) and points.y is in range [y1, y2] and then give me the count. Or I could get all points where points.x is in range [x1, x2) and then do a one dimensional histogram aggregation on points.y with interval yInterval).

So long story short: is there a way to do a filter on a nested type, and then do aggregation on those nested documents? Right now, I get the whole document and not just the nested documents which throws off my aggregation.

Thanks!

---

<div class="post-metadata">

**Author:** ![krmathieu](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@krmathieu](https://discuss.elastic.co/u/krmathieu)\
**Post date:** [April 18, 2017, 8:18pm UTC](https://discuss.elastic.co/t/nested-filter-aggregation/82639/2 "2017-04-18T20:18:19Z")

</div>

Here is a sample that may help get you started. I use this query to filter my nested objects and then facet on those results. You would need to change the path attribute to point to your nested objects, use field names relevant to your index and update the conditions in the must array. You might also consider tacking on inner\_objects to show exactly why each result hit. This is probably not exactly what you need, but it may help get you started.

Kevin

```
{
  "size": 0,
  "aggs": {
    "my_aggs_filter": {
      "nested": {
        "path": "segments"
      },
      "aggs": {
        "filter_types": {
          "filter": {
            "bool": {
              "must": [ 
              {"exists": {"field": "segments.entities.disambiguationResults"}},
              {"missing": {"field": "segments.entities.disambiguationResults.id"}},
              {"regexp": {"segments.entities.type": ".*(PERSON|ORG.*|GEO).*"}}
              ]
            }
          },
          "aggs": {
            "anchor_text": {
              "terms": {
                "field": "segments.anchor.text",
                "size": 200
              }
            }
          }
        }
      }
    }
  }
}
```

---

<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:** [May 16, 2017, 8:27pm UTC](https://discuss.elastic.co/t/nested-filter-aggregation/82639/3 "2017-05-16T20:27:14Z")

</div>

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