ElasticSearch 根据 restaurant_id 显示数据及其平均时间

ElasticSearch show data according to restaurant_id and its average time

I am very new to ElasticSearch and I wonder if it is possible to show the data according to restaurant_id and then the average time to confirm the order. for example, I have documents:

{ _index: "orders-development",
_type: "order",
_id: "4412917",
_score: 1,
_source: {confirm_duration: 5}
}

{ _index: "orders-development",
_type: "order",
_id: "4412917",
_score: 1,
_source: {confirm_duration: 4}
}

{ _index: "orders-development",
_type: "order",
_id: "4322923",
_score: 1,
_source: { confirm_duration: 12}
}

以上是存储在ES中的文件,现在要按restaurant_id显示数据,然后是平均时间。所以现在我使用下面的代码及其显示错误 "reason":"[terms] unknown field [avg], parser not found"

aggregations = {
  avg_order_confirmation: {
    terms: {
      field: :restaurant_id,
      size: 100,
      avg: { field: :confirm_duration },
    }
  }
}

就快完成了,您需要将 avg 移动到 terms 的子聚合:

aggregations = {
  avg_order_confirmation: {
    terms: {
      field: :restaurant_id,
      size: 100
    },
    aggs: {
      avg_confirmation: {
        avg: { field: :confirm_duration }
      }
    }
  }
}