How to Filter nested aggregations (busket results)

Hi all,

I'm trying to filter an aggregations.
I created aggregation for nested fields(pipe of id|value) of Badge field. It works good and return me buskets with pipe and doc_count. But I want to filter them to return me only ones that contain word "future". Is it possible?

Index (Badge is nested, value is keyword):
{"id":1, "Badge":[{"id": 5, "value":"analysis of economics"}, {"id": 4, "value":"future of science"}]}
{"id":2, "Badge":[{"id": 3, "value":"future of investments"}, {"id": 2, "value":"solar energy"}]}
{"id":3, "Badge":[{"id": 3, "value":"future of investments"}, {"id": 4, "value":"future of science"}]}

Query:
{
"size": 0,
"aggs": {
"Badge_agg" : {
"nested": {"path": "Badge"},
"aggs": {
"Badge_count": {
"terms": {
"script": { "lang": "painless", "source": "doc['Badge.id'].value + '|' + doc['Badge.value'].value" }
}
}
}
}
}
}

Result (baskets)

"buckets": [
{
"key": "3|future of investments",
"doc_count": 2
},
{
"key": "4|future of science",
"doc_count": 2
},
{
"key": "2|solar energy",
"doc_count": 1
},
{
"key": "5|analysis of economics",
"doc_count": 1
}
]

Desirable result:
"buckets": [
{
"key": "3|future of investments",
"doc_count": 2
},
{
"key": "4|future of science",
"doc_count": 2
}
]

Many thanks

Hey,

please take your time to properly format the snippets, this will make it much more readable.

How about this?

PUT test
{
  "mappings": {
    "properties": {
      "Badge": { "type": "nested"}
    }
  }
}

PUT test/_doc/1
{"id":1, "Badge":[{"id": 5, "value":"analysis of economics"}, {"id": 4, "value":"future of science"}]}
PUT test/_doc/2
{"id":2, "Badge":[{"id": 3, "value":"future of investments"}, {"id": 2, "value":"solar energy"}]}
PUT test/_doc/3
{"id":3, "Badge":[{"id": 3, "value":"future of investments"}, {"id": 4, "value":"future of science"}]}

GET test/_search
{
  "size": 0,
  "aggs": {
    "science_only": {
      "nested": {
        "path": "Badge"
      },
      "aggs": {
        "by_id": {
          "terms": {
            "field": "Badge.id",
            "size": 10
          }
        }
      }
    }
  }
}

GET test/_search
{
  "size": 0,
  "aggs": {
    "science_only": {
      "nested": {
        "path": "Badge"
      },
      "aggs": {
        "by_future": {
          "filter": {
            "match": {
              "Badge.value": "future"
            }
          },
          "aggs": {
            "by_id": {
              "terms": {
                "field": "Badge.id",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

--Alex

1 Like

Thank you so much!

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