Calculate duration using timestamps

I have a device that can exist in multiple states (more than 2). Lets say it can exist in state1, state2, state3 and state4

I have a document that stores the timestamp of each state

   state1: time
   state3: time + 10
   state2: time + 20
   state4: time + 40

From the above document the duration of state1 is state3 - state1

Duration of state3 is state2 - state3

I want to calculate the aggregate duration of each state from multiple such documents. But the order in which the timestamps occur may not be the same.
I basically want to calculate the duration of one state using the timestamp of the next state.

How do I do such aggregation in elasticsearch?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.