Skip to content

Commit

Permalink
Sorting on _score in the URI format is reversed, closes elastic#1191.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Aug 2, 2011
1 parent daa1a3c commit 2a79ff1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Expand Up @@ -64,8 +64,8 @@ public FieldSortBuilder(String fieldName) {

@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(fieldName);
if (order == SortOrder.DESC) {
builder.field("reverse", true);
if (order != null) {
builder.field("order", order.toString());
}
if (missing != null) {
builder.field("missing", missing);
Expand Down
Expand Up @@ -28,9 +28,17 @@ public enum SortOrder {
/**
* Ascending order.
*/
ASC,
ASC {
@Override public String toString() {
return "asc";
}
},
/**
* Descending order.
*/
DESC
DESC {
@Override public String toString() {
return "desc";
}
}
}
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.hamcrest.Matchers;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -112,6 +113,41 @@ protected Client getClient() {
}
}

@Test public void testScoreSortDirection() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 1)).execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();

client.prepareIndex("test", "type", "1").setSource("field", 2).execute().actionGet();
client.prepareIndex("test", "type", "2").setSource("field", 1).execute().actionGet();
client.prepareIndex("test", "type", "3").setSource("field", 0).execute().actionGet();

client.admin().indices().prepareRefresh().execute().actionGet();

SearchResponse searchResponse = client.prepareSearch("test").setQuery(customScoreQuery(matchAllQuery()).script("_source.field")).execute().actionGet();
assertThat(searchResponse.hits().getAt(0).getId(), equalTo("1"));
assertThat(searchResponse.hits().getAt(1).score(), Matchers.lessThan(searchResponse.hits().getAt(0).score()));
assertThat(searchResponse.hits().getAt(1).getId(), equalTo("2"));
assertThat(searchResponse.hits().getAt(2).score(), Matchers.lessThan(searchResponse.hits().getAt(1).score()));
assertThat(searchResponse.hits().getAt(2).getId(), equalTo("3"));

searchResponse = client.prepareSearch("test").setQuery(customScoreQuery(matchAllQuery()).script("_source.field")).addSort("_score", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.hits().getAt(0).getId(), equalTo("1"));
assertThat(searchResponse.hits().getAt(1).score(), Matchers.lessThan(searchResponse.hits().getAt(0).score()));
assertThat(searchResponse.hits().getAt(1).getId(), equalTo("2"));
assertThat(searchResponse.hits().getAt(2).score(), Matchers.lessThan(searchResponse.hits().getAt(1).score()));
assertThat(searchResponse.hits().getAt(2).getId(), equalTo("3"));

searchResponse = client.prepareSearch("test").setQuery(customScoreQuery(matchAllQuery()).script("_source.field")).addSort("_score", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.hits().getAt(2).getId(), equalTo("3"));
assertThat(searchResponse.hits().getAt(1).getId(), equalTo("2"));
assertThat(searchResponse.hits().getAt(0).getId(), equalTo("1"));
}

@Test public void testSimpleSortsSingleShard() throws Exception {
testSimpleSorts(1);
}
Expand Down

0 comments on commit 2a79ff1

Please sign in to comment.