# Sorting response hits using java api

**URL:** https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179
**Category:** Elasticsearch
**Created:** [August 12, 2013, 3:28pm UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179 "2013-08-12T15:28:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nir\_Biran](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@Nir\_Biran](https://discuss.elastic.co/u/Nir_Biran)
#### Post date: [August 12, 2013, 3:28pm UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179/1 "2013-08-12T15:28:57Z")

</div>

Hello,

i'm using logstash to manage logs, and as part of it - elasticsearch.  
when querying the logs i'm using\* scan search\* and have the following  
fields for each hit: @message, @source, @source\_host, @source\_path, @tags, \*@timestamp,  
\*@type.

this is my problem - the returned hits are unsorted. I want to sort them by  
@timestamp. How can I do so in java?  
This is what I tried:

SearchResponse scrollResp1 = client.prepareSearch()  
.setSearchType(SearchType.SCAN)  
.setScroll(new TimeValue(600000))  
.setQuery(QueryBuilders.queryString("@tags:"" +  
tagToSearch + "" AND @tags:"" + buildNumber + "" AND @source\_path:"" +  
fileName + """))  
.setSize(100)_.addSort("@timestamp", SortOrder.ASC)_  
.execute().actionGet();

and then the scrolling for scan search:

```
        while (true) {
            scrollResp1 = 

```

client.prepareSearchScroll(scrollResp1.getScrollId()).setScroll(new  
TimeValue(600000)).execute().actionGet();  
String message;

```
            for (SearchHit hit : scrollResp1.getHits()) {
                     ..................
            }

```

but it didn't sort anything..

any suggestions?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [August 12, 2013, 3:55pm UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179/2 "2013-08-12T15:55:48Z")

</div>

You cannot sort scan searches.

--  
Ivan

On Mon, Aug 12, 2013 at 8:28 AM, Nir Biran [nbiran2@gmail.com](mailto:nbiran2@gmail.com) wrote:

> Hello,
> 
> i'm using logstash to manage logs, and as part of it - elasticsearch.  
> when querying the logs i'm using\* scan search\* and have the following  
> fields for each hit: @message, @source, @source\_host, @source\_path, @tags,  
> \*@timestamp, \*@type.
> 
> this is my problem - the returned hits are unsorted. I want to sort them  
> by @timestamp. How can I do so in java?  
> This is what I tried:
> 
> SearchResponse scrollResp1 = client.prepareSearch()  
> .setSearchType(SearchType.SCAN)  
> .setScroll(new TimeValue(600000))  
> .setQuery(QueryBuilders.queryString("@tags:"" +  
> tagToSearch + "" AND @tags:"" + buildNumber + "" AND @source\_path:"" +  
> fileName + """))  
> .setSize(100)_.addSort("@timestamp", SortOrder.ASC)_  
> .execute().actionGet();
> 
> and then the scrolling for scan search:
> 
> ```
> while (true) {
> scrollResp1 =
> 
> ```
> 
> client.prepareSearchScroll(scrollResp1.getScrollId()).setScroll(new  
> TimeValue(600000)).execute().actionGet();  
> String message;
> 
> ```
> for (SearchHit hit : scrollResp1.getHits()) {
> ..................
> }
> 
> ```
> 
> but it didn't sort anything..
> 
> any suggestions?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [August 12, 2013, 9:01pm UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179/3 "2013-08-12T21:01:02Z")

</div>

Ivan,

Slight correction: Elasticsearch cannot sort scan searches. But I can!

My sorting of response documents is external to Elasticsearch, bolted on  
after the query has returned. I use a TreeSet which keeps its members  
sorted. I then implement a limit. When the limit is reached:

1. If the document is outside the upper bound it is ignored.
2. Otherwise, the document is added and the last document (at the upper  
bound) is removed (preserving the specified limit).

As I issue a scan query, I can then push documents into this TreeSet-based  
sorter and always end up with the N-most sorted documents. This isn't  
something I use a lot, but when I do need it this method is very, very  
useful. And the additional time related to sorting (including creating  
keys) is minimal.

Of course, there's lots of code to create the sort keys (probably  
duplicates a lot of what ES is doing, if I could see its internal comments  
😉

But I do have a side question: Is there a way to query (via Java, of  
course) the types of each explicitly mapped field? I can create mappings,  
but querying the mappings for this kind of thing eludes me. For now, I've  
created my own "schema" definition, and then use it to (1) generate the ES  
mappings with all the niggly add-ons (such as Finnish character  
equivalencies), and then also use it to drive the collation key generation  
for this post-query sorting. But if I could ask Elasticsearch directly at  
run-time, then this process would become much simpler and more robust.

Brian

On Monday, August 12, 2013 11:55:48 AM UTC-4, Ivan Brusic wrote:

> You cannot sort scan searches.
> 
> --  
> Ivan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Nir\_Biran](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@Nir\_Biran](https://discuss.elastic.co/u/Nir_Biran)
#### Post date: [August 13, 2013, 7:14am UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179/4 "2013-08-13T07:14:18Z")

</div>

Thanks for the replies.  
Brian, I don't have an answer for you, but I have a follow up question to  
my original post in light of the replies:

So I can't sort a scan search, but is there a different search which is  
sortable AND returns more than the first 10 hits?  
I only used scan search because it returns all the hits and not only the  
first 10..

seems like a trivial request - return all hits sorted.  
is there a trivial way?

On Tuesday, 13 August 2013 00:01:02 UTC+3, InquiringMind wrote:

> Ivan,
> 
> Slight correction: Elasticsearch cannot sort scan searches. But I can!
> 
> My sorting of response documents is external to Elasticsearch, bolted on  
> after the query has returned. I use a TreeSet which keeps its members  
> sorted. I then implement a limit. When the limit is reached:
> 
> 1. If the document is outside the upper bound it is ignored.
> 2. Otherwise, the document is added and the last document (at the upper  
> bound) is removed (preserving the specified limit).
> 
> As I issue a scan query, I can then push documents into this TreeSet-based  
> sorter and always end up with the N-most sorted documents. This isn't  
> something I use a lot, but when I do need it this method is very, very  
> useful. And the additional time related to sorting (including creating  
> keys) is minimal.
> 
> Of course, there's lots of code to create the sort keys (probably  
> duplicates a lot of what ES is doing, if I could see its internal comments  
> 😉
> 
> But I do have a side question: Is there a way to query (via Java, of  
> course) the types of each explicitly mapped field? I can create mappings,  
> but querying the mappings for this kind of thing eludes me. For now, I've  
> created my own "schema" definition, and then use it to (1) generate the ES  
> mappings with all the niggly add-ons (such as Finnish character  
> equivalencies), and then also use it to drive the collation key generation  
> for this post-query sorting. But if I could ask Elasticsearch directly at  
> run-time, then this process would become much simpler and more robust.
> 
> Brian
> 
> On Monday, August 12, 2013 11:55:48 AM UTC-4, Ivan Brusic wrote:
> 
> > You cannot sort scan searches.
> > 
> > --  
> > Ivan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)
#### Post date: [August 13, 2013, 8:20am UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179/5 "2013-08-13T08:20:21Z")

</div>

There is from/size

[http://www.elasticsearch.org/guide/reference/api/search/from-size/](http://www.elasticsearch.org/guide/reference/api/search/from-size/)

Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:21am UTC](https://discuss.elastic.co/t/sorting-response-hits-using-java-api/13179/6 "2017-07-06T02:21:24Z")

</div>


