# Elastic aggregation for timeline of a searched field based on timestamp

**URL:** <https://discuss.elastic.co/t/elastic-aggregation-for-timeline-of-a-searched-field-based-on-timestamp/231369>\
**Category:** Elasticsearch\
**Created:** [May 6, 2020, 3:09pm UTC](https://discuss.elastic.co/t/elastic-aggregation-for-timeline-of-a-searched-field-based-on-timestamp/231369 "2020-05-06T15:09:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pankesh\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pankesh_kumar/32/67810_2.png) [@Pankesh\_Kumar](https://discuss.elastic.co/u/Pankesh_Kumar)\
**Post date:** [May 6, 2020, 3:09pm UTC](https://discuss.elastic.co/t/elastic-aggregation-for-timeline-of-a-searched-field-based-on-timestamp/231369/1 "2020-05-06T15:09:17Z")

</div>

We are using elastic version: 6.8

I need help with a sample query that can solve below use case.

Sample data:

We have books index, that can travel in which zone a book was and when.

```auto
{'zone': 'z1', book: 'book1', timestamp: '01/01/2020'}
{'zone': 'z1', book: 'book1', timestamp: '03/01/2020'}
{'zone': 'z2', book: 'book1', timestamp: '04/01/2020'}
{'zone': 'z2', book: 'book1', timestamp: '05/01/2020'}
{'zone': 'z2', book: 'book1', timestamp: '10/01/2020'}
{'zone': 'z2', book: 'book1', timestamp: '12/01/2020'}
{'zone': 'z1', book: 'book1', timestamp: '15/01/2020'}
{'zone': 'z1', book: 'book1', timestamp: '16/01/2020'}
{'zone': 'z1', book: 'book1', timestamp: '20/01/2020'}

```

I want a query that can return the book's complete journey accross zones, while a book does back and forth in zones.

Expected Output if we query for book1:

```auto
[ 
    {
        zone: 'z1'
        start: '01/01/2020'
        end: '04/01/2020'
    },
    {
        zone: 'z2'
        start: '05/03/2020'
        end: '12/01/2020'
    },
    {
        zone: 'z1'
        start: '15/03/2020'
        end: '20/01/2020'
    }
]

```

---

<div class="post-metadata">

**Author:** ![Pankesh\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pankesh_kumar/32/67810_2.png) [@Pankesh\_Kumar](https://discuss.elastic.co/u/Pankesh_Kumar)\
**Post date:** [May 21, 2020, 3:48pm UTC](https://discuss.elastic.co/t/elastic-aggregation-for-timeline-of-a-searched-field-based-on-timestamp/231369/2 "2020-05-21T15:48:20Z")

</div>

This is what I came up with so far.

```
GET /book/_search
{
  "query": {
      "bool": {
          "must": [
              {
                  "match": {
                      "book": "book1"
                  }
              }
          ]
      }
  },
  "size": 0,
  "aggs": {
        "zoneList": {
          "terms": {
            "field": "zone",
            "size": 1000
        },
        "aggs": {
          "first_seen": {
           "top_hits": {
              "size": 1,
              "sort": [
                 {
                    "timestamp": {
                       "order": "asc"
                    }
                 }
              ],
              "_source": {
                  "includes": ["timestamp"]
                }
              }
          },
          "last_seen": {
             "top_hits": {
                "size": 1,
                "sort": [
                   {
                      "timestamp": {
                         "order": "desc"
                      }
                   }
                ],
                "_source": {
                  "includes": ["timestamp"]
                }
             }
          }  
        }
      }
  }
}
```

---

<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:** [June 18, 2020, 3:48pm UTC](https://discuss.elastic.co/t/elastic-aggregation-for-timeline-of-a-searched-field-based-on-timestamp/231369/3 "2020-06-18T15:48:22Z")

</div>

Closed on June 18, 2020, 3:48pm UTC

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