# Sorting on Multiple fields, JAVA API

**URL:** <https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298>\
**Category:** Elasticsearch\
**Created:** [July 3, 2012, 1:59pm UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298 "2012-07-03T13:59:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harsh](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Harsh](https://discuss.elastic.co/u/Harsh)\
**Post date:** [July 3, 2012, 1:59pm UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/1 "2012-07-03T13:59:30Z")

</div>

Hello,

I have written a small search application and have used the addSort()  
method for sorting the results. What I want to do is first sort the results  
based on its score and if the score is same then it should sort it based on  
a field which I have indexed. The code is as below

SearchResponse response =  
client.prepareSearch("indexName").setQuery(query).setFrom(0).setSize(60).addSort(scoreSort).addSort("field",SortOrder.DESC).execute.actionGet();

where "scoreSort" is a "ScoreSortBuilder" object.

I get the desired result if I use only one sort i.e either sorting on score  
or on the field. But when I use "addSort()" twice i.e first on score and  
then on field, I don't get the desired result. It is not sorting based on  
the "field" if the score is same.

Can anyone suggest what I might be doing wrong here? Any help would be  
appreciated.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 3, 2012, 4:21pm UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/2 "2012-07-03T16:21:07Z")

</div>

It should work, can you post a recreation showing that it does not work?

On Tue, Jul 3, 2012 at 3:59 PM, Harsh [harsh.chhatwani@gmail.com](mailto:harsh.chhatwani@gmail.com) wrote:

> Hello,
> 
> I have written a small search application and have used the addSort()  
> method for sorting the results. What I want to do is first sort the results  
> based on its score and if the score is same then it should sort it based on  
> a field which I have indexed. The code is as below
> 
> SearchResponse response =  
> client.prepareSearch("indexName").setQuery(query).setFrom(0).setSize(60).addSort(scoreSort).addSort("field",SortOrder.DESC).execute.actionGet();
> 
> where "scoreSort" is a "ScoreSortBuilder" object.
> 
> I get the desired result if I use only one sort i.e either sorting on  
> score or on the field. But when I use "addSort()" twice i.e first on score  
> and then on field, I don't get the desired result. It is not sorting based  
> on the "field" if the score is same.
> 
> Can anyone suggest what I might be doing wrong here? Any help would be  
> appreciated.

---

<div class="post-metadata">

