# Specify format in sort query

**URL:** <https://discuss.elastic.co/t/specify-format-in-sort-query/95435>\
**Category:** Elasticsearch\
**Created:** [August 2, 2017, 1:10am UTC](https://discuss.elastic.co/t/specify-format-in-sort-query/95435 "2017-08-02T01:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)\
**Post date:** [August 2, 2017, 1:10am UTC](https://discuss.elastic.co/t/specify-format-in-sort-query/95435/1 "2017-08-02T01:10:20Z")

</div>

I have a query, where `someDate` is of type date. I don't have control over mapping while creating an index. I wanted to get the value in millis for the following query. Is there a way to do that in ES (via API)

```
GET /myindex/_search
{
    "size": 1,
    "sort": [
        { "someDate": {"order": "asc"},
          "format": "epoch_millis" // this does not seem to be supported in ES 5.4
        }
        ]
}

```

The actual response looks like this:

`"SomeDate": "2017-08-01T01:07:04.000Z"`

I would like to get the following response

`"SomeDate": 1501636024000`

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 2, 2017, 8:52pm UTC](https://discuss.elastic.co/t/specify-format-in-sort-query/95435/2 "2017-08-02T20:52:13Z")

</div>

I'd probably [use a script\_field](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html) to do this.

Script fields basically execute an arbitrary script for each hit, and the script's return value is used to create a "virtual" field on the document. So you could use the script field to generate a "formattedDate" field which has the formatting you desire.

---

<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:** [August 30, 2017, 8:52pm UTC](https://discuss.elastic.co/t/specify-format-in-sort-query/95435/3 "2017-08-30T20:52:40Z")

</div>

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