# How to sort my time field in descending order with size 1 to get graph with only the latest documents?

**URL:** <https://discuss.elastic.co/t/how-to-sort-my-time-field-in-descending-order-with-size-1-to-get-graph-with-only-the-latest-documents/270494>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [April 18, 2021, 1:26pm UTC](https://discuss.elastic.co/t/how-to-sort-my-time-field-in-descending-order-with-size-1-to-get-graph-with-only-the-latest-documents/270494 "2021-04-18T13:26:15Z")\
**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 18, 2021, 1:26pm UTC](https://discuss.elastic.co/t/how-to-sort-my-time-field-in-descending-order-with-size-1-to-get-graph-with-only-the-latest-documents/270494/1 "2021-04-18T13:26:15Z")

</div>

Hi.  
I have Vega-lite code for a graph in kibana.  
I need to sort my time field in descending order with size 1 in order to get the documents belonging to the latest time.  
I know I need to use the following lines, but I don't know how to combine them with the rest of the code:

```auto
"body": {
    "size": 10000,
    "aggs": {
        "time_field": {
            "terms": {
                "field": "Time",
                "size": 1,
                "order": {
                    "_key": "desc"
                }
            },
            "aggs": {
                "x_Field": {
                    "terms": {
                        "field": "x",
                        "size": 10000
                    }
                }
            }
        }
    }
}

```

And this is the code I have now:

```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:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [April 18, 2021, 4:26pm UTC](https://discuss.elastic.co/t/how-to-sort-my-time-field-in-descending-order-with-size-1-to-get-graph-with-only-the-latest-documents/270494/2 "2021-04-18T16:26:35Z")

</div>

If you can provide the [Vega Spec](https://www.elastic.co/guide/en/kibana/current/vega.html#asking-for-help-with-a-vega-spec) (which will contain a sample of the data returned from the ES query) it would help us see what your data looks like in order to make a recommendation.

---

<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 19, 2021, 10:24am UTC](https://discuss.elastic.co/t/how-to-sort-my-time-field-in-descending-order-with-size-1-to-get-graph-with-only-the-latest-documents/270494/3 "2021-04-19T10:24:29Z")

</div>

Unfortunately, I don't know how to do it without exposing business confidential data...

---

<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 17, 2021, 10:25am UTC](https://discuss.elastic.co/t/how-to-sort-my-time-field-in-descending-order-with-size-1-to-get-graph-with-only-the-latest-documents/270494/4 "2021-05-17T10:25:11Z")

</div>

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