Sorting on Multiple fields, JAVA API


(Harsh) #1

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.


(Shay Banon) #2

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 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.


(Stine) #3

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 <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.

--


(phill) #4

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.

--


(MMM) #5

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 ( :frowning: ).

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.

--


(Stine) #6

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 ( :frowning: ).

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.

--


(system) #7