# Can i sort my documents by date several conditions?

**URL:** <https://discuss.elastic.co/t/can-i-sort-my-documents-by-date-several-conditions/324015>\
**Category:** Elasticsearch\
**Created:** [January 26, 2023, 11:13am UTC](https://discuss.elastic.co/t/can-i-sort-my-documents-by-date-several-conditions/324015 "2023-01-26T11:13:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volodymyr\_Dzhuryn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/volodymyr_dzhuryn/32/116423_2.png) [@Volodymyr\_Dzhuryn](https://discuss.elastic.co/u/Volodymyr_Dzhuryn)\
**Post date:** [January 26, 2023, 11:13am UTC](https://discuss.elastic.co/t/can-i-sort-my-documents-by-date-several-conditions/324015/1 "2023-01-26T11:13:49Z")

</div>

I have index with document which have two field with dates "match\_date\_time" and "created\_at".  
I need query for getting documents sorted by next criteria

```auto
1. [ASC order] articles with future match date and time (sorted by “"match_date_time”)
2. [DESC order] articles with expired match date and time (sorted by “match_date_time”)
3. [DESC order] articles without match date and time (sorted by “created_at”)

```

For example we have 6 articles

```auto
[
    {
      "id": 1,
      "match_date_time": "2023-01-28T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
    {
      "id": 2,
      "match_date_time": "2023-01-27T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
    {
      "id": 3,
      "match_date_time": "2023-01-23T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
    {
      "id": 4,
      "match_date_time": "2023-01-24T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
    {
      "id": 5,
      "match_date_time": null,
      "created_at": "2021-11-08T13:12:00+00:00"
    },

    {
      "id": 6,
      "match_date_time": null,
      "created_at": "2021-11-24T13:12:00+00:00"
    }
  ]

```

**Current date is 2023-01-26 From elastic we should get next sequence**

```auto
[
    //future date, sort asc by match_date_time
    {
      "id": 2,
      "match_date_time": "2023-01-27T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
   //future date, sort asc by match_date_time
    {
      "id": 1,
      "match_date_time": "2023-01-28T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
     //expired date, sort desc by match_date_time
    {
      "id": 4,
      "match_date_time": "2023-01-24T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
    //expired date, sort desc by match_date_time
    {
      "id": 3,
      "match_date_time": "2023-01-23T13:12:00+00:00",
      "created_at": "2021-11-08T13:12:00+00:00"
    },
    //match_date_time not exists sort desc by created_at 
    {
      "id": 6,
      "match_date_time": null,
      "created_at": "2021-11-24T13:12:00+00:00"
    },
     //match_date_time not exists sort desc by created_at 
    {
      "id": 5,
      "match_date_time": null,
      "created_at": "2021-11-08T13:12:00+00:00"
    }
  ]

```

It is possible to get data by one query?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [January 26, 2023, 11:41am UTC](https://discuss.elastic.co/t/can-i-sort-my-documents-by-date-several-conditions/324015/2 "2023-01-26T11:41:43Z")

</div>

Hi Volodymyr,

You can specify multiple fields for sorting with appropriate directions for each. [The first example in the docs should help show the syntax](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/sort-search-results.html).

---

<div class="post-metadata">

**Author:** ![Volodymyr\_Dzhuryn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/volodymyr_dzhuryn/32/116423_2.png) [@Volodymyr\_Dzhuryn](https://discuss.elastic.co/u/Volodymyr_Dzhuryn)\
**Post date:** [January 26, 2023, 11:55am UTC](https://discuss.elastic.co/t/can-i-sort-my-documents-by-date-several-conditions/324015/3 "2023-01-26T11:55:34Z")

</div>

but my sort criteria depend on field value. Documents which have match\_date\_time in future should be first and sorted by field match\_date\_time [ASC order], after them should be documents which have match\_date\_time in past and sorted by match\_date\_time [DESC order].

---

<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:** [February 23, 2023, 11:56am UTC](https://discuss.elastic.co/t/can-i-sort-my-documents-by-date-several-conditions/324015/4 "2023-02-23T11:56:18Z")

</div>

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