# Plot only documents with the latest time

**URL:** <https://discuss.elastic.co/t/plot-only-documents-with-the-latest-time/269800>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [April 11, 2021, 10:02am UTC](https://discuss.elastic.co/t/plot-only-documents-with-the-latest-time/269800 "2021-04-11T10:02:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shalhevet](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Shalhevet](https://discuss.elastic.co/u/Shalhevet)\
**Post date:** [April 11, 2021, 10:02am UTC](https://discuss.elastic.co/t/plot-only-documents-with-the-latest-time/269800/1 "2021-04-11T10:02:59Z")

</div>

I have an index in with time field, and for each time there are multiple documents.  
I want to present only the results of the latest time. how can I do it?  
Is there a way to calculate the maximum value of the time field over all documents, and then apply a filter based on this value?

here is an example of the code:

```auto
{
  $schema: https://vega.github.io/schema/vega-lite/v2.json

  data: {
    data: table
    url: {
      %context%: true
      %timefield%: Time
      index: data*
      body: {
        size: 10000
        _source: ["X", "Y"]
      }
    }
    format: {property: "hits.hits"}
  }
  mark: {type: "square", filled: true, size: 800 }
encoding: {
    x: {
      field: "_source.X"
      type: quantitative
      axis: {title: "X"}
    }
    y: {
      field: "_source.Y"
      type: quantitative
      axis: {title: "Y"}
    }  
}
}

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [April 12, 2021, 1:50pm UTC](https://discuss.elastic.co/t/plot-only-documents-with-the-latest-time/269800/2 "2021-04-12T13:50:55Z")

</div>

Use a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) to group your documents. Then use [top hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) aggregation to only get a single document per term.

---

<div class="post-metadata">

**Author:** ![Shalhevet](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Shalhevet](https://discuss.elastic.co/u/Shalhevet)\
**Post date:** [April 13, 2021, 5:42am UTC](https://discuss.elastic.co/t/plot-only-documents-with-the-latest-time/269800/6 "2021-04-13T05:42:46Z")

</div>

Thank you for your reply.  
But for a given Time value there are multiple documents, and I need all of them.  
and after visiting the link you sent, I am not sure where and how I should add these lines to my code.  
is it in the "body" section?

---

<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 11, 2021, 5:43am UTC](https://discuss.elastic.co/t/plot-only-documents-with-the-latest-time/269800/7 "2021-05-11T05:43:34Z")

</div>

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