Sorting on _score in the URI format is reversed, closes #1191.

This commit is contained in:
Shay Banon 2011-08-02 03:08:48 +03:00
parent 2b777df21e
commit cb51d3c576
3 changed files with 48 additions and 4 deletions

View File

@ -64,8 +64,8 @@ public class FieldSortBuilder extends SortBuilder {
@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);

View File

@ -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";
}
}
}

View File

@ -29,6 +29,7 @@ import org.elasticsearch.search.SearchHit;
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;
@ -112,6 +113,41 @@ public class SimpleSortTests extends AbstractNodesTests {
}
}
@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);
}