# ELASTICSEARCH - Sort agg values only in most recent document

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-agg-values-only-in-most-recent-document/247398>\
**Category:** Elasticsearch\
**Created:** [September 3, 2020, 2:04pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-agg-values-only-in-most-recent-document/247398 "2020-09-03T14:04:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_BA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_ba/32/46926_2.png) [@Miguel\_BA](https://discuss.elastic.co/u/Miguel_BA)\
**Post date:** [September 3, 2020, 2:04pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-agg-values-only-in-most-recent-document/247398/1 "2020-09-03T14:04:41Z")

</div>

I want to return the most recent document of all results of the aggregations, but it is returning all the documents How can I solve that?

```auto
{
  "aggs": {
    "nested": {
      "nested": {
        "path": "data.add"
      },
      "aggs": {
        "desc": {
          "terms": {
            "field": "data.add.desc.keyword"
          },
          "aggs": {
            "nested": {
              "nested": {
                "path": "data.add.services"
              },
              "aggs": {
                "mobilephone": {
                  "filter": {
                    "term": {
                      "data.add.services.mobilephone": "true"
                    }
                  },
                  "aggs": {
                    "street": {
                      "cardinality": {
                        "field": "data.add.services.street.keyword"
                      }
                    },
                    "range":{
                      "top_hits": {
                        "size": 1,
                        "sort": [
                          {"created_at.keyword": {"order": "desc"}}]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [September 6, 2020, 9:42pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-agg-values-only-in-most-recent-document/247398/2 "2020-09-06T21:42:15Z")

</div>

Try adding a top hits aggregation, based off the timestamp.

---

<div class="post-metadata">

**Author:** ![Miguel\_BA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_ba/32/46926_2.png) [@Miguel\_BA](https://discuss.elastic.co/u/Miguel_BA)\
**Post date:** [September 8, 2020, 5:20pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-agg-values-only-in-most-recent-document/247398/3 "2020-09-08T17:20:09Z")

</div>

For this case the solution is to add:

```auto
"order": {
          "_key": "desc":

```

instead of top\_hits.

QUERY

```auto
{"size": 0, 
  "aggs": {
    "filtrofecha": {
      "terms": {
        "field": "created_at.keyword",
        "order": {
          "_key": "desc"
        },
        "size": 1
      },
      "aggs": {
        "nested": {
          "nested": {
            "path": "data.addresses"
              },
              "aggs": {
                "nested": {
                  "nested": {
                    "path": "data.addresses.services"
                  },
                  "aggs": {
                    "filter": {
                      "filter": {
                        "term": {
                          "data.addresses.services.field1.keyword": "true"
                        }
                      },
                      "aggs": {
                        "unique": {
                          "cardinality": {
                            "field": "data.addresses.services.field2.keyword"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

```

---

<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:** [October 6, 2020, 5:20pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-agg-values-only-in-most-recent-document/247398/4 "2020-10-06T17:20:23Z")

</div>

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