Ravindra Yadav
Ravindra Yadav

Reputation: 35

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}
}

The above are the documents stored in ES and now I have to show data according to restaurant_id and then the average of time. So now I am using below code and its showing error "reason":"[terms] unknown field [avg], parser not found"

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

Upvotes: 0

Views: 25

Answers (1)

Val
Val

Reputation: 217514

You're almost there, you need to move avg to a sub-aggregation of the terms one:

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

Upvotes: 2

Related Questions