# Filter latest document in each bucket

**URL:** <https://discuss.elastic.co/t/filter-latest-document-in-each-bucket/235638>\
**Category:** Elasticsearch\
**Tags:** kql-kibana-query-language\
**Created:** [June 3, 2020, 9:49pm UTC](https://discuss.elastic.co/t/filter-latest-document-in-each-bucket/235638 "2020-06-03T21:49:45Z")\
**Posts on this page:** 2\
**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:49pm UTC](https://discuss.elastic.co/t/filter-latest-document-in-each-bucket/235638/1 "2020-06-03T21:49:46Z")

</div>

Hi All.  
I need your help, I just started working with Elasticsearch.  
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 specific:  
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, for each bucket is store in the latest documents in each bucket.  
Please help.  
Thanks  
Saar

---

<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 1, 2020, 9:49pm UTC](https://discuss.elastic.co/t/filter-latest-document-in-each-bucket/235638/2 "2020-07-01T21:49:46Z")

</div>

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