Search code examples
elasticsearchmongoosastic

Reverse order in ElasticSearch when zero_terms_query


I use elasticsearch on a main page. Now you can search and everything works perfect.

As an initial I use 'zero_terms_query': 'all which shows all the results in insertion order. I'd however prefer to see the lastest rather than the oldest ones shown. I think I can not change the order for all the query since the search query are already in a good order.

How can I change the order of this query? Or how to "reverse insert" in elaticsearch?

Thanks


Solution

  • You can use sort to specify how you want your results to be sorted. You need to use a field that contains the notion of "latest vs oldest", usually people use a date or timestamp field for that:

    {
       "sort": {
          "your_timestamp" : "desc"
       },
       "query": {
          "match": {...}
       }
    }