From 35be46df71e5da1e840b678d8650fb012ae7305b Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 6 Apr 2011 02:25:56 +0300 Subject: [PATCH] Bug: Sort on a column of type 'short' throws an exception, closes #835. --- .../action/search/SearchResponse.java | 13 +++++++++++ .../common/xcontent/XContentBuilder.java | 12 ++++++++++ .../search/sort/SimpleSortTests.java | 22 +++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java index d62d844b36c..99f5ee72851 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.facet.Facets; import org.elasticsearch.search.internal.InternalSearchResponse; @@ -295,4 +296,16 @@ public class SearchResponse implements ActionResponse, ToXContent { } out.writeVLong(tookInMillis); } + + @Override public String toString() { + try { + XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); + builder.startObject(); + toXContent(builder, EMPTY_PARAMS); + builder.endObject(); + return builder.string(); + } catch (IOException e) { + return "{ \"error\" : \"" + e.getMessage() + "\"}"; + } + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 596be3136e7..1b0806698af 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -577,6 +577,10 @@ public final class XContentBuilder { field(name, ((Integer) value).intValue()); } else if (type == Long.class) { field(name, ((Long) value).longValue()); + } else if (type == Short.class) { + field(name, ((Short) value).shortValue()); + } else if (type == Byte.class) { + field(name, ((Byte) value).byteValue()); } else if (type == Boolean.class) { field(name, ((Boolean) value).booleanValue()); } else if (type == Date.class) { @@ -624,6 +628,10 @@ public final class XContentBuilder { field(name, ((Integer) value).intValue()); } else if (type == Long.class) { field(name, ((Long) value).longValue()); + } else if (type == Short.class) { + field(name, ((Short) value).shortValue()); + } else if (type == Byte.class) { + field(name, ((Byte) value).byteValue()); } else if (type == Boolean.class) { field(name, ((Boolean) value).booleanValue()); } else if (type == Date.class) { @@ -668,6 +676,10 @@ public final class XContentBuilder { value(((Integer) value).intValue()); } else if (type == Long.class) { value(((Long) value).longValue()); + } else if (type == Short.class) { + value(((Short) value).shortValue()); + } else if (type == Byte.class) { + value(((Byte) value).byteValue()); } else if (type == Boolean.class) { value((Boolean) value); } else if (type == byte[].class) { 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 58b0df12711..edb195f6707 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 @@ -189,6 +189,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(searchResponse.hits().getAt(i).sortValues()[0].toString(), equalTo(new String(new char[]{(char) (97 + (9 - i)), (char) (97 + (9 - i))}))); } + assertThat(searchResponse.toString(), not(containsString("error"))); + // BYTE searchResponse = client.prepareSearch() @@ -217,6 +219,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).byteValue(), equalTo((byte) (9 - i))); } + assertThat(searchResponse.toString(), not(containsString("error"))); + // SHORT searchResponse = client.prepareSearch() @@ -245,6 +249,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).shortValue(), equalTo((short) (9 - i))); } + assertThat(searchResponse.toString(), not(containsString("error"))); + // INTEGER searchResponse = client.prepareSearch() @@ -260,6 +266,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).intValue(), equalTo((int) i)); } + assertThat(searchResponse.toString(), not(containsString("error"))); + searchResponse = client.prepareSearch() .setQuery(matchAllQuery()) .setSize(10) @@ -273,6 +281,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).intValue(), equalTo((int) (9 - i))); } + assertThat(searchResponse.toString(), not(containsString("error"))); + // LONG searchResponse = client.prepareSearch() @@ -288,6 +298,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).longValue(), equalTo((long) i)); } + assertThat(searchResponse.toString(), not(containsString("error"))); + searchResponse = client.prepareSearch() .setQuery(matchAllQuery()) .setSize(10) @@ -301,6 +313,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).longValue(), equalTo((long) (9 - i))); } + assertThat(searchResponse.toString(), not(containsString("error"))); + // FLOAT searchResponse = client.prepareSearch() @@ -316,6 +330,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d)); } + assertThat(searchResponse.toString(), not(containsString("error"))); + searchResponse = client.prepareSearch() .setQuery(matchAllQuery()) .setSize(10) @@ -329,6 +345,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d)); } + assertThat(searchResponse.toString(), not(containsString("error"))); + // DOUBLE searchResponse = client.prepareSearch() @@ -344,6 +362,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d)); } + assertThat(searchResponse.toString(), not(containsString("error"))); + searchResponse = client.prepareSearch() .setQuery(matchAllQuery()) .setSize(10) @@ -356,6 +376,8 @@ public class SimpleSortTests extends AbstractNodesTests { assertThat(searchResponse.hits().getAt(i).id(), equalTo(Integer.toString(9 - i))); assertThat(((Number) searchResponse.hits().getAt(i).sortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d)); } + + assertThat(searchResponse.toString(), not(containsString("error"))); } @Test public void testDocumentsWithNullValue() throws Exception {