Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

Elasticsearch, composite and sub(?) aggregations

I'm using composite to scroll through whole data. (it's like pagination)

Suppose a car selling data,

For each day, I'd like to count the number of cars sold per car-brand

{
  day1: {
    honda: 3,
    bmw: 5
  },
  day2: {
    honda: 4,
    audi: 1,
    tesla:5
  }
}

I'm doing something like the following but it doesn't work

GET _search
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "date": {
              "date_histogram": {
                "field": "created_at",
                "calendar_interval": "1d"
              },
              "aggs": {
                "car_brand": {
                  "terms": {
                    "field": "car_brands"
                  }
                }
              }
            }
          }
        ]
      }
    }
  }
}

with error message

{
  "error" : {
    "root_cause" : [
      {
        "type" : "x_content_parse_exception",
        "reason" : "[14:17] [composite] failed to parse field [sources]"
      }
    ],
    "type" : "x_content_parse_exception",
    "reason" : "[14:17] [composite] failed to parse field [sources]",
    "caused_by" : {
      "type" : "illegal_state_exception",
      "reason" : "expected value but got [FIELD_NAME]"
    }
  },
  "status" : 400
}
question from:https://stackoverflow.com/questions/65918587/elasticsearch-composite-and-sub-aggregations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Composite aggs cannot directly accept sub-aggs. Go with

GET _search
{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "date": {
              "date_histogram": {
                "field": "created_at",
                "calendar_interval": "1d"
              }
            }
          },
          {
            "car_brand": {
              "terms": {
                "field": "car_brands"
              }
            }
          }
        ]
      }
    }
  }
}

instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...