Sorting on _score in the URI format is reversed, closes #1191.
This commit is contained in:
parent
2b777df21e
commit
cb51d3c576
|
@ -64,8 +64,8 @@ public class FieldSortBuilder extends SortBuilder {
|
||||||
|
|
||||||
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject(fieldName);
|
builder.startObject(fieldName);
|
||||||
if (order == SortOrder.DESC) {
|
if (order != null) {
|
||||||
builder.field("reverse", true);
|
builder.field("order", order.toString());
|
||||||
}
|
}
|
||||||
if (missing != null) {
|
if (missing != null) {
|
||||||
builder.field("missing", missing);
|
builder.field("missing", missing);
|
||||||
|
|
|
@ -28,9 +28,17 @@ public enum SortOrder {
|
||||||
/**
|
/**
|
||||||
* Ascending order.
|
* Ascending order.
|
||||||
*/
|
*/
|
||||||
ASC,
|
ASC {
|
||||||
|
@Override public String toString() {
|
||||||
|
return "asc";
|
||||||
|
}
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* Descending order.
|
* Descending order.
|
||||||
*/
|
*/
|
||||||
DESC
|
DESC {
|
||||||
|
@Override public String toString() {
|
||||||
|
return "desc";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ import org.elasticsearch.search.SearchHit;
|
||||||
import org.elasticsearch.search.sort.SortBuilders;
|
import org.elasticsearch.search.sort.SortBuilders;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.elasticsearch.test.integration.AbstractNodesTests;
|
import org.elasticsearch.test.integration.AbstractNodesTests;
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
import org.testng.annotations.AfterClass;
|
import org.testng.annotations.AfterClass;
|
||||||
import org.testng.annotations.BeforeClass;
|
import org.testng.annotations.BeforeClass;
|
||||||
import org.testng.annotations.Test;
|
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 {
|
@Test public void testSimpleSortsSingleShard() throws Exception {
|
||||||
testSimpleSorts(1);
|
testSimpleSorts(1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue