# Calculating Chronological Contiguous Document Sequences

**URL:** <https://discuss.elastic.co/t/calculating-chronological-contiguous-document-sequences/324240>\
**Category:** Elasticsearch\
**Created:** [January 30, 2023, 2:57pm UTC](https://discuss.elastic.co/t/calculating-chronological-contiguous-document-sequences/324240 "2023-01-30T14:57:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Segev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guy_segev/32/116537_2.png) [@Guy\_Segev](https://discuss.elastic.co/u/Guy_Segev)\
**Post date:** [January 30, 2023, 2:57pm UTC](https://discuss.elastic.co/t/calculating-chronological-contiguous-document-sequences/324240/1 "2023-01-30T14:57:06Z")

</div>

Hi,  
Would appreciate help with the following conundrum:

I have an index with time-series events; here are some document examples:

```auto
{ "timestamp": 1, "status": "running" }
{ "timestamp": 2, "status": "running" }
{ "timestamp": 3, "status": "blocked" }
{ "timestamp": 4, "status": "blocked" }
{ "timestamp": 5 "status": "blocked" }
{ "timestamp": 10, "status": "blocked" }
{ "timestamp": 11, "status": "blocked" }
{ "timestamp": 12, "status": "running" }
{ "timestamp": 13, "status": "running" }

```

I need to find sequences of chronologically contiguous documents **with the same status** , whereby a timestamp gap (no matter it's size) or a different status, closes a bucket and opens a new one in its stead.  
An expected output would look like:

```auto
{
  "buckets": [
    {
      "start": 1,
      "end": 2,
      "status": "running",
      "count": 2
    },
    {
      "start": 3,
      "end": 5,
      "status": "blocked",
      "count": 3
    },
    {
      "start": 1,
      "end": 2,
      "status": "running"
    },
    {
      "start": 10,
      "end": 11,
      "status": "blocked"
    },
    {
      "start": 12,
      "end": 13,
      "status": "running"
    }
  ]
}

```

Any help would be greatly appreciated

---

<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:** [February 27, 2023, 2:57pm UTC](https://discuss.elastic.co/t/calculating-chronological-contiguous-document-sequences/324240/2 "2023-02-27T14:57:20Z")

</div>

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