1

I want to filter the results of the composite aggregation which inside has a top_hits aggregation. So I first group my data with a top_hits, then I use this as a subaggregation inside my composite aggregation that has a single source based on an Id and I don't know how to filter those gruped results.

I've tried using the filters aggregation but I'm not sure since composite aggregation must be the father of all aggregations. Tried different combinations of these aggregation but none of these show me the results as I want.

{
  "size": 0,
  "aggs": {
    "grouped_data": {
      "composite": {
        "sources": [
          {
            "artifact": {
              "terms": {
                "field": "artifactId.keyword"
              }
            }
          }
        ],
        "size": 20
      },
      "aggs": {
        "top_artifacts_hits": {
          "top_hits": {
            "size": 1,
            "sort": [{
              "initialDate": {
                "order": "desc"
              }
            }]
          }
        }
      }
    }
  }
}

I tried using the query API for filtering but that is not a good option for me since the filters I want to apply are meant for the grouped results. Using some query before the main aggregation makes ElasticSearch query first and then group. I need it to be backwards. I'm using ES 6.3 under AWS.

So my documents look something like this:

{
  "artifactId": "foo",
  "clientId": "bar",
  "artifactState": "foozz",
  "initialDate": 1559745246
}

What I need to do is to get the last artifactState based on the initialDate for each different artifactId so this is why I'm using top_hits + composite.

sant016
  • 195
  • 1
  • 2
  • 14
  • Few queries. Why use `composite aggregation` and not `terms aggregation`? For filtering part, if you'd want to filter based on the `artifactId`, what's wrong with `filter aggregation`. Could you add some sample documents and mention what expected documents are to be returned? – Kamal Kunjapur Jun 04 '19 at 21:51
  • @Kamal now edited. – sant016 Jun 05 '19 at 14:36

0 Answers0