# Sort data by field in Elasticsearch

**URL:** <https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463>\
**Category:** Elasticsearch\
**Created:** [November 29, 2016, 10:18am UTC](https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463 "2016-11-29T10:18:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniketpatil](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@aniketpatil](https://discuss.elastic.co/u/aniketpatil)\
**Post date:** [November 29, 2016, 10:18am UTC](https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463/1 "2016-11-29T10:18:31Z")

</div>

I have written elasticsearch query using java api in which status count is fetched per day.  
Code:

```
SearchResponse responseOutput = client.prepareSearch(ConstantsValue.indexName)
.setTypes(ConstantsValue._Type)
.setFetchSource(new String[]{"STATUS", "DTCREATED"}, null)
.setQuery(
    QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
    FilterBuilders.rangeFilter("DTCREATED").from(fromDate).to(toDate))).
    addAggregation(AggregationBuilders.terms("STATUS_AGG").field("STATUS")
        .subAggregation(AggregationBuilders.dateHistogram("DATE_AGG").field("DTCREATED").interval(DateHistogram.Interval.DAY).format("yyyy-MM-dd"))
        )
       .addSort("DTCREATED", SortOrder.ASC)
       .get();

```

I am trying to sort data by DTCREATED field which contains both date and time but query does not provide sorted result. I can't find what I am missing in query. Any help ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 29, 2016, 10:30am UTC](https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463/2 "2016-11-29T10:30:33Z")

</div>

May be share some data sample or the resultset?

---

<div class="post-metadata">

**Author:** ![aniketpatil](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@aniketpatil](https://discuss.elastic.co/u/aniketpatil)\
**Post date:** [November 29, 2016, 10:37am UTC](https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463/3 "2016-11-29T10:37:24Z")

</div>

Mapping is as below:

```
{
  "transactioninfo" : {
    "mappings" : {
      "doc" : {
        "properties" : {
            "DTCREATED" : {
            "type" : "date",
            "format" : "dateOptionalTime"
          },
            "STATUS" : {
            "type" : "string"
          },
       }
    }
}

```

Resultset:

```
 "aggregations" : {
    "group_by_STATUS" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 6,
      "buckets" : [ {
        "key" : "success",
        "doc_count" : 11148,
        "group_by_DATE" : {
          "buckets" : [ {
            "key_as_string" : "2016-09-26",
            "key" : 1474848000000,
            "doc_count" : 6,
            "grades_count" : {
              "value" : 6
            }
          }, {
            "key_as_string" : "2016-09-27",
            "key" : 1474934400000,
            "doc_count" : 19,
            "grades_count" : {
              "value" : 19
            }
   }

```

Data format:

> "STATUS":"SUCCESS"  
> "DTCREATED":"2016-03-02T08:07:05.000Z"

Response object contains sorted data but when rendering graph in nvd3 x axis dates are not rendered in sorted order

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 29, 2016, 10:57am UTC](https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463/4 "2016-11-29T10:57:22Z")

</div>

You set the order on the query not on the agg itself.

I guess that if you look at hits, they are correctly sorted.

That being said, date histogram already produces ASC sorted result.

[https://github.com/elastic/elasticsearch/blob/5.x/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java#L80-L80](https://github.com/elastic/elasticsearch/blob/5.x/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java#L80-L80)

As far as I can see your resultset, it comes in the correct order, no?

---

<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:** [December 27, 2016, 10:57am UTC](https://discuss.elastic.co/t/sort-data-by-field-in-elasticsearch/67463/5 "2016-12-27T10:57:41Z")

</div>

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