# TSVB: Create time series graph which based on latest document in each bucket

**URL:** https://discuss.elastic.co/t/tsvb-create-time-series-graph-which-based-on-latest-document-in-each-bucket/235640
**Category:** Kibana
**Created:** [June 3, 2020, 9:54pm UTC](https://discuss.elastic.co/t/tsvb-create-time-series-graph-which-based-on-latest-document-in-each-bucket/235640 "2020-06-03T21:54:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ofirsa](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@ofirsa](https://discuss.elastic.co/u/ofirsa)
#### Post date: [June 3, 2020, 9:54pm UTC](https://discuss.elastic.co/t/tsvb-create-time-series-graph-which-based-on-latest-document-in-each-bucket/235640/1 "2020-06-03T21:54:05Z")

</div>

Hi All.  
I need your help, I just started working with Elasticsearch and Kibana.  
I have an Index with the following mapping:

```auto
{
  "mapping": {
    "_doc": {
      "properties": {
        "pending_tasks": {
          "type": "integer"
        },
        "type": {
          "type": "keyword"
        },
        "updated_at": {
          "type": "date"
        }
      }
    }
  }
}

```

'pending\_tasks' is number of tasks wait in the queue.  
'type' represent the queue type. let say it can be one of ['A', 'B', C'].  
'updated\_at' represent the date/time this document created.

So to my question:  
I first split all documents to buckets (based on date histogram).  
Now I want to maintain in each bucket only the last document per type (['A', 'B', C']), and ignore the rest of the documents in each bucket.  
How can I do this???

Now to be more kibana (TSVB) oriented:  
I want to create a chart in Kibana based on TSVB tool, that show the pending tasks over time for each of the types ['A', 'B', C'].  
The data for the number of pending task per type inr each bucket is store in the latest documents in each bucket.  
Please help.  
Thanks  
Saar

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [June 24, 2020, 2:41pm UTC](https://discuss.elastic.co/t/tsvb-create-time-series-graph-which-based-on-latest-document-in-each-bucket/235640/2 "2020-06-24T14:41:56Z")

</div>

I think what you want to do in TSVB is use the Terms aggregation to split the `type` field, and then you want to use the `Top Hits` aggregation to select only the most recent document per type. You must select a numeric field from this document, such as `pending_tasks`: but this should work.

---

<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: [July 22, 2020, 2:54pm UTC](https://discuss.elastic.co/t/tsvb-create-time-series-graph-which-based-on-latest-document-in-each-bucket/235640/3 "2020-07-22T14:54:17Z")

</div>

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