# Group by consecutive values in elastic search

**URL:** <https://discuss.elastic.co/t/group-by-consecutive-values-in-elastic-search/128081>\
**Category:** Elasticsearch\
**Created:** [April 15, 2018, 3:51pm UTC](https://discuss.elastic.co/t/group-by-consecutive-values-in-elastic-search/128081 "2018-04-15T15:51:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajjangir](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@rajjangir](https://discuss.elastic.co/u/rajjangir)\
**Post date:** [April 15, 2018, 3:51pm UTC](https://discuss.elastic.co/t/group-by-consecutive-values-in-elastic-search/128081/1 "2018-04-15T15:51:18Z")

</div>

I have document structure like below 👎  
{  
"id" : 1,  
"imei" : "1002786191",  
"date" : 2018-04-15",  
"totalData" : [  
{  
"latitude" : 1.89754,  
"longitude" : 2.89765,  
"speed" : 0,  
"gpsdt" : "2018-04-15T09:18:23"  
},  
{  
"latitude" : 3.89754,  
"longitude" : 2.89765,  
"speed" : 0,  
"gpsdt" : "2018-04-15T09:18:23"  
}

```
                    ],

```

}

Now I have to find vehicle stopage time, like for how much time vehicle took a stopage.  
So for this I need to perform a group by operation and I'll group lat and long and will apply a where clause for speed ..because if vehicle was stopped then speed must be 0.

But now let me explain my problem : If a vehicle is stopped for 1 hour at point A whose lat longs are (1.2345 and 5.7890) and then it starts moving and for 3 hours it continously moving and it comes back at same point A(1.2345,5.7890) and it is stopped for 2 hours because starting and ending point is same so lat long also would be same.  
So to find stopage time for these two stopage , If I do group by lat and long then for both stopage the value of lat long fields would be same. But I have to consider them 2 stopage , not only one. because after first stopage vehicle has moved to some distance so I'll consider them two separate stopage.  
So please suggest me a method/approach to write a query in elasticSearch.  
I tried too much but did not find any solution.

Thanks in advance.  
!wq

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 16, 2018, 8:38pm UTC](https://discuss.elastic.co/t/group-by-consecutive-values-in-elastic-search/128081/2 "2018-04-16T20:38:37Z")

</div>

Since everything seem to be stored in a single document I would suggest doing this calculations in your application before you store the document in elasticsearch.

---

<div class="post-metadata">

**Author:** ![rajjangir](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@rajjangir](https://discuss.elastic.co/u/rajjangir)\
**Post date:** [April 21, 2018, 9:08am UTC](https://discuss.elastic.co/t/group-by-consecutive-values-in-elastic-search/128081/3 "2018-04-21T09:08:38Z")

</div>

Is it not possible by using any method/approach?  
Because I did not find any way to calculate stoppage time for a vehicle.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 19, 2018, 9:08am UTC](https://discuss.elastic.co/t/group-by-consecutive-values-in-elastic-search/128081/4 "2018-05-19T09:08:51Z")

</div>

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