From fb647e9bf41c411c65b14a3987d9710965a5866a Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Thu, 10 Mar 2016 12:50:09 +0100 Subject: [PATCH] Add sanity checks and support for reverse in FieldSortBuilder.parse(...) After another round of input from @cbuescher this adds a few more sanity checks to request parsing. In addition adds (back) support for the reverse option. --- .../elasticsearch/search/sort/FieldSortBuilder.java | 13 +++++++++++-- .../search/sort/GeoDistanceSortBuilder.java | 2 +- .../elasticsearch/search/sort/ScoreSortBuilder.java | 3 +-- ...{SortBuilderTemp.java => SortBuilderParser.java} | 6 +++--- .../search/sort/AbstractSortTestCase.java | 3 ++- 5 files changed, 18 insertions(+), 9 deletions(-) rename core/src/main/java/org/elasticsearch/search/sort/{SortBuilderTemp.java => SortBuilderParser.java} (87%) diff --git a/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java index a11eefc1f66..4ead35fd96b 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java @@ -35,14 +35,14 @@ import java.util.Objects; /** * A sort builder to sort based on a document field. */ -public class FieldSortBuilder extends SortBuilder implements SortBuilderTemp { - public static final String NAME = "field_sort"; +public class FieldSortBuilder extends SortBuilder implements SortBuilderParser { static final FieldSortBuilder PROTOTYPE = new FieldSortBuilder(""); public static final String NAME = "field_sort"; public static final ParseField NESTED_PATH = new ParseField("nested_path"); public static final ParseField NESTED_FILTER = new ParseField("nested_filter"); public static final ParseField MISSING = new ParseField("missing"); public static final ParseField ORDER = new ParseField("order"); + public static final ParseField REVERSE = new ParseField("reverse"); public static final ParseField SORT_MODE = new ParseField("mode"); public static final ParseField UNMAPPED_TYPE = new ParseField("unmapped_type"); @@ -304,12 +304,19 @@ public class FieldSortBuilder extends SortBuilder implements SortBuilderTemp { +public class GeoDistanceSortBuilder extends SortBuilder implements SortBuilderParser { public static final String NAME = "_geo_distance"; public static final boolean DEFAULT_COERCE = false; public static final boolean DEFAULT_IGNORE_MALFORMED = false; diff --git a/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java index 6b1bc054ee7..c416965f38a 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.sort; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -35,7 +34,7 @@ import java.util.Objects; /** * A sort builder allowing to sort by score. */ -public class ScoreSortBuilder extends SortBuilder implements NamedWriteable, +public class ScoreSortBuilder extends SortBuilder implements SortBuilderParser, SortElementParserTemp { private static final String NAME = "_score"; diff --git a/core/src/main/java/org/elasticsearch/search/sort/SortBuilderTemp.java b/core/src/main/java/org/elasticsearch/search/sort/SortBuilderParser.java similarity index 87% rename from core/src/main/java/org/elasticsearch/search/sort/SortBuilderTemp.java rename to core/src/main/java/org/elasticsearch/search/sort/SortBuilderParser.java index e3e571c4410..90d54a50121 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/SortBuilderTemp.java +++ b/core/src/main/java/org/elasticsearch/search/sort/SortBuilderParser.java @@ -25,9 +25,9 @@ import org.elasticsearch.index.query.QueryParseContext; import java.io.IOException; -public interface SortBuilderTemp extends NamedWriteable, ToXContent { +public interface SortBuilderParser extends NamedWriteable, ToXContent { /** - * Creates a new item from the json held by the {@link SortBuilderTemp} + * Creates a new item from the json held by the {@link SortBuilderParser} * in {@link org.elasticsearch.common.xcontent.XContent} format * * @param context @@ -36,5 +36,5 @@ public interface SortBuilderTemp extends NamedWriteable * call * @return the new item */ - SortBuilderTemp fromXContent(QueryParseContext context, String elementName) throws IOException; + SortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException; } diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index 8cadfb24408..def78acf5af 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -42,7 +42,7 @@ import java.io.IOException; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; -public abstract class AbstractSortTestCase> extends ESTestCase { +public abstract class AbstractSortTestCase> extends ESTestCase { protected static NamedWriteableRegistry namedWriteableRegistry; @@ -54,6 +54,7 @@ public abstract class AbstractSortTestCase> extends namedWriteableRegistry = new NamedWriteableRegistry(); namedWriteableRegistry.registerPrototype(SortBuilder.class, GeoDistanceSortBuilder.PROTOTYPE); namedWriteableRegistry.registerPrototype(SortBuilder.class, ScoreSortBuilder.PROTOTYPE); + namedWriteableRegistry.registerPrototype(SortBuilder.class, FieldSortBuilder.PROTOTYPE); indicesQueriesRegistry = new SearchModule(Settings.EMPTY, namedWriteableRegistry).buildQueryParserRegistry(); }