# Possible degradation in Elastic 2.x and 5.x for sorting on nested dates

**URL:** <https://discuss.elastic.co/t/possible-degradation-in-elastic-2-x-and-5-x-for-sorting-on-nested-dates/48968>\
**Category:** Elasticsearch\
**Created:** [May 2, 2016, 2:49pm UTC](https://discuss.elastic.co/t/possible-degradation-in-elastic-2-x-and-5-x-for-sorting-on-nested-dates/48968 "2016-05-02T14:49:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roeland](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Roeland](https://discuss.elastic.co/u/Roeland)\
**Post date:** [May 2, 2016, 2:49pm UTC](https://discuss.elastic.co/t/possible-degradation-in-elastic-2-x-and-5-x-for-sorting-on-nested-dates/48968/1 "2016-05-02T14:49:18Z")

</div>

SOLVED: The answer is at the bottom.

First, I have applied the following code first to ES 2.0.0 and then to a later version. Only the version of 2.0.0 works as expected...

Mapping:

```
{
  "mappings":{
    "_default_":{
      "date_detection": false,
      "dynamic_templates":[
        {
          "arrays":{
            "match":"*_array",
            "mapping":{
              "type":"nested",
              "include_in_root":false
            }
          }
        },
        {
          "dates":{
            "match":"*Date",
            "mapping":{
              "type":"date",
              "format":"strict_date_optional_time"
            }
          }
        }
      ]
    }
  }
}

```

Adding Data in bulk:

```
{"index":{"_type" : "log", "_id" : "2" } }
{"level_array":{"id":2,"ingestDate":null}}
{"index":{"_type" : "log", "_id" : "3" } }
{"level_array":{"id":3,"ingestDate":"2011-08-04"}}
{"index":{"_type" : "log", "_id" : "4" } }
{"level_array":{"id":4,"ingestDate":"2011-08-04T10:00:00"}}

```

Search sorted:

```
{
  "_source": true,
  "query": {
    "match_all": {}
  },
  "from":0,
  "size":10,
  "sort" : [{
   "level_array.ingestDate" : {
    "order" : "desc",
    "missing" : "_last"
   }
  }
 ]
}

```

In ES 2.0.0 I get the following results:

```
{
  ...
  "hits": {
    ...
    "hits": [
      {
        ...
        "_source": {
          "level_array": {
            "id": 4,
            "ingestDate": "2011-08-04T10:00:00"
          }
        },
        "sort": [
          1312452000000
        ]
      },
      {
        ...
        "_source": {
          "level_array": {
            "id": 3,
            "ingestDate": "2011-08-04"
          }
        },
        "sort": [
          1312416000000
        ]
      },
      {
        ...
        "_source": {
          "level_array": {
            "id": 2,
            "ingestDate": null
          }
        },
        "sort": [
          -9223372036854775808
        ]
      }
    ]
  }
}

```

You can see that the sort is correct, and that there are correct values in the sort result.  
If we do the same for a later release, eg 2.3.2 or 5.0.0-alpha1, the search gives the following results:

```
{
  ...
  "hits": {
    ...
    "hits": [
      {
        ...
        "_source": {
          "level_array": {
            "id": 2,
            "ingestDate": null
          }
        },
        "sort": [
          -9223372036854775808
        ]
      },
      {
        ...
        "_source": {
          "level_array": {
            "id": 4,
            "ingestDate": "2011-08-04T10:00:00"
          }
        },
        "sort": [
          -9223372036854775808
        ]
      },
      {
       ...
        "_source": {
          "level_array": {
            "id": 3,
            "ingestDate": "2011-08-04"
          }
        },
        "sort": [
          -9223372036854775808
        ]
      }
    ]
  }
}

```

Solution:

```
{
  "_source": true,
  "query": {
    "match_all": {}
  },
  "from":0,
  "size":10,
  "sort" : [{
   "level_array.ingestDate" : {
    "nested_path" : "level_array",
    "order" : "desc",
    "missing" : "_last"
   }
  }
 ]
}
```

---

<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:** [July 5, 2017, 10:54pm UTC](https://discuss.elastic.co/t/possible-degradation-in-elastic-2-x-and-5-x-for-sorting-on-nested-dates/48968/2 "2017-07-05T22:54:37Z")

</div>


