Nested Filter Aggregation

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!

1 Like

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
              }
            }
          }
        }
      }
    }
  }
}
1 Like

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