From cb51d3c5762e6f2808f9ab358943b3dc85e9bd95 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 2 Aug 2011 03:08:48 +0300 Subject: [PATCH] Sorting on _score in the URI format is reversed, closes #1191. --- .../search/sort/FieldSortBuilder.java | 4 +-- .../elasticsearch/search/sort/SortOrder.java | 12 +++++-- .../search/sort/SimpleSortTests.java | 36 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java index 3cd3762651c..8ca9d706d47 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java @@ -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); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/SortOrder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/SortOrder.java index 750cb601123..757f0fbc939 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/SortOrder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/SortOrder.java @@ -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"; + } + } } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java index 37674b53a35..8bc68550a8d 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java @@ -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); }