Vikas Pandya
Vikas Pandya

Reputation: 1988

multiple metric sub aggregations situation with ElasticSearch

I am aware that Elasticsearch supports sub aggregations with bucketing (where bucketing aggregation can have bucketing or metric sub aggregations). Sub aggregation isn't possible with metric aggregations. May be that makes sense but here is the use case.

I have term aggregation as a parent. And using another term aggregation as a child of it. child term has a child aggregation of type top_hits. top_hits is a metric aggregation so it can't take any child aggregation. And now need to include avg aggregation into the mix. Given top_hits is the last aggregation in the aggregation tree can't have avg as a child to it since top_hits is a metric aggregation.

following is the desired aggregation levels. (of course it's invalid given top_hits is a metric aggregation and true for avg aggregation too.

{
  "aggregations": {
    "top_makes": {
      "terms": {
        "field": "make"
      },
      "aggregations": {
        "top_models": {
          "terms": {
            "field": "model"
          },
          "aggregations": {
            "top_res": {
              "top_hits": {
                "_source": {
                  "include": [
                    "model",
                    "color"
                  ]
                },
                "size": 10
              }
            }
          }
        }
      },
      "aggregations": {
        "avg_length": {
          "avg": {
            "field": "vlength"
          }
        }
      }
    }
  }
}

What's the workaround or best way to address this?

Upvotes: 2

Views: 3234

Answers (1)

progrrammer
progrrammer

Reputation: 4489

I think this will work , verify ..

{
  "aggregations": {
    "top_makes": {
      "terms": {
        "field": "make"
      },
      "aggregations": {
        "top_models": {
          "terms": {
            "field": "model"
          },
          "aggregations": {
            "top_res": {
              "top_hits": {
                "_source": {
                  "include": [
                    "model",
                    "color"
                  ]
                },
                "size": 10
              }
            }
          },
          "avg_length": {
            "avg": {
              "field": "vlength"
            }
          }
        }
      }
    }
  }
}

The point is you can have 1 or more sibblings (sub aggregation) for a parent aggregation.

Upvotes: 2

Related Questions