# Sorting across nodes

**URL:** https://discuss.elastic.co/t/sorting-across-nodes/31873
**Category:** Elasticsearch
**Created:** [October 8, 2015, 6:19pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873 "2015-10-08T18:19:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![adam1](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@adam1](https://discuss.elastic.co/u/adam1)
#### Post date: [October 8, 2015, 6:19pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/1 "2015-10-08T18:19:21Z")

</div>

Hi,

I'm querying with a sort clause, and I'm getting unexpected results. I'm using the java client.

I'm sorting by a long timestamp field. The generated sort clause looks like this:  
"sort" : [ {  
"timestamp\_ms" : {  
"order" : "desc"  
}  
} ]

The main issue is that I'm getting five separate sorted groupings. There are five nodes in my cluster, so it would seem it is sorting per node, but that's just a guess.

A secondary issue is that it's sorting ascending, not descending.

Here is a sample of the results I am getting (timestamp formatted to a string for readability). After this block, there are four more similar sets of results, starting over at Oct 1.

How can I get es fully sort this result set?

Thu Oct 01 19:02:32 EDT 2015  
Thu Oct 01 19:36:48 EDT 2015  
Thu Oct 01 19:38:26 EDT 2015  
Thu Oct 01 20:41:22 EDT 2015  
Fri Oct 02 09:18:57 EDT 2015  
Fri Oct 02 09:29:10 EDT 2015  
Fri Oct 02 20:34:34 EDT 2015  
Fri Oct 02 21:57:42 EDT 2015  
Fri Oct 02 22:25:10 EDT 2015  
Sat Oct 03 07:21:38 EDT 2015  
Sun Oct 04 00:35:19 EDT 2015  
Sun Oct 04 00:06:13 EDT 2015  
Sun Oct 04 00:09:46 EDT 2015  
Sun Oct 04 07:46:56 EDT 2015  
Sun Oct 04 07:49:50 EDT 2015  
Sun Oct 04 08:04:05 EDT 2015  
Sun Oct 04 10:49:27 EDT 2015  
Mon Oct 05 19:48:57 EDT 2015  
Mon Oct 05 19:57:09 EDT 2015  
Mon Oct 05 20:57:11 EDT 2015  
Mon Oct 05 21:01:48 EDT 2015  
Mon Oct 05 21:02:19 EDT 2015  
Mon Oct 05 21:57:19 EDT 2015  
Tue Oct 06 07:38:36 EDT 2015  
Tue Oct 06 07:37:40 EDT 2015  
Tue Oct 06 07:39:39 EDT 2015  
Tue Oct 06 11:03:07 EDT 2015  
Tue Oct 06 19:16:55 EDT 2015  
Tue Oct 06 19:19:32 EDT 2015  
Tue Oct 06 19:59:04 EDT 2015  
Tue Oct 06 20:01:21 EDT 2015  
Tue Oct 06 23:13:54 EDT 2015  
Wed Oct 07 12:19:11 EDT 2015  
Wed Oct 07 21:14:01 EDT 2015  
Thu Oct 08 12:47:59 EDT 2015

---

<div class="post-metadata">

### Author: ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)
#### Post date: [October 8, 2015, 6:56pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/2 "2015-10-08T18:56:37Z")

</div>

If you use Sense or curl and just do, are you getting the same results?

```auto
GET /yourindex/_search
{
  "size": 200, 
  "fields": ["timestamp_ms"], 
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "timestamp_ms": {
        "order": "desc"
      }
    }
  ]
}

```

---

<div class="post-metadata">

### Author: ![adam1](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@adam1](https://discuss.elastic.co/u/adam1)
#### Post date: [October 8, 2015, 7:50pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/3 "2015-10-08T19:50:05Z")

</div>

Hi,

Thanks for the response. It seems to only be an issue through the Java client. Is there some trick to getting it to work?

I appreciate any help.

-Adam

---

<div class="post-metadata">

### Author: ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)
#### Post date: [October 8, 2015, 11:09pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/4 "2015-10-08T23:09:48Z")

</div>

Hi,

I'm not sure, you can post your code here or link it to a gist and maybe we can figure out whats wrong.

---

<div class="post-metadata">

### Author: ![adam1](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@adam1](https://discuss.elastic.co/u/adam1)
#### Post date: [October 9, 2015, 2:56pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/5 "2015-10-09T14:56:34Z")

</div>

I've developed a lot of code around querying elasticsearch, but the sort part boils down to something like this:  
searchRequestBuilder.addSort("field\_name", SortOrder.DESC);

And I see the sort clause in the query.

I tried this, but it didn't help:  
searchRequestBuilder.setSearchType("dfs\_query\_and\_fetch");

The code to get the values looks something like this:

```
            SearchResponse response = searchRequestBuilder.get();
	SearchHits hits = response.getHits();
	for (SearchHit hit : hits) {
		Map<String, SearchHitField> hitFields = hit.fields();
		for (Entry<String, SearchHitField> entry : hitFields.entrySet()) {
			List<Object> values = entry.getValue().getValues();
			if (values != null && values.size() > 0) {
				String key = entry.getKey().toString();
				for (Object value : values) {
                                    // etc...
```

---

<div class="post-metadata">

### Author: ![adam1](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@adam1](https://discuss.elastic.co/u/adam1)
#### Post date: [October 9, 2015, 3:30pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/7 "2015-10-09T15:30:45Z")

</div>

There was a bug in my code. Thanks for looking though.

---

<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, 11:45pm UTC](https://discuss.elastic.co/t/sorting-across-nodes/31873/8 "2017-07-05T23:45:35Z")

</div>


