Java SearchHits provides out-of-order results when sort order provided in query

I'm using Elasticsearch 1.5.2, and after doing a search that specifies a sort order; when I run the generated JSON (as copied from the Java objects), the results appear in the desired sorted order.

However, when I extract search results as below, the results aren't in the same order.

Unless I'm doing something wrong here, I expect that this version of Elasticsearch perhaps is using a non-ordered Collection to store the search hits, causing this to happen. Does anyone know if this is the case, and if there's a workaround?

Incidentally, I have the source jar for this version of Elasticsearch, but for some reason the subpackage is not included in that source jar...

  • Tim

      // Run the query
      SearchResponse response = builder.execute().actionGet();
      // Scroll until no hits are returned
      List<Map<String, Object>> resultsList = new ArrayList<Map<String, Object>>();
      boolean done = false;
      while (!done) {
          response = getClient().prepareSearchScroll(response.getScrollId()).setScroll(TimeValue.timeValueMinutes(1))
          for (SearchHit searchHit : response.getHits().getHits()) {
              Map<String, SearchHitField> fields = searchHit.getFields();
              Map<String, Object> document = new HashMap<String, Object>();
              for (Map.Entry<String, SearchHitField> entry : fields.entrySet()) {
                  String name = entry.getKey();
                  SearchHitField hitValue = entry.getValue();
                  document.put(name, getSearchHitFieldValue(name, hitValue));
          done = (response.getHits().getHits().length == 0);

Nevermind, folks, I found out it's because I'm using the SCAN type query, which doesn't sort.

It pays to read ALL the documentation - sorry! :wink:

  • Tim