Unique values with sort request

Hi everyone,

I currently have a query with the purpose being to only retrieve documents with a unique "Object ID"

And then order the documents by the date they were created

It'd be the equivalent to "SELECT DISTINCT id, DateCreated, Cat FROM table GROUP BY ObjectID ORDER BY DateCreated DESC" in SQL

However, the sort does not seem to work, I'm not sure if this even possible / defeats the purpose of top hits, or if I need to add another aggregation ?

Elasticsearch version: 5.6.5

{
  "aggs": {
    "agg1": {
      "terms": {
        "field": "ObjectID"
      },
        "aggs": {
                "top_docs": {
                    "top_hits": {
                        "sort": [
                            {
                                "DateCreated": {
                                    "order": "asc"
                                }
                            }
                        ],
                        "_source": {
                            "includes": [
                                "id", "DateCreated", "Cat", "Name"
                            ]
                        },
                        "size" : 1
                    }
                }
            }
        }
      }
    }

Any assistance would be greatly appreciated

Instead of using the aggregation you may want to try the field collapsing API

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