**Author:** ![Stine](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Stine](https://discuss.elastic.co/u/Stine)\
**Post date:** [August 31, 2012, 1:35pm UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/3 "2012-08-31T13:35:33Z")

</div>

I am having the same issue. I would like something like:

client.prepareSearch(indexName).setTypes(typeName)  
.setQuery(query)  
.addSort(fieldSort("lastActivity").order(DESC).missing("\_last"))  
.addSort(fieldSort("hasPicture").order(DESC).missing("\_last"))  
.setSize(limit)  
.execute().actionGet();

But that does not work... until I remove .missing("\_last") from the two  
sorts. Then it stops failing.

Den tirsdag den 3. juli 2012 18.21.07 UTC+2 skrev kimchy:

> It should work, can you post a recreation showing that it does not work?
> 
> On Tue, Jul 3, 2012 at 3:59 PM, Harsh \<[harsh.c...@gmail.com](mailto:harsh.c...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hello,
> > 
> > I have written a small search application and have used the addSort()  
> > method for sorting the results. What I want to do is first sort the results  
> > based on its score and if the score is same then it should sort it based on  
> > a field which I have indexed. The code is as below
> > 
> > SearchResponse response =  
> > client.prepareSearch("indexName").setQuery(query).setFrom(0).setSize(60).addSort(scoreSort).addSort("field",SortOrder.DESC).execute.actionGet();
> > 
> > where "scoreSort" is a "ScoreSortBuilder" object.
> > 
> > I get the desired result if I use only one sort i.e either sorting on  
> > score or on the field. But when I use "addSort()" twice i.e first on score  
> > and then on field, I don't get the desired result. It is not sorting based  
> > on the "field" if the score is same.
> > 
> > Can anyone suggest what I might be doing wrong here? Any help would be  
> > appreciated.

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [September 5, 2012, 3:59am UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/4 "2012-09-05T03:59:54Z")

</div>

I would look into the mapping to see what type the hasPicture field is  
and see if \_last results in a value beyond the values that are present.

-Paul

On 8/31/2012 6:35 AM, Stine wrote:

> I am having the same issue. I would like something like:
> 
> |  
> client.prepareSearch(indexName).setTypes(typeName)  
> .setQuery(query)  
> .addSort(fieldSort("lastActivity").order(DESC).missing("\_last"))  
> .addSort(fieldSort("hasPicture").order(DESC).missing("\_last"))  
> .setSize(limit)  
> .execute().actionGet();  
> |
> 
> But that does not work... until I remove .missing("\_last") from the  
> two sorts. Then it stops failing.

--

---

<div class="post-metadata">

**Author:** ![MMM](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@MMM](https://discuss.elastic.co/u/MMM)\
**Post date:** [September 5, 2012, 12:01pm UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/5 "2012-09-05T12:01:06Z")

</div>

Hello,

take a look at your servers log.

Numeric fields support specific handling for missing fields in a doc

I don't think, that you hasPicture is Integer or? I know, that missing is  
not working on String field ( ☹ ).

Matus

Am Mittwoch, 5. September 2012 05:59:56 UTC+2 schrieb P Hill:

> I would look into the mapping to see what type the hasPicture field is  
> and see if \_last results in a value beyond the values that are present.
> 
> -Paul
> 
> On 8/31/2012 6:35 AM, Stine wrote:
> 
> I am having the same issue. I would like something like:
> 
> client.prepareSearch(indexName).setTypes(typeName)  
> .setQuery(query)  
> .addSort(fieldSort("lastActivity").order(DESC).missing("\_last"))  
> .addSort(fieldSort("hasPicture").order(DESC).missing("\_last"))  
> .setSize(limit)  
> .execute().actionGet();
> 
> But that does not work... until I remove .missing("\_last") from the two  
> sorts. Then it stops failing.

--

---

<div class="post-metadata">

**Author:** ![Stine](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Stine](https://discuss.elastic.co/u/Stine)\
**Post date:** [September 11, 2012, 7:59am UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/6 "2012-09-11T07:59:06Z")

</div>

Hello,

thanks a lot for your input! You were right - when I removed the  
missing("\_last") from hasPicture it worked fine.

-Stine

Den onsdag den 5. september 2012 14.01.06 UTC+2 skrev MMM:

> Hello,
> 
> take a look at your servers log.
> 
> Numeric fields support specific handling for missing fields in a doc
> 
> I don't think, that you hasPicture is Integer or? I know, that missing is  
> not working on String field ( ☹ ).
> 
> Matus
> 
> Am Mittwoch, 5. September 2012 05:59:56 UTC+2 schrieb P Hill:
> 
> > I would look into the mapping to see what type the hasPicture field is  
> > and see if \_last results in a value beyond the values that are present.
> > 
> > -Paul
> > 
> > On 8/31/2012 6:35 AM, Stine wrote:
> > 
> > I am having the same issue. I would like something like:
> > 
> > client.prepareSearch(indexName).setTypes(typeName)  
> > .setQuery(query)  
> > .addSort(fieldSort("lastActivity").order(DESC).missing("\_last"))  
> > .addSort(fieldSort("hasPicture").order(DESC).missing("\_last"))  
> > .setSize(limit)  
> > .execute().actionGet();
> > 
> > But that does not work... until I remove .missing("\_last") from the two  
> > sorts. Then it stops failing.

--

---

<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, 3:13am UTC](https://discuss.elastic.co/t/sorting-on-multiple-fields-java-api/8298/7 "2017-07-06T03:13:15Z")

</div>


