From eb338f46f7f3e7220282b3b8b55b7a2a56e5b408 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 8 Apr 2016 12:47:34 -0400 Subject: [PATCH] Remove PROTOTYPE from QueryBuilders Relates to #17085 --- .../action/explain/ExplainRequest.java | 17 +- .../elasticsearch/action/get/GetRequest.java | 6 +- .../action/get/MultiGetRequest.java | 4 +- .../action/support/ToXContentToBytes.java | 1 + .../elasticsearch/common/geo/GeoDistance.java | 2 +- .../common/io/stream/StreamInput.java | 17 ++ .../common/io/stream/StreamOutput.java | 20 +++ .../search/function/CombineFunction.java | 7 +- .../function/FiltersFunctionScoreQuery.java | 7 +- .../elasticsearch/common/unit/Fuzziness.java | 30 ++-- .../org/elasticsearch/index/VersionType.java | 2 +- .../index/query/AbstractQueryBuilder.java | 35 ++--- .../index/query/BaseTermQueryBuilder.java | 28 ++-- .../index/query/BoolQueryBuilder.java | 61 ++++---- .../index/query/BoostingQueryBuilder.java | 40 ++--- .../index/query/CommonTermsQueryBuilder.java | 58 +++---- .../query/ConstantScoreQueryBuilder.java | 33 ++-- .../index/query/DisMaxQueryBuilder.java | 39 ++--- .../index/query/EmptyQueryBuilder.java | 29 ++-- .../index/query/ExistsQueryBuilder.java | 24 +-- .../query/FieldMaskingSpanQueryBuilder.java | 33 ++-- .../index/query/FuzzyQueryBuilder.java | 50 +++--- .../query/GeoBoundingBoxQueryBuilder.java | 43 +++--- .../index/query/GeoDistanceQueryBuilder.java | 47 +++--- .../query/GeoDistanceRangeQueryBuilder.java | 61 ++++---- .../index/query/GeoPolygonQueryBuilder.java | 53 +++---- .../index/query/GeoShapeQueryBuilder.java | 89 +++++------ .../index/query/GeoValidationMethod.java | 2 +- .../index/query/GeohashCellQuery.java | 44 +++--- .../index/query/HasChildQueryBuilder.java | 55 +++---- .../index/query/HasParentQueryBuilder.java | 45 +++--- .../index/query/IdsQueryBuilder.java | 30 ++-- .../index/query/IndicesQueryBuilder.java | 32 ++-- .../index/query/MatchAllQueryBuilder.java | 26 ++-- .../index/query/MatchNoneQueryBuilder.java | 25 +-- .../query/MatchPhrasePrefixQueryBuilder.java | 41 ++--- .../index/query/MatchPhraseQueryBuilder.java | 37 ++--- .../index/query/MatchQueryBuilder.java | 99 ++++++------ .../index/query/MoreLikeThisQueryBuilder.java | 108 ++++++------- .../index/query/MultiMatchQueryBuilder.java | 119 +++++++------- .../index/query/NestedQueryBuilder.java | 46 +++--- .../elasticsearch/index/query/Operator.java | 2 +- .../index/query/ParentIdQueryBuilder.java | 30 ++-- .../index/query/PercolatorQueryBuilder.java | 89 ++++------- .../index/query/PrefixQueryBuilder.java | 32 ++-- .../index/query/QueryStringQueryBuilder.java | 146 +++++++++--------- .../index/query/RangeQueryBuilder.java | 70 ++++----- .../index/query/RegexpQueryBuilder.java | 40 ++--- .../index/query/ScriptQueryBuilder.java | 25 +-- .../index/query/SimpleQueryStringBuilder.java | 85 +++++----- .../query/SpanContainingQueryBuilder.java | 36 ++--- .../index/query/SpanFirstQueryBuilder.java | 35 +++-- .../query/SpanMultiTermQueryBuilder.java | 32 ++-- .../index/query/SpanNearQueryBuilder.java | 39 +++-- .../index/query/SpanNotQueryBuilder.java | 45 +++--- .../index/query/SpanOrQueryBuilder.java | 32 ++-- .../index/query/SpanTermQueryBuilder.java | 13 +- .../index/query/SpanWithinQueryBuilder.java | 36 ++--- .../index/query/TemplateQueryBuilder.java | 26 ++-- .../index/query/TermQueryBuilder.java | 14 +- .../index/query/TermsQueryBuilder.java | 44 +++--- .../index/query/TypeQueryBuilder.java | 25 +-- .../index/query/WildcardQueryBuilder.java | 34 ++-- .../index/query/WrapperQueryBuilder.java | 25 +-- .../FunctionScoreQueryBuilder.java | 72 +++------ .../index/query/support/InnerHitBuilder.java | 82 +++++----- .../index/search/MatchQuery.java | 18 +-- .../elasticsearch/indices/TermsLookup.java | 46 +++--- .../elasticsearch/search/SearchModule.java | 117 ++++++-------- .../GeoDistanceAggregatorBuilder.java | 2 +- .../tophits/TopHitsAggregatorBuilder.java | 4 +- .../search/builder/SearchSourceBuilder.java | 4 +- .../fetch/source/FetchSourceContext.java | 21 +-- .../search/sort/GeoDistanceSortBuilder.java | 2 +- .../common/geo/GeoDistanceTests.java | 4 +- .../common/io/stream/BytesStreamsTests.java | 7 +- .../common/unit/FuzzinessTests.java | 2 +- .../index/query/AbstractQueryTestCase.java | 6 +- .../index/query/CombineFunctionTests.java | 12 +- .../query/ConstantScoreQueryBuilderTests.java | 7 +- .../index/query/ExistsQueryBuilderTests.java | 12 +- .../GeoBoundingBoxQueryBuilderTests.java | 14 +- .../query/GeoDistanceQueryBuilderTests.java | 2 +- .../index/query/OperatorTests.java | 4 +- .../query/ParentIdQueryBuilderTests.java | 1 - .../query/QueryStringQueryBuilderTests.java | 7 +- .../index/query/RangeQueryBuilderTests.java | 12 +- .../index/query/ScoreModeTests.java | 12 +- .../index/query/ScriptQueryBuilderTests.java | 7 +- .../query/SimpleQueryStringBuilderTests.java | 7 +- .../query/SpanMultiTermQueryBuilderTests.java | 7 +- .../index/query/SpanOrQueryBuilderTests.java | 7 +- .../query/TemplateQueryBuilderTests.java | 7 +- .../query/plugin/DummyQueryParserPlugin.java | 27 ++-- .../indices/TermsLookupTests.java | 2 +- .../search/SearchModuleTests.java | 2 +- 96 files changed, 1406 insertions(+), 1630 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java b/core/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java index 08c188ae998..a974a22e89b 100644 --- a/core/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java +++ b/core/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java @@ -167,11 +167,8 @@ public class ExplainRequest extends SingleShardRequest { preference = in.readOptionalString(); query = in.readQuery(); filteringAlias = in.readStringArray(); - if (in.readBoolean()) { - fields = in.readStringArray(); - } - - fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); + fields = in.readOptionalStringArray(); + fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new); nowInMillis = in.readVLong(); } @@ -184,14 +181,8 @@ public class ExplainRequest extends SingleShardRequest { out.writeOptionalString(preference); out.writeQuery(query); out.writeStringArray(filteringAlias); - if (fields != null) { - out.writeBoolean(true); - out.writeStringArray(fields); - } else { - out.writeBoolean(false); - } - - FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); + out.writeOptionalStringArray(fields); + out.writeOptionalStreamable(fetchSourceContext); out.writeVLong(nowInMillis); } } diff --git a/core/src/main/java/org/elasticsearch/action/get/GetRequest.java b/core/src/main/java/org/elasticsearch/action/get/GetRequest.java index b2f3bc2b018..4b27283d000 100644 --- a/core/src/main/java/org/elasticsearch/action/get/GetRequest.java +++ b/core/src/main/java/org/elasticsearch/action/get/GetRequest.java @@ -287,8 +287,7 @@ public class GetRequest extends SingleShardRequest implements Realti this.versionType = VersionType.fromValue(in.readByte()); this.version = in.readLong(); - - fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); + fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new); } @Override @@ -319,8 +318,7 @@ public class GetRequest extends SingleShardRequest implements Realti out.writeBoolean(ignoreErrorsOnGeneratedFields); out.writeByte(versionType.getValue()); out.writeLong(version); - - FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); + out.writeOptionalStreamable(fetchSourceContext); } @Override diff --git a/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java b/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java index f67e2b27b56..5688cd9983c 100644 --- a/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java +++ b/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java @@ -198,7 +198,7 @@ public class MultiGetRequest extends ActionRequest implements I version = in.readLong(); versionType = VersionType.fromValue(in.readByte()); - fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); + fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new); } @Override @@ -220,7 +220,7 @@ public class MultiGetRequest extends ActionRequest implements I out.writeLong(version); out.writeByte(versionType.getValue()); - FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); + out.writeOptionalStreamable(fetchSourceContext); } @Override diff --git a/core/src/main/java/org/elasticsearch/action/support/ToXContentToBytes.java b/core/src/main/java/org/elasticsearch/action/support/ToXContentToBytes.java index b78e291ffaa..445967252a2 100644 --- a/core/src/main/java/org/elasticsearch/action/support/ToXContentToBytes.java +++ b/core/src/main/java/org/elasticsearch/action/support/ToXContentToBytes.java @@ -75,6 +75,7 @@ public abstract class ToXContentToBytes implements ToXContent { toXContent(builder, EMPTY_PARAMS); return builder.string(); } catch (Exception e) { + // So we have a stack trace logged somewhere return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}"; } } diff --git a/core/src/main/java/org/elasticsearch/common/geo/GeoDistance.java b/core/src/main/java/org/elasticsearch/common/geo/GeoDistance.java index b9edb9975b4..b08ee06a0d1 100644 --- a/core/src/main/java/org/elasticsearch/common/geo/GeoDistance.java +++ b/core/src/main/java/org/elasticsearch/common/geo/GeoDistance.java @@ -136,7 +136,7 @@ public enum GeoDistance implements Writeable { return GeoDistance.values()[ord]; } - public static GeoDistance readGeoDistanceFrom(StreamInput in) throws IOException { + public static GeoDistance readFromStream(StreamInput in) throws IOException { return DEFAULT.readFrom(in); } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 0963a20c61b..536d09d3dde 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -511,6 +511,23 @@ public abstract class StreamInput extends InputStream { return new GeoPoint(readDouble(), readDouble()); } + /** + * Read a {@linkplain DateTimeZone}. + */ + public DateTimeZone readTimeZone() throws IOException { + return DateTimeZone.forID(readString()); + } + + /** + * Read an optional {@linkplain DateTimeZone}. + */ + public DateTimeZone readOptionalTimeZone() throws IOException { + if (readBoolean()) { + return DateTimeZone.forID(readString()); + } + return null; + } + public int[] readIntArray() throws IOException { int length = readVInt(); int[] values = new int[length]; diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 14cfecd5c19..ed5fa3c20e9 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -44,6 +44,7 @@ import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.suggest.SuggestionBuilder; import org.elasticsearch.search.suggest.phrase.SmoothingModel; import org.elasticsearch.tasks.Task; +import org.joda.time.DateTimeZone; import org.joda.time.ReadableInstant; import java.io.EOFException; @@ -759,6 +760,25 @@ public abstract class StreamOutput extends OutputStream { writeDouble(geoPoint.lon()); } + /** + * Write a {@linkplain DateTimeZone} to the stream. + */ + public void writeTimeZone(DateTimeZone timeZone) throws IOException { + writeString(timeZone.getID()); + } + + /** + * Write an optional {@linkplain DateTimeZone} to the stream. + */ + public void writeOptionalTimeZone(DateTimeZone timeZone) throws IOException { + if (timeZone == null) { + writeBoolean(false); + } else { + writeBoolean(true); + writeTimeZone(timeZone); + } + } + /** * Writes a list of {@link Writeable} objects */ diff --git a/core/src/main/java/org/elasticsearch/common/lucene/search/function/CombineFunction.java b/core/src/main/java/org/elasticsearch/common/lucene/search/function/CombineFunction.java index 3876e6f3f9c..e8a2ef0e8e4 100644 --- a/core/src/main/java/org/elasticsearch/common/lucene/search/function/CombineFunction.java +++ b/core/src/main/java/org/elasticsearch/common/lucene/search/function/CombineFunction.java @@ -146,8 +146,7 @@ public enum CombineFunction implements Writeable { out.writeVInt(this.ordinal()); } - @Override - public CombineFunction readFrom(StreamInput in) throws IOException { + public static CombineFunction readFromStream(StreamInput in) throws IOException { int ordinal = in.readVInt(); if (ordinal < 0 || ordinal >= values().length) { throw new IOException("Unknown CombineFunction ordinal [" + ordinal + "]"); @@ -155,10 +154,6 @@ public enum CombineFunction implements Writeable { return values()[ordinal]; } - public static CombineFunction readCombineFunctionFrom(StreamInput in) throws IOException { - return CombineFunction.MULTIPLY.readFrom(in); - } - public static CombineFunction fromString(String combineFunction) { return valueOf(combineFunction.toUpperCase(Locale.ROOT)); } diff --git a/core/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java b/core/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java index 54e8c0e3488..22b1a71a1fe 100644 --- a/core/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java +++ b/core/src/main/java/org/elasticsearch/common/lucene/search/function/FiltersFunctionScoreQuery.java @@ -83,8 +83,7 @@ public class FiltersFunctionScoreQuery extends Query { out.writeVInt(this.ordinal()); } - @Override - public ScoreMode readFrom(StreamInput in) throws IOException { + public static ScoreMode readFromStream(StreamInput in) throws IOException { int ordinal = in.readVInt(); if (ordinal < 0 || ordinal >= values().length) { throw new IOException("Unknown ScoreMode ordinal [" + ordinal + "]"); @@ -92,10 +91,6 @@ public class FiltersFunctionScoreQuery extends Query { return values()[ordinal]; } - public static ScoreMode readScoreModeFrom(StreamInput in) throws IOException { - return ScoreMode.MULTIPLY.readFrom(in); - } - public static ScoreMode fromString(String scoreMode) { return valueOf(scoreMode.toUpperCase(Locale.ROOT)); } diff --git a/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java b/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java index 24a727691cc..9068c5ffa50 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java +++ b/core/src/main/java/org/elasticsearch/common/unit/Fuzziness.java @@ -47,10 +47,6 @@ public final class Fuzziness implements ToXContent, Writeable { private final String fuzziness; - /** the prototype constant is intended for deserialization when used with - * {@link org.elasticsearch.common.io.stream.StreamableReader#readFrom(StreamInput)} */ - static final Fuzziness PROTOTYPE = AUTO; - private Fuzziness(int fuzziness) { if (fuzziness != 0 && fuzziness != 1 && fuzziness != 2) { throw new IllegalArgumentException("Valid edit distances are [0, 1, 2] but was [" + fuzziness + "]"); @@ -65,6 +61,18 @@ public final class Fuzziness implements ToXContent, Writeable { this.fuzziness = fuzziness.toUpperCase(Locale.ROOT); } + /** + * Read from a stream. + */ + public Fuzziness(StreamInput in) throws IOException { + fuzziness = in.readString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeString(fuzziness); + } + /** * Creates a {@link Fuzziness} instance from an edit distance. The value must be one of [0, 1, 2] * @@ -237,18 +245,4 @@ public final class Fuzziness implements ToXContent, Writeable { public int hashCode() { return fuzziness.hashCode(); } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(fuzziness); - } - - @Override - public Fuzziness readFrom(StreamInput in) throws IOException { - return new Fuzziness(in.readString()); - } - - public static Fuzziness readFuzzinessFrom(StreamInput in) throws IOException { - return PROTOTYPE.readFrom(in); - } } diff --git a/core/src/main/java/org/elasticsearch/index/VersionType.java b/core/src/main/java/org/elasticsearch/index/VersionType.java index 30bc0effd65..7cf9adf1a6a 100644 --- a/core/src/main/java/org/elasticsearch/index/VersionType.java +++ b/core/src/main/java/org/elasticsearch/index/VersionType.java @@ -381,7 +381,7 @@ public enum VersionType implements Writeable { throw new IllegalArgumentException("No version type match [" + value + "]"); } - public static VersionType readVersionTypeFrom(StreamInput in) throws IOException { + public static VersionType readFromStream(StreamInput in) throws IOException { int ordinal = in.readVInt(); assert (ordinal == 0 || ordinal == 1 || ordinal == 2 || ordinal == 3); return VersionType.values()[ordinal]; diff --git a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index 39c835e4b09..6c624665a99 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -56,6 +56,20 @@ public abstract class AbstractQueryBuilder> super(XContentType.JSON); } + protected AbstractQueryBuilder(StreamInput in) throws IOException { + boost = in.readFloat(); + queryName = in.readOptionalString(); + } + + @Override + public final void writeTo(StreamOutput out) throws IOException { + out.writeFloat(boost); + out.writeOptionalString(queryName); + doWriteTo(out); + } + + protected abstract void doWriteTo(StreamOutput out) throws IOException; + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); @@ -143,25 +157,6 @@ public abstract class AbstractQueryBuilder> return (QB) this; } - @Override - public final QB readFrom(StreamInput in) throws IOException { - QB queryBuilder = doReadFrom(in); - queryBuilder.boost = in.readFloat(); - queryBuilder.queryName = in.readOptionalString(); - return queryBuilder; - } - - protected abstract QB doReadFrom(StreamInput in) throws IOException; - - @Override - public final void writeTo(StreamOutput out) throws IOException { - doWriteTo(out); - out.writeFloat(boost); - out.writeOptionalString(queryName); - } - - protected abstract void doWriteTo(StreamOutput out) throws IOException; - protected final QueryValidationException addValidationError(String validationError, QueryValidationException validationException) { return QueryValidationException.addValidationError(getName(), validationError, validationException); } @@ -261,7 +256,7 @@ public abstract class AbstractQueryBuilder> @Override public final QueryBuilder rewrite(QueryRewriteContext queryShardContext) throws IOException { - QueryBuilder rewritten = doRewrite(queryShardContext); + QueryBuilder rewritten = doRewrite(queryShardContext); if (rewritten == this) { return rewritten; } diff --git a/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java index f1ce96fa8c1..d8ca025dafa 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java @@ -119,6 +119,21 @@ public abstract class BaseTermQueryBuilder> this.value = convertToBytesRefIfString(value); } + /** + * Read from a stream. + */ + protected BaseTermQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + value = in.readGenericValue(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeGenericValue(value); + } + /** Returns the field name used in this query. */ public String fieldName() { return this.fieldName; @@ -152,17 +167,4 @@ public abstract class BaseTermQueryBuilder> return Objects.equals(fieldName, other.fieldName) && Objects.equals(value, other.value); } - - @Override - protected final QB doReadFrom(StreamInput in) throws IOException { - return createBuilder(in.readString(), in.readGenericValue()); - } - - protected abstract QB createBuilder(String fieldName, Object value); - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeGenericValue(value); - } } diff --git a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java index 8df53dc0386..b09c15f6711 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -47,7 +47,6 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "bool"; public static final ParseField QUERY_NAME_FIELD = new ParseField(BoolQueryBuilder.NAME); - public static final BoolQueryBuilder PROTOTYPE = new BoolQueryBuilder(); public static final boolean ADJUST_PURE_NEGATIVE_DEFAULT = true; public static final boolean DISABLE_COORD_DEFAULT = false; @@ -76,6 +75,37 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { private String minimumShouldMatch; + /** + * Build an empty bool query. + */ + public BoolQueryBuilder() { + } + + /** + * Read from a stream. + */ + public BoolQueryBuilder(StreamInput in) throws IOException { + super(in); + mustClauses.addAll(readQueries(in)); + mustNotClauses.addAll(readQueries(in)); + shouldClauses.addAll(readQueries(in)); + filterClauses.addAll(readQueries(in)); + adjustPureNegative = in.readBoolean(); + disableCoord = in.readBoolean(); + minimumShouldMatch = in.readOptionalString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + writeQueries(out, mustClauses); + writeQueries(out, mustNotClauses); + writeQueries(out, shouldClauses); + writeQueries(out, filterClauses); + out.writeBoolean(adjustPureNegative); + out.writeBoolean(disableCoord); + out.writeOptionalString(minimumShouldMatch); + } + /** * Adds a query that must appear in the matching documents and will * contribute to scoring. No null value allowed. @@ -441,35 +471,6 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { Objects.equals(filterClauses, other.filterClauses); } - @Override - protected BoolQueryBuilder doReadFrom(StreamInput in) throws IOException { - BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); - List> queryBuilders = readQueries(in); - boolQueryBuilder.mustClauses.addAll(queryBuilders); - queryBuilders = readQueries(in); - boolQueryBuilder.mustNotClauses.addAll(queryBuilders); - queryBuilders = readQueries(in); - boolQueryBuilder.shouldClauses.addAll(queryBuilders); - queryBuilders = readQueries(in); - boolQueryBuilder.filterClauses.addAll(queryBuilders); - boolQueryBuilder.adjustPureNegative = in.readBoolean(); - boolQueryBuilder.disableCoord = in.readBoolean(); - boolQueryBuilder.minimumShouldMatch = in.readOptionalString(); - return boolQueryBuilder; - - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - writeQueries(out, mustClauses); - writeQueries(out, mustNotClauses); - writeQueries(out, shouldClauses); - writeQueries(out, filterClauses); - out.writeBoolean(adjustPureNegative); - out.writeBoolean(disableCoord); - out.writeOptionalString(minimumShouldMatch); - } - @Override protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { BoolQueryBuilder newBuilder = new BoolQueryBuilder(); diff --git a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java index 9e2ce2f1cb7..abfa400da69 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java @@ -47,15 +47,14 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder positiveQuery; - private final QueryBuilder negativeQuery; + private final QueryBuilder negativeQuery; private float negativeBoost = -1; @@ -66,7 +65,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder positiveQuery, QueryBuilder negativeQuery) { if (positiveQuery == null) { throw new IllegalArgumentException("inner clause [positive] cannot be null."); } @@ -77,6 +76,23 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { QueryBuilder positiveQuery = this.positiveQuery.rewrite(queryRewriteContext); diff --git a/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java index 56bc7a38591..be14df7f283 100644 --- a/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java @@ -60,7 +60,6 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder filterBuilder; /** * A query that wraps another query and simply returns a constant score equal to the @@ -51,17 +50,30 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder filterBuilder) { if (filterBuilder == null) { throw new IllegalArgumentException("inner clause [filter] cannot be null."); } this.filterBuilder = filterBuilder; } + /** + * Read from a stream. + */ + public ConstantScoreQueryBuilder(StreamInput in) throws IOException { + super(in); + filterBuilder = in.readQuery(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(filterBuilder); + } + /** * @return the query that was wrapped in this constant score query */ - public QueryBuilder innerQuery() { + public QueryBuilder innerQuery() { return this.filterBuilder; } @@ -149,20 +161,9 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { - QueryBuilder rewrite = filterBuilder.rewrite(queryRewriteContext); + QueryBuilder rewrite = filterBuilder.rewrite(queryRewriteContext); if (rewrite != filterBuilder) { return new ConstantScoreQueryBuilder(rewrite); } diff --git a/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryBuilder.java index 73881c26696..cf615fdba3e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryBuilder.java @@ -43,17 +43,35 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder public static final String NAME = "dis_max"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final DisMaxQueryBuilder PROTOTYPE = new DisMaxQueryBuilder(); + + /** Default multiplication factor for breaking ties in document scores.*/ + public static final float DEFAULT_TIE_BREAKER = 0.0f; private static final ParseField TIE_BREAKER_FIELD = new ParseField("tie_breaker"); private static final ParseField QUERIES_FIELD = new ParseField("queries"); private final List> queries = new ArrayList<>(); - /** Default multiplication factor for breaking ties in document scores.*/ - public static float DEFAULT_TIE_BREAKER = 0.0f; private float tieBreaker = DEFAULT_TIE_BREAKER; + public DisMaxQueryBuilder() { + } + + /** + * Read from a stream. + */ + public DisMaxQueryBuilder(StreamInput in) throws IOException { + super(in); + queries.addAll(readQueries(in)); + tieBreaker = in.readFloat(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + writeQueries(out, queries); + out.writeFloat(tieBreaker); + } + /** * Add a sub-query to this disjunction. */ @@ -176,21 +194,6 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder return new DisjunctionMaxQuery(luceneQueries, tieBreaker); } - @Override - protected DisMaxQueryBuilder doReadFrom(StreamInput in) throws IOException { - DisMaxQueryBuilder disMax = new DisMaxQueryBuilder(); - List> queryBuilders = readQueries(in); - disMax.queries.addAll(queryBuilders); - disMax.tieBreaker = in.readFloat(); - return disMax; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - writeQueries(out, queries); - out.writeFloat(tieBreaker); - } - @Override protected int doHashCode() { return Objects.hash(queries, tieBreaker); diff --git a/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java index e810292a95c..22493688254 100644 --- a/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java @@ -34,10 +34,25 @@ import java.io.IOException; public final class EmptyQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "empty_query"; - - /** the one and only empty query builder */ public static final EmptyQueryBuilder PROTOTYPE = new EmptyQueryBuilder(); + /** + * Construct an empty query. This query can *technically* be named and given a boost. + */ + public EmptyQueryBuilder() { + } + + /** + * Read from a stream. + */ + public EmptyQueryBuilder(StreamInput in) throws IOException { + super(in); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + } + @Override public String getWriteableName() { return NAME; @@ -57,16 +72,6 @@ public final class EmptyQueryBuilder extends AbstractQueryBuilder public static final String NAME = "exists"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final ExistsQueryBuilder PROTOTYPE = new ExistsQueryBuilder("field"); public static final ParseField FIELD_FIELD = new ParseField("field"); @@ -58,6 +57,19 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder this.fieldName = fieldName; } + /** + * Read from a stream. + */ + public ExistsQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + } + /** * @return the field name that has to exist for this query to match */ @@ -152,16 +164,6 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder return Objects.equals(fieldName, other.fieldName); } - @Override - protected ExistsQueryBuilder doReadFrom(StreamInput in) throws IOException { - return new ExistsQueryBuilder(in.readString()); - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - } - @Override public String getWriteableName() { return NAME; diff --git a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java index 50d646bdd00..40f14574981 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java @@ -39,13 +39,11 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder queryBuilder; private final String fieldName; @@ -55,7 +53,7 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder queryBuilder, String fieldName) { if (Strings.isEmpty(fieldName)) { throw new IllegalArgumentException("field name is null or empty"); } @@ -66,6 +64,21 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder) in.readQuery(); + fieldName = in.readString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(queryBuilder); + out.writeString(fieldName); + } + /** * @return the field name for this query */ @@ -153,18 +166,6 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder i public static final String NAME = "fuzzy"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final FuzzyQueryBuilder PROTOTYPE = new FuzzyQueryBuilder(); /** Default maximum edit distance. Defaults to AUTO. */ public static final Fuzziness DEFAULT_FUZZINESS = Fuzziness.AUTO; @@ -163,10 +162,29 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i this.value = convertToBytesRefIfString(value); } - private FuzzyQueryBuilder() { - // for prototype - this.fieldName = null; - this.value = null; + /** + * Read from a stream. + */ + public FuzzyQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + value = in.readGenericValue(); + fuzziness = new Fuzziness(in); + prefixLength = in.readVInt(); + maxExpansions = in.readVInt(); + transpositions = in.readBoolean(); + rewrite = in.readOptionalString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(this.fieldName); + out.writeGenericValue(this.value); + this.fuzziness.writeTo(out); + out.writeVInt(this.prefixLength); + out.writeVInt(this.maxExpansions); + out.writeBoolean(this.transpositions); + out.writeOptionalString(this.rewrite); } public String fieldName() { @@ -336,28 +354,6 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i return query; } - @Override - protected FuzzyQueryBuilder doReadFrom(StreamInput in) throws IOException { - FuzzyQueryBuilder fuzzyQueryBuilder = new FuzzyQueryBuilder(in.readString(), in.readGenericValue()); - fuzzyQueryBuilder.fuzziness = Fuzziness.readFuzzinessFrom(in); - fuzzyQueryBuilder.prefixLength = in.readVInt(); - fuzzyQueryBuilder.maxExpansions = in.readVInt(); - fuzzyQueryBuilder.transpositions = in.readBoolean(); - fuzzyQueryBuilder.rewrite = in.readOptionalString(); - return fuzzyQueryBuilder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(this.fieldName); - out.writeGenericValue(this.value); - this.fuzziness.writeTo(out); - out.writeVInt(this.prefixLength); - out.writeVInt(this.maxExpansions); - out.writeBoolean(this.transpositions); - out.writeOptionalString(this.rewrite); - } - @Override protected int doHashCode() { return Objects.hash(fieldName, value, fuzziness, prefixLength, maxExpansions, transpositions, rewrite); diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java index 07807ac49cf..fab49cb2ed9 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java @@ -57,8 +57,6 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder PROTO_SHAPE = Arrays.asList(new GeoPoint(1.0, 1.0), new GeoPoint(1.0, 2.0), - new GeoPoint(2.0, 1.0)); - - public static final GeoPolygonQueryBuilder PROTOTYPE = new GeoPolygonQueryBuilder("field", PROTO_SHAPE); - private static final ParseField COERCE_FIELD = new ParseField("coerce", "normalize"); private static final ParseField IGNORE_MALFORMED_FIELD = new ParseField("ignore_malformed"); private static final ParseField VALIDATION_METHOD = new ParseField("validation_method"); @@ -90,6 +84,30 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder(size); + for (int i = 0; i < size; i++) { + shell.add(in.readGeoPoint()); + } + validationMethod = GeoValidationMethod.readFromStream(in); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeVInt(shell.size()); + for (GeoPoint point : shell) { + out.writeGeoPoint(point); + } + validationMethod.writeTo(out); + } + public String fieldName() { return fieldName; } @@ -268,29 +286,6 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder shell = new ArrayList<>(); - int size = in.readVInt(); - for (int i = 0; i < size; i++) { - shell.add(in.readGeoPoint()); - } - GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell); - builder.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in); - return builder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeVInt(shell.size()); - for (GeoPoint point : shell) { - out.writeGeoPoint(point); - } - validationMethod.writeTo(out); - } - @Override protected boolean doEquals(GeoPolygonQueryBuilder other) { return Objects.equals(validationMethod, other.validationMethod) diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java index e4f95f1cee0..9a0d4c36608 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.ShapesAvailability; import org.elasticsearch.common.geo.SpatialStrategy; -import org.elasticsearch.common.geo.builders.PointBuilder; import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -61,8 +60,6 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder{ public static final GeoValidationMethod DEFAULT = STRICT; public static final boolean DEFAULT_LENIENT_PARSING = (DEFAULT != STRICT); - public static GeoValidationMethod readGeoValidationMethodFrom(StreamInput in) throws IOException { + public static GeoValidationMethod readFromStream(StreamInput in) throws IOException { return GeoValidationMethod.values()[in.readVInt()]; } diff --git a/core/src/main/java/org/elasticsearch/index/query/GeohashCellQuery.java b/core/src/main/java/org/elasticsearch/index/query/GeohashCellQuery.java index c34d423ca32..d2338b36135 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeohashCellQuery.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeohashCellQuery.java @@ -98,7 +98,6 @@ public class GeohashCellQuery { * false. */ public static class Builder extends AbstractQueryBuilder { - public static final Builder PROTOTYPE = new Builder("field", new GeoPoint()); // we need to store the geohash rather than the corresponding point, // because a transformation from a geohash to a point an back to the // geohash will extend the accuracy of the hash to max precision @@ -128,6 +127,25 @@ public class GeohashCellQuery { this.neighbors = neighbors; } + /** + * Read from a stream. + */ + public Builder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + geohash = in.readString(); + levels = in.readOptionalVInt(); + neighbors = in.readBoolean(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeString(geohash); + out.writeOptionalVInt(levels); + out.writeBoolean(neighbors); + } + public Builder point(GeoPoint point) { this.geohash = point.getGeohash(); return this; @@ -307,30 +325,6 @@ public class GeohashCellQuery { return builder; } - @Override - protected Builder doReadFrom(StreamInput in) throws IOException { - String field = in.readString(); - String geohash = in.readString(); - Builder builder = new Builder(field, geohash); - if (in.readBoolean()) { - builder.precision(in.readVInt()); - } - builder.neighbors(in.readBoolean()); - return builder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeString(geohash); - boolean hasLevels = levels != null; - out.writeBoolean(hasLevels); - if (hasLevels) { - out.writeVInt(levels); - } - out.writeBoolean(neighbors); - } - @Override protected boolean doEquals(Builder other) { return Objects.equals(fieldName, other.fieldName) diff --git a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java index 2ba2623739a..6e3d170f474 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java @@ -68,8 +68,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { QueryBuilder rewrite = query.rewrite(queryRewriteContext); diff --git a/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java index 2104fc23150..76507e4e7eb 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java @@ -46,7 +46,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "ids"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final IdsQueryBuilder PROTOTYPE = new IdsQueryBuilder(); private static final ParseField TYPE_FIELD = new ParseField("type", "types", "_type"); private static final ParseField VALUES_FIELD = new ParseField("values"); @@ -58,7 +57,6 @@ public class IdsQueryBuilder extends AbstractQueryBuilder { private final String[] types; - /** * Creates a new IdsQueryBuilder without providing the types of the documents to look for */ @@ -76,6 +74,21 @@ public class IdsQueryBuilder extends AbstractQueryBuilder { this.types = types; } + /** + * Read from a stream. + */ + public IdsQueryBuilder(StreamInput in) throws IOException { + super(in); + types = in.readStringArray(); + Collections.addAll(ids, in.readStringArray()); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeStringArray(types); + out.writeStringArray(ids.toArray(new String[ids.size()])); + } + /** * Returns the types used in this query */ @@ -208,19 +221,6 @@ public class IdsQueryBuilder extends AbstractQueryBuilder { return query; } - @Override - protected IdsQueryBuilder doReadFrom(StreamInput in) throws IOException { - IdsQueryBuilder idsQueryBuilder = new IdsQueryBuilder(in.readStringArray()); - idsQueryBuilder.addIds(in.readStringArray()); - return idsQueryBuilder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeStringArray(types); - out.writeStringArray(ids.toArray(new String[ids.size()])); - } - @Override protected int doHashCode() { return Objects.hash(ids, Arrays.hashCode(types)); diff --git a/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java index b2bab3f6799..477e55812cb 100644 --- a/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java @@ -41,7 +41,6 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder innerQuery() { return this.innerQuery; } @@ -202,20 +218,6 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder { private Float cutoffFrequency = null; - public static final MatchQueryBuilder PROTOTYPE = new MatchQueryBuilder("",""); - /** * Constructs a new match query. */ @@ -118,6 +116,49 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { this.value = value; } + /** + * Read from a stream. + */ + public MatchQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + value = in.readGenericValue(); + type = MatchQuery.Type.readFromStream(in); + operator = Operator.readFromStream(in); + slop = in.readVInt(); + prefixLength = in.readVInt(); + maxExpansions = in.readVInt(); + fuzzyTranspositions = in.readBoolean(); + lenient = in.readBoolean(); + zeroTermsQuery = MatchQuery.ZeroTermsQuery.readFromStream(in); + // optional fields + analyzer = in.readOptionalString(); + minimumShouldMatch = in.readOptionalString(); + fuzzyRewrite = in.readOptionalString(); + fuzziness = in.readOptionalWriteable(Fuzziness::new); + cutoffFrequency = in.readOptionalFloat(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeGenericValue(value); + type.writeTo(out); + operator.writeTo(out); + out.writeVInt(slop); + out.writeVInt(prefixLength); + out.writeVInt(maxExpansions); + out.writeBoolean(fuzzyTranspositions); + out.writeBoolean(lenient); + zeroTermsQuery.writeTo(out); + // optional fields + out.writeOptionalString(analyzer); + out.writeOptionalString(minimumShouldMatch); + out.writeOptionalString(fuzzyRewrite); + out.writeOptionalWriteable(fuzziness); + out.writeOptionalFloat(cutoffFrequency); + } + /** Returns the field name used in this query. */ public String fieldName() { return this.fieldName; @@ -461,60 +502,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { fuzzyRewrite, lenient, fuzzyTranspositions, zeroTermsQuery, cutoffFrequency); } - @Override - protected MatchQueryBuilder doReadFrom(StreamInput in) throws IOException { - MatchQueryBuilder matchQuery = new MatchQueryBuilder(in.readString(), in.readGenericValue()); - matchQuery.type = MatchQuery.Type.readTypeFrom(in); - matchQuery.operator = Operator.readOperatorFrom(in); - matchQuery.slop = in.readVInt(); - matchQuery.prefixLength = in.readVInt(); - matchQuery.maxExpansions = in.readVInt(); - matchQuery.fuzzyTranspositions = in.readBoolean(); - matchQuery.lenient = in.readBoolean(); - matchQuery.zeroTermsQuery = MatchQuery.ZeroTermsQuery.readZeroTermsQueryFrom(in); - // optional fields - matchQuery.analyzer = in.readOptionalString(); - matchQuery.minimumShouldMatch = in.readOptionalString(); - matchQuery.fuzzyRewrite = in.readOptionalString(); - if (in.readBoolean()) { - matchQuery.fuzziness = Fuzziness.readFuzzinessFrom(in); - } - if (in.readBoolean()) { - matchQuery.cutoffFrequency = in.readFloat(); - } - return matchQuery; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeGenericValue(value); - type.writeTo(out); - operator.writeTo(out); - out.writeVInt(slop); - out.writeVInt(prefixLength); - out.writeVInt(maxExpansions); - out.writeBoolean(fuzzyTranspositions); - out.writeBoolean(lenient); - zeroTermsQuery.writeTo(out); - // optional fields - out.writeOptionalString(analyzer); - out.writeOptionalString(minimumShouldMatch); - out.writeOptionalString(fuzzyRewrite); - if (fuzziness == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - fuzziness.writeTo(out); - } - if (cutoffFrequency == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeFloat(cutoffFrequency); - } - } - @Override public String getWriteableName() { return NAME; diff --git a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index c36f7263243..8e3b39a9ee8 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -82,7 +82,6 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder) in.readGenericValue(); routing = in.readOptionalString(); version = in.readLong(); - versionType = VersionType.readVersionTypeFrom(in); + versionType = VersionType.readFromStream(in); } @Override @@ -510,6 +509,51 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder fieldsBoosts; - private MultiMatchQueryBuilder.Type type = DEFAULT_TYPE; + private Type type = DEFAULT_TYPE; private Operator operator = DEFAULT_OPERATOR; private String analyzer; private int slop = DEFAULT_PHRASE_SLOP; @@ -93,8 +93,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder { /** @@ -128,8 +126,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder(); + for (int i = 0; i < size; i++) { + fieldsBoosts.put(in.readString(), in.readFloat()); + } + type = Type.readFromStream(in); + operator = Operator.readFromStream(in); + analyzer = in.readOptionalString(); + slop = in.readVInt(); + fuzziness = in.readOptionalWriteable(Fuzziness::new); + prefixLength = in.readVInt(); + maxExpansions = in.readVInt(); + minimumShouldMatch = in.readOptionalString(); + fuzzyRewrite = in.readOptionalString(); + useDisMax = in.readOptionalBoolean(); + tieBreaker = in.readOptionalFloat(); + lenient = in.readBoolean(); + cutoffFrequency = in.readOptionalFloat(); + zeroTermsQuery = MatchQuery.ZeroTermsQuery.readFromStream(in); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeGenericValue(value); + out.writeVInt(fieldsBoosts.size()); + for (Map.Entry fieldsEntry : fieldsBoosts.entrySet()) { + out.writeString(fieldsEntry.getKey()); + out.writeFloat(fieldsEntry.getValue()); + } + type.writeTo(out); + operator.writeTo(out); + out.writeOptionalString(analyzer); + out.writeVInt(slop); + out.writeOptionalWriteable(fuzziness); + out.writeVInt(prefixLength); + out.writeVInt(maxExpansions); + out.writeOptionalString(minimumShouldMatch); + out.writeOptionalString(fuzzyRewrite); + out.writeOptionalBoolean(useDisMax); + out.writeOptionalFloat(tieBreaker); + out.writeBoolean(lenient); + out.writeOptionalFloat(cutoffFrequency); + zeroTermsQuery.writeTo(out); + } + public Object value() { return value; } @@ -721,61 +763,6 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder fieldsEntry : fieldsBoosts.entrySet()) { - out.writeString(fieldsEntry.getKey()); - out.writeFloat(fieldsEntry.getValue()); - } - type.writeTo(out); - operator.writeTo(out); - out.writeOptionalString(analyzer); - out.writeVInt(slop); - if (fuzziness != null) { - out.writeBoolean(true); - fuzziness.writeTo(out); - } else { - out.writeBoolean(false); - } - out.writeVInt(prefixLength); - out.writeVInt(maxExpansions); - out.writeOptionalString(minimumShouldMatch); - out.writeOptionalString(fuzzyRewrite); - out.writeOptionalBoolean(useDisMax); - out.writeGenericValue(tieBreaker); - out.writeBoolean(lenient); - out.writeGenericValue(cutoffFrequency); - zeroTermsQuery.writeTo(out); - } - @Override protected int doHashCode() { return Objects.hash(value, fieldsBoosts, type, operator, analyzer, slop, fuzziness, diff --git a/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index 9993ed7e13d..e7912a2991e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -43,7 +43,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder */ public static final String NAME = "nested"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final NestedQueryBuilder PROTOTYPE = new NestedQueryBuilder("", EmptyQueryBuilder.PROTOTYPE); /** * The default score move for nested queries. @@ -84,6 +83,25 @@ public class NestedQueryBuilder extends AbstractQueryBuilder } } + /** + * Read from a stream. + */ + public NestedQueryBuilder(StreamInput in) throws IOException { + super(in); + path = in.readString(); + scoreMode = ScoreMode.values()[in.readVInt()]; + query = in.readQuery(); + innerHitBuilder = in.readOptionalWriteable(InnerHitBuilder::new); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(path); + out.writeVInt(scoreMode.ordinal()); + out.writeQuery(query); + out.writeOptionalWriteable(innerHitBuilder); + } + /** * The score mode how the scores from the matching child documents are mapped into the nested parent document. */ @@ -198,32 +216,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder return Objects.hash(query, path, scoreMode, innerHitBuilder); } - private NestedQueryBuilder(StreamInput in) throws IOException { - path = in.readString(); - final int ordinal = in.readVInt(); - scoreMode = ScoreMode.values()[ordinal]; - query = in.readQuery(); - innerHitBuilder = InnerHitBuilder.optionalReadFromStream(in); - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(path); - out.writeVInt(scoreMode.ordinal()); - out.writeQuery(query); - if (innerHitBuilder != null) { - out.writeBoolean(true); - innerHitBuilder.writeTo(out); - } else { - out.writeBoolean(false); - } - } - - @Override - protected NestedQueryBuilder doReadFrom(StreamInput in) throws IOException { - return new NestedQueryBuilder(in); - } - @Override protected Query doToQuery(QueryShardContext context) throws IOException { ObjectMapper nestedObjectMapper = context.getObjectMapper(path); diff --git a/core/src/main/java/org/elasticsearch/index/query/Operator.java b/core/src/main/java/org/elasticsearch/index/query/Operator.java index 528b792f196..48e671218cd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/Operator.java +++ b/core/src/main/java/org/elasticsearch/index/query/Operator.java @@ -53,7 +53,7 @@ public enum Operator implements Writeable { } } - public static Operator readOperatorFrom(StreamInput in) throws IOException { + public static Operator readFromStream(StreamInput in) throws IOException { int ordinal = in.readVInt(); if (ordinal < 0 || ordinal >= values().length) { throw new IOException("Unknown Operator ordinal [" + ordinal + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/ParentIdQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ParentIdQueryBuilder.java index 032ee124b3d..1fc79908f3f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ParentIdQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ParentIdQueryBuilder.java @@ -43,8 +43,6 @@ public final class ParentIdQueryBuilder extends AbstractQueryBuilder public static final String NAME = "prefix"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final PrefixQueryBuilder PROTOTYPE = new PrefixQueryBuilder("field", "value"); private static final ParseField PREFIX_FIELD = new ParseField("value", "prefix"); private static final ParseField REWRITE_FIELD = new ParseField("rewrite"); @@ -72,6 +71,23 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder this.value = value; } + /** + * Read from a stream. + */ + public PrefixQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + value = in.readString(); + rewrite = in.readOptionalString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeString(value); + out.writeOptionalString(rewrite); + } + public String fieldName() { return this.fieldName; } @@ -178,20 +194,6 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder return query; } - @Override - protected PrefixQueryBuilder doReadFrom(StreamInput in) throws IOException { - PrefixQueryBuilder prefixQueryBuilder = new PrefixQueryBuilder(in.readString(), in.readString()); - prefixQueryBuilder.rewrite = in.readOptionalString(); - return prefixQueryBuilder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeString(value); - out.writeOptionalString(rewrite); - } - @Override protected final int doHashCode() { return Objects.hash(fieldName, value, rewrite); diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java index 2147733b8fe..e49b0cace4f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java @@ -59,7 +59,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder fieldsEntry : this.fieldsAndWeights.entrySet()) { + out.writeString(fieldsEntry.getKey()); + out.writeFloat(fieldsEntry.getValue()); + } + this.defaultOperator.writeTo(out); + out.writeOptionalString(this.analyzer); + out.writeOptionalString(this.quoteAnalyzer); + out.writeOptionalString(this.quoteFieldSuffix); + out.writeBoolean(this.autoGeneratePhraseQueries); + out.writeOptionalBoolean(this.allowLeadingWildcard); + out.writeOptionalBoolean(this.analyzeWildcard); + out.writeBoolean(this.lowercaseExpandedTerms); + out.writeBoolean(this.enablePositionIncrements); + out.writeString(this.locale.toLanguageTag()); + this.fuzziness.writeTo(out); + out.writeVInt(this.fuzzyPrefixLength); + out.writeVInt(this.fuzzyMaxExpansions); + out.writeOptionalString(this.fuzzyRewrite); + out.writeVInt(this.phraseSlop); + out.writeBoolean(this.useDisMax); + out.writeFloat(this.tieBreaker); + out.writeOptionalString(this.rewrite); + out.writeOptionalString(this.minimumShouldMatch); + out.writeOptionalBoolean(this.lenient); + out.writeOptionalTimeZone(timeZone); + out.writeBoolean(this.escape); + out.writeVInt(this.maxDeterminizedStates); + } + public String queryString() { return this.queryString; } @@ -731,81 +800,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder fieldsEntry : this.fieldsAndWeights.entrySet()) { - out.writeString(fieldsEntry.getKey()); - out.writeFloat(fieldsEntry.getValue()); - } - this.defaultOperator.writeTo(out); - out.writeOptionalString(this.analyzer); - out.writeOptionalString(this.quoteAnalyzer); - out.writeOptionalString(this.quoteFieldSuffix); - out.writeBoolean(this.autoGeneratePhraseQueries); - out.writeOptionalBoolean(this.allowLeadingWildcard); - out.writeOptionalBoolean(this.analyzeWildcard); - out.writeBoolean(this.lowercaseExpandedTerms); - out.writeBoolean(this.enablePositionIncrements); - out.writeString(this.locale.toLanguageTag()); - this.fuzziness.writeTo(out); - out.writeVInt(this.fuzzyPrefixLength); - out.writeVInt(this.fuzzyMaxExpansions); - out.writeOptionalString(this.fuzzyRewrite); - out.writeVInt(this.phraseSlop); - out.writeBoolean(this.useDisMax); - out.writeFloat(this.tieBreaker); - out.writeOptionalString(this.rewrite); - out.writeOptionalString(this.minimumShouldMatch); - out.writeOptionalBoolean(this.lenient); - if (this.timeZone == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeString(this.timeZone.getID()); - } - out.writeBoolean(this.escape); - out.writeVInt(this.maxDeterminizedStates); - } - @Override protected boolean doEquals(QueryStringQueryBuilder other) { return Objects.equals(queryString, other.queryString) && diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index 226f33fd2cb..0f8bbff959e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -48,7 +48,6 @@ import java.util.Objects; public class RangeQueryBuilder extends AbstractQueryBuilder implements MultiTermQueryBuilder { public static final String NAME = "range"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final RangeQueryBuilder PROTOTYPE = new RangeQueryBuilder("field"); public static final boolean DEFAULT_INCLUDE_UPPER = true; public static final boolean DEFAULT_INCLUDE_LOWER = true; @@ -93,6 +92,38 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i this.fieldName = fieldName; } + /** + * Read from a stream. + */ + public RangeQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + from = in.readGenericValue(); + to = in.readGenericValue(); + includeLower = in.readBoolean(); + includeUpper = in.readBoolean(); + timeZone = in.readOptionalTimeZone(); + String formatString = in.readOptionalString(); + if (formatString != null) { + format = Joda.forPattern(formatString); + } + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(this.fieldName); + out.writeGenericValue(this.from); + out.writeGenericValue(this.to); + out.writeBoolean(this.includeLower); + out.writeBoolean(this.includeUpper); + out.writeOptionalTimeZone(timeZone); + String formatString = null; + if (this.format != null) { + formatString = this.format.format(); + } + out.writeOptionalString(formatString); + } + /** * Get the field name for this query. */ @@ -434,43 +465,6 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i return query; } - @Override - protected RangeQueryBuilder doReadFrom(StreamInput in) throws IOException { - RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder(in.readString()); - rangeQueryBuilder.from = in.readGenericValue(); - rangeQueryBuilder.to = in.readGenericValue(); - rangeQueryBuilder.includeLower = in.readBoolean(); - rangeQueryBuilder.includeUpper = in.readBoolean(); - String timeZoneId = in.readOptionalString(); - if (timeZoneId != null) { - rangeQueryBuilder.timeZone = DateTimeZone.forID(timeZoneId); - } - String formatString = in.readOptionalString(); - if (formatString != null) { - rangeQueryBuilder.format = Joda.forPattern(formatString); - } - return rangeQueryBuilder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(this.fieldName); - out.writeGenericValue(this.from); - out.writeGenericValue(this.to); - out.writeBoolean(this.includeLower); - out.writeBoolean(this.includeUpper); - String timeZoneId = null; - if (this.timeZone != null) { - timeZoneId = this.timeZone.getID(); - } - out.writeOptionalString(timeZoneId); - String formatString = null; - if (this.format != null) { - formatString = this.format.format(); - } - out.writeOptionalString(formatString); - } - @Override protected int doHashCode() { String timeZoneId = timeZone == null ? null : timeZone.getID(); diff --git a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java index 84285e0907e..7a19d1f719e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -45,7 +45,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder public static final String NAME = "regexp"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final RegexpQueryBuilder PROTOTYPE = new RegexpQueryBuilder("field", "value"); public static final int DEFAULT_FLAGS_VALUE = RegexpFlag.ALL.value(); public static final int DEFAULT_MAX_DETERMINIZED_STATES = Operations.DEFAULT_MAX_DETERMINIZED_STATES; @@ -85,6 +84,27 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder this.value = value; } + /** + * Read from a stream. + */ + public RegexpQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + value = in.readString(); + flagsValue = in.readVInt(); + maxDeterminizedStates = in.readVInt(); + rewrite = in.readOptionalString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeString(value); + out.writeVInt(flagsValue); + out.writeVInt(maxDeterminizedStates); + out.writeOptionalString(rewrite); + } + /** Returns the field name used in this query. */ public String fieldName() { return this.fieldName; @@ -249,24 +269,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder return query; } - @Override - protected RegexpQueryBuilder doReadFrom(StreamInput in) throws IOException { - RegexpQueryBuilder regexpQueryBuilder = new RegexpQueryBuilder(in.readString(), in.readString()); - regexpQueryBuilder.flagsValue = in.readVInt(); - regexpQueryBuilder.maxDeterminizedStates = in.readVInt(); - regexpQueryBuilder.rewrite = in.readOptionalString(); - return regexpQueryBuilder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeString(value); - out.writeVInt(flagsValue); - out.writeVInt(maxDeterminizedStates); - out.writeOptionalString(rewrite); - } - @Override protected int doHashCode() { return Objects.hash(fieldName, value, flagsValue, maxDeterminizedStates, rewrite); diff --git a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 4c046334f95..794160881f5 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -52,8 +52,6 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder public static final String NAME = "script"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final ScriptQueryBuilder PROTOTYPE = new ScriptQueryBuilder(new Script("")); - private static final ParseField PARAMS_FIELD = new ParseField("params"); private final Script script; @@ -65,6 +63,19 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder this.script = script; } + /** + * Read from a stream. + */ + public ScriptQueryBuilder(StreamInput in) throws IOException { + super(in); + script = Script.readScript(in); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + script.writeTo(out); + } + public Script script() { return this.script; } @@ -217,16 +228,6 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder } } - @Override - protected ScriptQueryBuilder doReadFrom(StreamInput in) throws IOException { - return new ScriptQueryBuilder(Script.readScript(in)); - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - script.writeTo(out); - } - @Override protected int doHashCode() { return Objects.hash(script); diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java index 4b55d225f04..187f2c33607 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -94,8 +94,6 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder fields = new HashMap<>(); + for (int i = 0; i < size; i++) { + String field = in.readString(); + Float weight = in.readFloat(); + fields.put(field, weight); + } + fieldsAndWeights.putAll(fields); + flags = in.readInt(); + analyzer = in.readOptionalString(); + defaultOperator = Operator.readFromStream(in); + settings.lowercaseExpandedTerms(in.readBoolean()); + settings.lenient(in.readBoolean()); + settings.analyzeWildcard(in.readBoolean()); + settings.locale(Locale.forLanguageTag(in.readString())); + minimumShouldMatch = in.readOptionalString(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(queryText); + out.writeInt(fieldsAndWeights.size()); + for (Map.Entry entry : fieldsAndWeights.entrySet()) { + out.writeString(entry.getKey()); + out.writeFloat(entry.getValue()); + } + out.writeInt(flags); + out.writeOptionalString(analyzer); + defaultOperator.writeTo(out); + out.writeBoolean(settings.lowercaseExpandedTerms()); + out.writeBoolean(settings.lenient()); + out.writeBoolean(settings.analyzeWildcard()); + out.writeString(settings.locale().toLanguageTag()); + out.writeOptionalString(minimumShouldMatch); + } + /** Returns the text to parse the query from. */ public String value() { return this.queryText; @@ -482,47 +522,6 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder fields = new HashMap<>(); - for (int i = 0; i < size; i++) { - String field = in.readString(); - Float weight = in.readFloat(); - fields.put(field, weight); - } - result.fieldsAndWeights.putAll(fields); - result.flags = in.readInt(); - result.analyzer = in.readOptionalString(); - result.defaultOperator = Operator.readOperatorFrom(in); - result.settings.lowercaseExpandedTerms(in.readBoolean()); - result.settings.lenient(in.readBoolean()); - result.settings.analyzeWildcard(in.readBoolean()); - String localeStr = in.readString(); - result.settings.locale(Locale.forLanguageTag(localeStr)); - result.minimumShouldMatch = in.readOptionalString(); - return result; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(queryText); - out.writeInt(fieldsAndWeights.size()); - for (Map.Entry entry : fieldsAndWeights.entrySet()) { - out.writeString(entry.getKey()); - out.writeFloat(entry.getValue()); - } - out.writeInt(flags); - out.writeOptionalString(analyzer); - defaultOperator.writeTo(out); - out.writeBoolean(settings.lowercaseExpandedTerms()); - out.writeBoolean(settings.lenient()); - out.writeBoolean(settings.analyzeWildcard()); - out.writeString(settings.locale().toLanguageTag()); - out.writeOptionalString(minimumShouldMatch); - } - @Override protected int doHashCode() { return Objects.hash(fieldsAndWeights, analyzer, defaultOperator, queryText, minimumShouldMatch, settings, flags); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanContainingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanContainingQueryBuilder.java index b7e7f908a09..c829c238812 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanContainingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanContainingQueryBuilder.java @@ -40,20 +40,18 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder big; + private final SpanQueryBuilder little; /** * @param big the big clause, it must enclose {@code little} for a match. * @param little the little clause, it must be contained within {@code big} for a match. */ - public SpanContainingQueryBuilder(SpanQueryBuilder big, SpanQueryBuilder little) { + public SpanContainingQueryBuilder(SpanQueryBuilder big, SpanQueryBuilder little) { if (big == null) { throw new IllegalArgumentException("inner clause [big] cannot be null."); } @@ -64,6 +62,21 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder) in.readQuery(); + little = (SpanQueryBuilder) in.readQuery(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(big); + out.writeQuery(little); + } + /** * @return the big clause, it must enclose {@code little} for a match. */ @@ -142,19 +155,6 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder matchBuilder; private final int end; @@ -52,7 +51,7 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilderend positions */ - public SpanFirstQueryBuilder(SpanQueryBuilder matchBuilder, int end) { + public SpanFirstQueryBuilder(SpanQueryBuilder matchBuilder, int end) { if (matchBuilder == null) { throw new IllegalArgumentException("inner span query cannot be null"); } @@ -63,10 +62,25 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder)in.readQuery(); + end = in.readInt(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(matchBuilder); + out.writeInt(end); + } + /** * @return the inner {@link SpanQueryBuilder} defined in this query */ - public SpanQueryBuilder innerQuery() { + public SpanQueryBuilder innerQuery() { return this.matchBuilder; } @@ -141,19 +155,6 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder multiTermQueryBuilder; - public SpanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder) { + public SpanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder) { if (multiTermQueryBuilder == null) { throw new IllegalArgumentException("inner multi term query cannot be null"); } this.multiTermQueryBuilder = multiTermQueryBuilder; } - public MultiTermQueryBuilder innerQuery() { + /** + * Read from a stream. + */ + public SpanMultiTermQueryBuilder(StreamInput in) throws IOException { + super(in); + multiTermQueryBuilder = (MultiTermQueryBuilder) in.readQuery(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(multiTermQueryBuilder); + } + + public MultiTermQueryBuilder innerQuery() { return this.multiTermQueryBuilder; } @@ -133,17 +144,6 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder clause : readQueries(in)) { + this.clauses.add((SpanQueryBuilder) clause); + } + slop = in.readVInt(); + inOrder = in.readBoolean(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + writeQueries(out, clauses); + out.writeVInt(slop); + out.writeBoolean(inOrder); + } + /** * @return the maximum number of intervening unmatched positions permitted */ @@ -199,25 +217,6 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder> clauses = readQueries(in); - SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder((SpanQueryBuilder)clauses.get(0), in.readVInt()); - for (int i = 1; i < clauses.size(); i++) { - queryBuilder.clauses.add((SpanQueryBuilder)clauses.get(i)); - } - queryBuilder.inOrder = in.readBoolean(); - return queryBuilder; - - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - writeQueries(out, clauses); - out.writeVInt(slop); - out.writeBoolean(inOrder); - } - @Override protected int doHashCode() { return Objects.hash(clauses, slop, inOrder); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java index daca8eb5f1f..c65c8f9ea4e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java @@ -36,8 +36,6 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder include; - private final SpanQueryBuilder exclude; + private final SpanQueryBuilder exclude; private int pre = DEFAULT_PRE; @@ -64,7 +62,7 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder include, SpanQueryBuilder exclude) { if (include == null) { throw new IllegalArgumentException("inner clause [include] cannot be null."); } @@ -75,6 +73,25 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder) in.readQuery(); + exclude = (SpanQueryBuilder) in.readQuery(); + pre = in.readVInt(); + post = in.readVInt(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(include); + out.writeQuery(exclude); + out.writeVInt(pre); + out.writeVInt(post); + } + /** * @return the span query whose matches are filtered */ @@ -232,24 +249,6 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder public static final String NAME = "span_or"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final SpanOrQueryBuilder PROTOTYPE = new SpanOrQueryBuilder(SpanTermQueryBuilder.PROTOTYPE); private static final ParseField CLAUSES_FIELD = new ParseField("clauses"); @@ -54,6 +53,21 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder clauses.add(initialClause); } + /** + * Read from a stream. + */ + public SpanOrQueryBuilder(StreamInput in) throws IOException { + super(in); + for (QueryBuilder clause: readQueries(in)) { + clauses.add((SpanQueryBuilder) clause); + } + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + writeQueries(out, clauses); + } + public SpanOrQueryBuilder clause(SpanQueryBuilder clause) { if (clause == null) { throw new IllegalArgumentException("inner bool query clause cannot be null"); @@ -141,22 +155,6 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder return new SpanOrQuery(spanQueries); } - @Override - protected SpanOrQueryBuilder doReadFrom(StreamInput in) throws IOException { - List> clauses = readQueries(in); - SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder((SpanQueryBuilder)clauses.get(0)); - for (int i = 1; i < clauses.size(); i++) { - queryBuilder.clauses.add((SpanQueryBuilder)clauses.get(i)); - } - return queryBuilder; - - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - writeQueries(out, clauses); - } - @Override protected int doHashCode() { return Objects.hash(clauses); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java index d22c54395ac..8205a80b4da 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -25,6 +25,7 @@ import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.search.spans.SpanTermQuery; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; +import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.MappedFieldType; @@ -73,6 +74,13 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder big; + private final SpanQueryBuilder little; /** * Query that returns spans from little that are contained in a spans from big. * @param big clause that must enclose {@code little} for a match. * @param little the little clause, it must be contained within {@code big} for a match. */ - public SpanWithinQueryBuilder(SpanQueryBuilder big, SpanQueryBuilder little) { + public SpanWithinQueryBuilder(SpanQueryBuilder big, SpanQueryBuilder little) { if (big == null) { throw new IllegalArgumentException("inner clause [big] cannot be null."); } @@ -65,6 +63,21 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder) in.readQuery(); + little = (SpanQueryBuilder) in.readQuery(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeQuery(big); + out.writeQuery(little); + } + /** * @return the little clause, contained within {@code big} for a match. */ @@ -154,19 +167,6 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "term"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - public static final TermQueryBuilder PROTOTYPE = new TermQueryBuilder("name", "value"); private static final ParseField TERM_FIELD = new ParseField("term"); private static final ParseField VALUE_FIELD = new ParseField("value"); @@ -77,6 +77,13 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { super(fieldName, value); } + /** + * Read from a stream. + */ + public TermQueryBuilder(StreamInput in) throws IOException { + super(in); + } + public static TermQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { XContentParser parser = parseContext.parser(); @@ -149,11 +156,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { return query; } - @Override - protected TermQueryBuilder createBuilder(String fieldName, Object value) { - return new TermQueryBuilder(fieldName, value); - } - @Override public String getWriteableName() { return NAME; diff --git a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java index fb915797799..2ffe90faf74 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java @@ -57,10 +57,9 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "terms"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME, "in"); - public static final TermsQueryBuilder PROTOTYPE = new TermsQueryBuilder("field", "value"); private final String fieldName; - private final List values; + private final List values; private final TermsLookup termsLookup; public TermsQueryBuilder(String fieldName, TermsLookup termsLookup) { @@ -164,6 +163,23 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { this.termsLookup = null; } + /** + * Read from a stream. + */ + public TermsQueryBuilder(StreamInput in) throws IOException { + super(in); + fieldName = in.readString(); + termsLookup = in.readOptionalWriteable(TermsLookup::new); + values = (List) in.readGenericValue(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeString(fieldName); + out.writeOptionalWriteable(termsLookup); + out.writeGenericValue(values); + } + public String fieldName() { return this.fieldName; } @@ -318,7 +334,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { return terms; } - private static Query handleTermsQuery(List terms, String fieldName, QueryShardContext context) { + private static Query handleTermsQuery(List terms, String fieldName, QueryShardContext context) { MappedFieldType fieldType = context.fieldMapper(fieldName); String indexFieldName; if (fieldType != null) { @@ -352,28 +368,6 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { return query; } - @SuppressWarnings("unchecked") - @Override - protected TermsQueryBuilder doReadFrom(StreamInput in) throws IOException { - String field = in.readString(); - TermsLookup lookup = null; - if (in.readBoolean()) { - lookup = TermsLookup.readTermsLookupFrom(in); - } - List values = (List) in.readGenericValue(); - return new TermsQueryBuilder(field, values, lookup); - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeBoolean(termsLookup != null); - if (termsLookup != null) { - termsLookup.writeTo(out); - } - out.writeGenericValue(values); - } - @Override protected int doHashCode() { return Objects.hash(fieldName, values, termsLookup); diff --git a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java index ca1e40cd34f..1e4490cfabf 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java @@ -42,8 +42,6 @@ public class TypeQueryBuilder extends AbstractQueryBuilder { private final BytesRef type; - public static final TypeQueryBuilder PROTOTYPE = new TypeQueryBuilder("type"); - public TypeQueryBuilder(String type) { if (type == null) { throw new IllegalArgumentException("[type] cannot be null"); @@ -58,6 +56,19 @@ public class TypeQueryBuilder extends AbstractQueryBuilder { this.type = type; } + /** + * Read from a stream. + */ + public TypeQueryBuilder(StreamInput in) throws IOException { + super(in); + type = in.readBytesRef(); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + out.writeBytesRef(type); + } + public String type() { return BytesRefs.toString(this.type); } @@ -126,16 +137,6 @@ public class TypeQueryBuilder extends AbstractQueryBuilder { } } - @Override - protected TypeQueryBuilder doReadFrom(StreamInput in) throws IOException { - return new TypeQueryBuilder(in.readBytesRef()); - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeBytesRef(type); - } - @Override protected int doHashCode() { return Objects.hash(type); diff --git a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java index 6621b0725f4..13a28d93b89 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java @@ -50,7 +50,6 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder { - - public static final FunctionScoreQueryBuilder PROTOTYPE = new FunctionScoreQueryBuilder(EmptyQueryBuilder.PROTOTYPE, - new FunctionScoreQueryBuilder.FilterFunctionBuilder[0]); - public static final String NAME = "function_score"; - public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); // For better readability of error message @@ -149,6 +143,29 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder query = in.readQuery(); - int size = in.readVInt(); - FilterFunctionBuilder[] filterFunctionBuilders = new FilterFunctionBuilder[size]; - for (int i = 0; i < size; i++) { - filterFunctionBuilders[i] = new FilterFunctionBuilder(in); - } - FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(query, filterFunctionBuilders); - functionScoreQueryBuilder.maxBoost(in.readFloat()); - if (in.readBoolean()) { - functionScoreQueryBuilder.setMinScore(in.readFloat()); - } - if (in.readBoolean()) { - functionScoreQueryBuilder.boostMode(CombineFunction.readCombineFunctionFrom(in)); - } - functionScoreQueryBuilder.scoreMode(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in)); - return functionScoreQueryBuilder; - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeQuery(query); - out.writeVInt(filterFunctionBuilders.length); - for (FilterFunctionBuilder filterFunctionBuilder : filterFunctionBuilders) { - filterFunctionBuilder.writeTo(out); - } - out.writeFloat(maxBoost); - if (minScore == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeFloat(minScore); - } - if (boostMode == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - boostMode.writeTo(out); - } - scoreMode.writeTo(out); - } - @Override protected Query doToQuery(QueryShardContext context) throws IOException { FilterFunction[] filterFunctions = new FilterFunction[filterFunctionBuilders.length]; diff --git a/core/src/main/java/org/elasticsearch/index/query/support/InnerHitBuilder.java b/core/src/main/java/org/elasticsearch/index/query/support/InnerHitBuilder.java index e3d26304ce1..429771fea17 100644 --- a/core/src/main/java/org/elasticsearch/index/query/support/InnerHitBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/support/InnerHitBuilder.java @@ -116,14 +116,6 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl }, SearchSourceBuilder.INNER_HITS_FIELD); } - public static InnerHitBuilder optionalReadFromStream(StreamInput in) throws IOException { - if (in.readBoolean()) { - return new InnerHitBuilder(in); - } else { - return null; - } - } - private String name; private String nestedPath; private String parentChildType; @@ -143,8 +135,13 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl private InnerHitsBuilder innerHitsBuilder; private FetchSourceContext fetchSourceContext; - // pkg protected, because is used in InnerHitsBuilder - InnerHitBuilder(StreamInput in) throws IOException { + public InnerHitBuilder() { + } + + /** + * Read from a stream. + */ + public InnerHitBuilder(StreamInput in) throws IOException { name = in.readOptionalString(); nestedPath = in.readOptionalString(); parentChildType = in.readOptionalString(); @@ -156,9 +153,9 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl fieldNames = (List) in.readGenericValue(); fieldDataFields = (List) in.readGenericValue(); if (in.readBoolean()) { - scriptFields = in.readList(t -> ScriptField.PROTOTYPE.readFrom(in)); + scriptFields = in.readList(ScriptField.PROTOTYPE::readFrom); } - fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); + fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new); if (in.readBoolean()) { int size = in.readVInt(); sorts = new ArrayList<>(size); @@ -171,7 +168,35 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl innerHitsBuilder = in.readOptionalWriteable(InnerHitsBuilder.PROTO::readFrom); } - public InnerHitBuilder() { + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeOptionalString(name); + out.writeOptionalString(nestedPath); + out.writeOptionalString(parentChildType); + out.writeVInt(from); + out.writeVInt(size); + out.writeBoolean(explain); + out.writeBoolean(version); + out.writeBoolean(trackScores); + out.writeGenericValue(fieldNames); + out.writeGenericValue(fieldDataFields); + boolean hasScriptFields = scriptFields != null; + out.writeBoolean(hasScriptFields); + if (hasScriptFields) { + out.writeList(scriptFields); + } + out.writeOptionalStreamable(fetchSourceContext); + boolean hasSorts = sorts != null; + out.writeBoolean(hasSorts); + if (hasSorts) { + out.writeVInt(sorts.size()); + for (SortBuilder sort : sorts) { + out.writeSortBuilder(sort); + } + } + out.writeOptionalWriteable(highlightBuilder); + out.writeQuery(query); + out.writeOptionalWriteable(innerHitsBuilder); } public InnerHitBuilder setParentChildType(String parentChildType) { @@ -460,37 +485,6 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl innerHitsContext.parsedQuery(parsedQuery); } - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeOptionalString(name); - out.writeOptionalString(nestedPath); - out.writeOptionalString(parentChildType); - out.writeVInt(from); - out.writeVInt(size); - out.writeBoolean(explain); - out.writeBoolean(version); - out.writeBoolean(trackScores); - out.writeGenericValue(fieldNames); - out.writeGenericValue(fieldDataFields); - boolean hasScriptFields = scriptFields != null; - out.writeBoolean(hasScriptFields); - if (hasScriptFields) { - out.writeList(scriptFields); - } - FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); - boolean hasSorts = sorts != null; - out.writeBoolean(hasSorts); - if (hasSorts) { - out.writeVInt(sorts.size()); - for (SortBuilder sort : sorts) { - out.writeSortBuilder(sort); - } - } - out.writeOptionalWriteable(highlightBuilder); - out.writeQuery(query); - out.writeOptionalWriteable(innerHitsBuilder); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java b/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java index c8b708271c6..bb167a8ccbe 100644 --- a/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java @@ -64,14 +64,11 @@ public class MatchQuery { private final int ordinal; - private static final Type PROTOTYPE = BOOLEAN; - private Type(int ordinal) { this.ordinal = ordinal; } - @Override - public Type readFrom(StreamInput in) throws IOException { + public static Type readFromStream(StreamInput in) throws IOException { int ord = in.readVInt(); for (Type type : Type.values()) { if (type.ordinal == ord) { @@ -81,10 +78,6 @@ public class MatchQuery { throw new ElasticsearchException("unknown serialized type [" + ord + "]"); } - public static Type readTypeFrom(StreamInput in) throws IOException { - return PROTOTYPE.readFrom(in); - } - @Override public void writeTo(StreamOutput out) throws IOException { out.writeVInt(this.ordinal); @@ -97,14 +90,11 @@ public class MatchQuery { private final int ordinal; - private static final ZeroTermsQuery PROTOTYPE = NONE; - private ZeroTermsQuery(int ordinal) { this.ordinal = ordinal; } - @Override - public ZeroTermsQuery readFrom(StreamInput in) throws IOException { + public static ZeroTermsQuery readFromStream(StreamInput in) throws IOException { int ord = in.readVInt(); for (ZeroTermsQuery zeroTermsQuery : ZeroTermsQuery.values()) { if (zeroTermsQuery.ordinal == ord) { @@ -114,10 +104,6 @@ public class MatchQuery { throw new ElasticsearchException("unknown serialized type [" + ord + "]"); } - public static ZeroTermsQuery readZeroTermsQueryFrom(StreamInput in) throws IOException { - return PROTOTYPE.readFrom(in); - } - @Override public void writeTo(StreamOutput out) throws IOException { out.writeVInt(this.ordinal); diff --git a/core/src/main/java/org/elasticsearch/indices/TermsLookup.java b/core/src/main/java/org/elasticsearch/indices/TermsLookup.java index 806181d0d4e..26f8d3e0b67 100644 --- a/core/src/main/java/org/elasticsearch/indices/TermsLookup.java +++ b/core/src/main/java/org/elasticsearch/indices/TermsLookup.java @@ -35,8 +35,6 @@ import java.util.Objects; * Encapsulates the parameters needed to fetch terms. */ public class TermsLookup implements Writeable, ToXContent { - static final TermsLookup PROTOTYPE = new TermsLookup("index", "type", "id", "path"); - private String index; private final String type; private final String id; @@ -64,6 +62,26 @@ public class TermsLookup implements Writeable, ToXContent { this.path = path; } + /** + * Read from a stream. + */ + public TermsLookup(StreamInput in) throws IOException { + type = in.readString(); + id = in.readString(); + path = in.readString(); + index = in.readOptionalString(); + routing = in.readOptionalString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeString(type); + out.writeString(id); + out.writeString(path); + out.writeOptionalString(index); + out.writeOptionalString(routing); + } + public String index() { return index; } @@ -139,30 +157,6 @@ public class TermsLookup implements Writeable, ToXContent { return index + "/" + type + "/" + id + "/" + path; } - @Override - public TermsLookup readFrom(StreamInput in) throws IOException { - String type = in.readString(); - String id = in.readString(); - String path = in.readString(); - String index = in.readOptionalString(); - TermsLookup termsLookup = new TermsLookup(index, type, id, path); - termsLookup.routing = in.readOptionalString(); - return termsLookup; - } - - public static TermsLookup readTermsLookupFrom(StreamInput in) throws IOException { - return PROTOTYPE.readFrom(in); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(type); - out.writeString(id); - out.writeString(path); - out.writeOptionalString(index); - out.writeOptionalString(routing); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (index != null) { diff --git a/core/src/main/java/org/elasticsearch/search/SearchModule.java b/core/src/main/java/org/elasticsearch/search/SearchModule.java index 87fd105c404..cf9a4b940be 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/core/src/main/java/org/elasticsearch/search/SearchModule.java @@ -535,85 +535,68 @@ public class SearchModule extends AbstractModule { } private void registerBuiltinQueryParsers() { - registerQuery(MatchQueryBuilder.PROTOTYPE::readFrom, MatchQueryBuilder::fromXContent, MatchQueryBuilder.QUERY_NAME_FIELD); - registerQuery(MatchPhraseQueryBuilder.PROTOTYPE::readFrom, MatchPhraseQueryBuilder::fromXContent, - MatchPhraseQueryBuilder.QUERY_NAME_FIELD); - registerQuery(MatchPhrasePrefixQueryBuilder.PROTOTYPE::readFrom, MatchPhrasePrefixQueryBuilder::fromXContent, + registerQuery(MatchQueryBuilder::new, MatchQueryBuilder::fromXContent, MatchQueryBuilder.QUERY_NAME_FIELD); + registerQuery(MatchPhraseQueryBuilder::new, MatchPhraseQueryBuilder::fromXContent, MatchPhraseQueryBuilder.QUERY_NAME_FIELD); + registerQuery(MatchPhrasePrefixQueryBuilder::new, MatchPhrasePrefixQueryBuilder::fromXContent, MatchPhrasePrefixQueryBuilder.QUERY_NAME_FIELD); - registerQuery(MultiMatchQueryBuilder.PROTOTYPE::readFrom, MultiMatchQueryBuilder::fromXContent, - MultiMatchQueryBuilder.QUERY_NAME_FIELD); - registerQuery(NestedQueryBuilder.PROTOTYPE::readFrom, NestedQueryBuilder::fromXContent, NestedQueryBuilder.QUERY_NAME_FIELD); - registerQuery(HasChildQueryBuilder.PROTOTYPE::readFrom, HasChildQueryBuilder::fromXContent, HasChildQueryBuilder.QUERY_NAME_FIELD); - registerQuery(HasParentQueryBuilder.PROTOTYPE::readFrom, HasParentQueryBuilder::fromXContent, - HasParentQueryBuilder.QUERY_NAME_FIELD); - registerQuery(DisMaxQueryBuilder.PROTOTYPE::readFrom, DisMaxQueryBuilder::fromXContent, DisMaxQueryBuilder.QUERY_NAME_FIELD); - registerQuery(IdsQueryBuilder.PROTOTYPE::readFrom, IdsQueryBuilder::fromXContent, IdsQueryBuilder.QUERY_NAME_FIELD); - registerQuery(MatchAllQueryBuilder.PROTOTYPE::readFrom, MatchAllQueryBuilder::fromXContent, MatchAllQueryBuilder.QUERY_NAME_FIELD); - registerQuery(QueryStringQueryBuilder.PROTOTYPE::readFrom, QueryStringQueryBuilder::fromXContent, - QueryStringQueryBuilder.QUERY_NAME_FIELD); - registerQuery(BoostingQueryBuilder.PROTOTYPE::readFrom, BoostingQueryBuilder::fromXContent, BoostingQueryBuilder.QUERY_NAME_FIELD); + registerQuery(MultiMatchQueryBuilder::new, MultiMatchQueryBuilder::fromXContent, MultiMatchQueryBuilder.QUERY_NAME_FIELD); + registerQuery(NestedQueryBuilder::new, NestedQueryBuilder::fromXContent, NestedQueryBuilder.QUERY_NAME_FIELD); + registerQuery(HasChildQueryBuilder::new, HasChildQueryBuilder::fromXContent, HasChildQueryBuilder.QUERY_NAME_FIELD); + registerQuery(HasParentQueryBuilder::new, HasParentQueryBuilder::fromXContent, HasParentQueryBuilder.QUERY_NAME_FIELD); + registerQuery(DisMaxQueryBuilder::new, DisMaxQueryBuilder::fromXContent, DisMaxQueryBuilder.QUERY_NAME_FIELD); + registerQuery(IdsQueryBuilder::new, IdsQueryBuilder::fromXContent, IdsQueryBuilder.QUERY_NAME_FIELD); + registerQuery(MatchAllQueryBuilder::new, MatchAllQueryBuilder::fromXContent, MatchAllQueryBuilder.QUERY_NAME_FIELD); + registerQuery(QueryStringQueryBuilder::new, QueryStringQueryBuilder::fromXContent, QueryStringQueryBuilder.QUERY_NAME_FIELD); + registerQuery(BoostingQueryBuilder::new, BoostingQueryBuilder::fromXContent, BoostingQueryBuilder.QUERY_NAME_FIELD); BooleanQuery.setMaxClauseCount(settings.getAsInt("index.query.bool.max_clause_count", settings.getAsInt("indices.query.bool.max_clause_count", BooleanQuery.getMaxClauseCount()))); - registerQuery(BoolQueryBuilder.PROTOTYPE::readFrom, BoolQueryBuilder::fromXContent, BoolQueryBuilder.QUERY_NAME_FIELD); - registerQuery(TermQueryBuilder.PROTOTYPE::readFrom, TermQueryBuilder::fromXContent, TermQueryBuilder.QUERY_NAME_FIELD); - registerQuery(TermsQueryBuilder.PROTOTYPE::readFrom, TermsQueryBuilder::fromXContent, TermsQueryBuilder.QUERY_NAME_FIELD); - registerQuery(FuzzyQueryBuilder.PROTOTYPE::readFrom, FuzzyQueryBuilder::fromXContent, FuzzyQueryBuilder.QUERY_NAME_FIELD); - registerQuery(RegexpQueryBuilder.PROTOTYPE::readFrom, RegexpQueryBuilder::fromXContent, RegexpQueryBuilder.QUERY_NAME_FIELD); - registerQuery(RangeQueryBuilder.PROTOTYPE::readFrom, RangeQueryBuilder::fromXContent, RangeQueryBuilder.QUERY_NAME_FIELD); - registerQuery(PrefixQueryBuilder.PROTOTYPE::readFrom, PrefixQueryBuilder::fromXContent, PrefixQueryBuilder.QUERY_NAME_FIELD); - registerQuery(WildcardQueryBuilder.PROTOTYPE::readFrom, WildcardQueryBuilder::fromXContent, WildcardQueryBuilder.QUERY_NAME_FIELD); - registerQuery(ConstantScoreQueryBuilder.PROTOTYPE::readFrom, ConstantScoreQueryBuilder::fromXContent, - ConstantScoreQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanTermQueryBuilder.PROTOTYPE::readFrom, SpanTermQueryBuilder::fromXContent, SpanTermQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanNotQueryBuilder.PROTOTYPE::readFrom, SpanNotQueryBuilder::fromXContent, SpanNotQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanWithinQueryBuilder.PROTOTYPE::readFrom, SpanWithinQueryBuilder::fromXContent, - SpanWithinQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanContainingQueryBuilder.PROTOTYPE::readFrom, SpanContainingQueryBuilder::fromXContent, + registerQuery(BoolQueryBuilder::new, BoolQueryBuilder::fromXContent, BoolQueryBuilder.QUERY_NAME_FIELD); + registerQuery(TermQueryBuilder::new, TermQueryBuilder::fromXContent, TermQueryBuilder.QUERY_NAME_FIELD); + registerQuery(TermsQueryBuilder::new, TermsQueryBuilder::fromXContent, TermsQueryBuilder.QUERY_NAME_FIELD); + registerQuery(FuzzyQueryBuilder::new, FuzzyQueryBuilder::fromXContent, FuzzyQueryBuilder.QUERY_NAME_FIELD); + registerQuery(RegexpQueryBuilder::new, RegexpQueryBuilder::fromXContent, RegexpQueryBuilder.QUERY_NAME_FIELD); + registerQuery(RangeQueryBuilder::new, RangeQueryBuilder::fromXContent, RangeQueryBuilder.QUERY_NAME_FIELD); + registerQuery(PrefixQueryBuilder::new, PrefixQueryBuilder::fromXContent, PrefixQueryBuilder.QUERY_NAME_FIELD); + registerQuery(WildcardQueryBuilder::new, WildcardQueryBuilder::fromXContent, WildcardQueryBuilder.QUERY_NAME_FIELD); + registerQuery(ConstantScoreQueryBuilder::new, ConstantScoreQueryBuilder::fromXContent, ConstantScoreQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanTermQueryBuilder::new, SpanTermQueryBuilder::fromXContent, SpanTermQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanNotQueryBuilder::new, SpanNotQueryBuilder::fromXContent, SpanNotQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanWithinQueryBuilder::new, SpanWithinQueryBuilder::fromXContent, SpanWithinQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanContainingQueryBuilder::new, SpanContainingQueryBuilder::fromXContent, SpanContainingQueryBuilder.QUERY_NAME_FIELD); - registerQuery(FieldMaskingSpanQueryBuilder.PROTOTYPE::readFrom, FieldMaskingSpanQueryBuilder::fromXContent, + registerQuery(FieldMaskingSpanQueryBuilder::new, FieldMaskingSpanQueryBuilder::fromXContent, FieldMaskingSpanQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanFirstQueryBuilder.PROTOTYPE::readFrom, SpanFirstQueryBuilder::fromXContent, - SpanFirstQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanNearQueryBuilder.PROTOTYPE::readFrom, SpanNearQueryBuilder::fromXContent, SpanNearQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanOrQueryBuilder.PROTOTYPE::readFrom, SpanOrQueryBuilder::fromXContent, SpanOrQueryBuilder.QUERY_NAME_FIELD); - registerQuery(MoreLikeThisQueryBuilder.PROTOTYPE::readFrom, MoreLikeThisQueryBuilder::fromXContent, - MoreLikeThisQueryBuilder.QUERY_NAME_FIELD); - registerQuery(WrapperQueryBuilder.PROTOTYPE::readFrom, WrapperQueryBuilder::fromXContent, WrapperQueryBuilder.QUERY_NAME_FIELD); - registerQuery(IndicesQueryBuilder.PROTOTYPE::readFrom, IndicesQueryBuilder::fromXContent, IndicesQueryBuilder.QUERY_NAME_FIELD); - registerQuery(CommonTermsQueryBuilder.PROTOTYPE::readFrom, CommonTermsQueryBuilder::fromXContent, - CommonTermsQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SpanMultiTermQueryBuilder.PROTOTYPE::readFrom, SpanMultiTermQueryBuilder::fromXContent, - SpanMultiTermQueryBuilder.QUERY_NAME_FIELD); - registerQuery(FunctionScoreQueryBuilder.PROTOTYPE::readFrom, c -> FunctionScoreQueryBuilder.fromXContent(scoreFunctionsRegistry, c), + registerQuery(SpanFirstQueryBuilder::new, SpanFirstQueryBuilder::fromXContent, SpanFirstQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanNearQueryBuilder::new, SpanNearQueryBuilder::fromXContent, SpanNearQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanOrQueryBuilder::new, SpanOrQueryBuilder::fromXContent, SpanOrQueryBuilder.QUERY_NAME_FIELD); + registerQuery(MoreLikeThisQueryBuilder::new, MoreLikeThisQueryBuilder::fromXContent, MoreLikeThisQueryBuilder.QUERY_NAME_FIELD); + registerQuery(WrapperQueryBuilder::new, WrapperQueryBuilder::fromXContent, WrapperQueryBuilder.QUERY_NAME_FIELD); + registerQuery(IndicesQueryBuilder::new, IndicesQueryBuilder::fromXContent, IndicesQueryBuilder.QUERY_NAME_FIELD); + registerQuery(CommonTermsQueryBuilder::new, CommonTermsQueryBuilder::fromXContent, CommonTermsQueryBuilder.QUERY_NAME_FIELD); + registerQuery(SpanMultiTermQueryBuilder::new, SpanMultiTermQueryBuilder::fromXContent, SpanMultiTermQueryBuilder.QUERY_NAME_FIELD); + registerQuery(FunctionScoreQueryBuilder::new, c -> FunctionScoreQueryBuilder.fromXContent(scoreFunctionsRegistry, c), FunctionScoreQueryBuilder.QUERY_NAME_FIELD); - registerQuery(SimpleQueryStringBuilder.PROTOTYPE::readFrom, SimpleQueryStringBuilder::fromXContent, - SimpleQueryStringBuilder.QUERY_NAME_FIELD); - registerQuery(TemplateQueryBuilder.PROTOTYPE::readFrom, TemplateQueryBuilder::fromXContent, TemplateQueryBuilder.QUERY_NAME_FIELD); - registerQuery(TypeQueryBuilder.PROTOTYPE::readFrom, TypeQueryBuilder::fromXContent, TypeQueryBuilder.QUERY_NAME_FIELD); - registerQuery(ScriptQueryBuilder.PROTOTYPE::readFrom, ScriptQueryBuilder::fromXContent, ScriptQueryBuilder.QUERY_NAME_FIELD); - registerQuery(GeoDistanceQueryBuilder.PROTOTYPE::readFrom, GeoDistanceQueryBuilder::fromXContent, - GeoDistanceQueryBuilder.QUERY_NAME_FIELD); - registerQuery(GeoDistanceRangeQueryBuilder.PROTOTYPE::readFrom, GeoDistanceRangeQueryBuilder::fromXContent, + registerQuery(SimpleQueryStringBuilder::new, SimpleQueryStringBuilder::fromXContent, SimpleQueryStringBuilder.QUERY_NAME_FIELD); + registerQuery(TemplateQueryBuilder::new, TemplateQueryBuilder::fromXContent, TemplateQueryBuilder.QUERY_NAME_FIELD); + registerQuery(TypeQueryBuilder::new, TypeQueryBuilder::fromXContent, TypeQueryBuilder.QUERY_NAME_FIELD); + registerQuery(ScriptQueryBuilder::new, ScriptQueryBuilder::fromXContent, ScriptQueryBuilder.QUERY_NAME_FIELD); + registerQuery(GeoDistanceQueryBuilder::new, GeoDistanceQueryBuilder::fromXContent, GeoDistanceQueryBuilder.QUERY_NAME_FIELD); + registerQuery(GeoDistanceRangeQueryBuilder::new, GeoDistanceRangeQueryBuilder::fromXContent, GeoDistanceRangeQueryBuilder.QUERY_NAME_FIELD); - registerQuery(GeoBoundingBoxQueryBuilder.PROTOTYPE::readFrom, GeoBoundingBoxQueryBuilder::fromXContent, + registerQuery(GeoBoundingBoxQueryBuilder::new, GeoBoundingBoxQueryBuilder::fromXContent, GeoBoundingBoxQueryBuilder.QUERY_NAME_FIELD); - registerQuery(GeohashCellQuery.Builder.PROTOTYPE::readFrom, GeohashCellQuery.Builder::fromXContent, - GeohashCellQuery.QUERY_NAME_FIELD); - registerQuery(GeoPolygonQueryBuilder.PROTOTYPE::readFrom, GeoPolygonQueryBuilder::fromXContent, - GeoPolygonQueryBuilder.QUERY_NAME_FIELD); - registerQuery(ExistsQueryBuilder.PROTOTYPE::readFrom, ExistsQueryBuilder::fromXContent, ExistsQueryBuilder.QUERY_NAME_FIELD); - registerQuery(MatchNoneQueryBuilder.PROTOTYPE::readFrom, MatchNoneQueryBuilder::fromXContent, - MatchNoneQueryBuilder.QUERY_NAME_FIELD); - registerQuery(ParentIdQueryBuilder.PROTO::readFrom, ParentIdQueryBuilder::fromXContent, ParentIdQueryBuilder.QUERY_NAME_FIELD); - registerQuery(PercolatorQueryBuilder.PROTO::readFrom, PercolatorQueryBuilder::fromXContent, - PercolatorQueryBuilder.QUERY_NAME_FIELD); + registerQuery(GeohashCellQuery.Builder::new, GeohashCellQuery.Builder::fromXContent, GeohashCellQuery.QUERY_NAME_FIELD); + registerQuery(GeoPolygonQueryBuilder::new, GeoPolygonQueryBuilder::fromXContent, GeoPolygonQueryBuilder.QUERY_NAME_FIELD); + registerQuery(ExistsQueryBuilder::new, ExistsQueryBuilder::fromXContent, ExistsQueryBuilder.QUERY_NAME_FIELD); + registerQuery(MatchNoneQueryBuilder::new, MatchNoneQueryBuilder::fromXContent, MatchNoneQueryBuilder.QUERY_NAME_FIELD); + registerQuery(ParentIdQueryBuilder::new, ParentIdQueryBuilder::fromXContent, ParentIdQueryBuilder.QUERY_NAME_FIELD); + registerQuery(PercolatorQueryBuilder::new, PercolatorQueryBuilder::fromXContent, PercolatorQueryBuilder.QUERY_NAME_FIELD); if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) { - registerQuery(GeoShapeQueryBuilder.PROTOTYPE::readFrom, GeoShapeQueryBuilder::fromXContent, - GeoShapeQueryBuilder.QUERY_NAME_FIELD); + registerQuery(GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent, GeoShapeQueryBuilder.QUERY_NAME_FIELD); } // EmptyQueryBuilder is not registered as query parser but used internally. // We need to register it with the NamedWriteableRegistry in order to serialize it - namedWriteableRegistry.registerPrototype(QueryBuilder.class, EmptyQueryBuilder.PROTOTYPE); + namedWriteableRegistry.register(QueryBuilder.class, EmptyQueryBuilder.NAME, EmptyQueryBuilder::new); } static { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java index a11bfd113bb..841f706ba50 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java @@ -210,7 +210,7 @@ public class GeoDistanceAggregatorBuilder extends ValuesSourceAggregatorBuilder< factory.addRange(Range.PROTOTYPE.readFrom(in)); } factory.keyed = in.readBoolean(); - factory.distanceType = GeoDistance.readGeoDistanceFrom(in); + factory.distanceType = GeoDistance.readFromStream(in); factory.unit = DistanceUnit.readFromStream(in); return factory; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregatorBuilder.java index 009a7cf1e61..e9c697527eb 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregatorBuilder.java @@ -522,7 +522,7 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder fieldDataFields = new ArrayList<>(size); @@ -566,7 +566,7 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder public GeoDistanceSortBuilder(StreamInput in) throws IOException { fieldName = in.readString(); points.addAll((List) in.readGenericValue()); - geoDistance = GeoDistance.readGeoDistanceFrom(in); + geoDistance = GeoDistance.readFromStream(in); unit = DistanceUnit.readFromStream(in); order = SortOrder.readFromStream(in); sortMode = in.readOptionalWriteable(SortMode::readFromStream); diff --git a/core/src/test/java/org/elasticsearch/common/geo/GeoDistanceTests.java b/core/src/test/java/org/elasticsearch/common/geo/GeoDistanceTests.java index b10763a95b0..407c9790dbe 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/GeoDistanceTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/GeoDistanceTests.java @@ -47,7 +47,7 @@ public class GeoDistanceTests extends ESTestCase { try (BytesStreamOutput out = new BytesStreamOutput()) { geoDistance.writeTo(out); try (StreamInput in = StreamInput.wrap(out.bytes())) {; - GeoDistance copy = GeoDistance.readGeoDistanceFrom(in); + GeoDistance copy = GeoDistance.readFromStream(in); assertEquals(copy.toString() + " vs. " + geoDistance.toString(), copy, geoDistance); } } @@ -61,7 +61,7 @@ public class GeoDistanceTests extends ESTestCase { out.writeVInt(randomIntBetween(Integer.MIN_VALUE, -1)); } try (StreamInput in = StreamInput.wrap(out.bytes())) { - GeoDistance.readGeoDistanceFrom(in); + GeoDistance.readFromStream(in); } catch (IOException e) { assertThat(e.getMessage(), containsString("Unknown GeoDistance ordinal [")); } diff --git a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 4c45d41be31..7e4dd4f4e19 100644 --- a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -285,6 +285,9 @@ public class BytesStreamsTests extends ESTestCase { out.writeOptionalBytesReference(new BytesArray("test")); out.writeOptionalDouble(null); out.writeOptionalDouble(1.2); + out.writeTimeZone(DateTimeZone.forID("CET")); + out.writeOptionalTimeZone(DateTimeZone.getDefault()); + out.writeOptionalTimeZone(null); final byte[] bytes = out.bytes().toBytes(); StreamInput in = StreamInput.wrap(out.bytes().toBytes()); assertEquals(in.available(), bytes.length); @@ -311,6 +314,9 @@ public class BytesStreamsTests extends ESTestCase { assertThat(in.readOptionalBytesReference(), equalTo(new BytesArray("test"))); assertNull(in.readOptionalDouble()); assertThat(in.readOptionalDouble(), closeTo(1.2, 0.0001)); + assertEquals(DateTimeZone.forID("CET"), in.readTimeZone()); + assertEquals(DateTimeZone.getDefault(), in.readOptionalTimeZone()); + assertNull(in.readOptionalTimeZone()); assertEquals(0, in.available()); in.close(); out.close(); @@ -543,5 +549,4 @@ public class BytesStreamsTests extends ESTestCase { assertEquals(point, geoPoint); } } - } diff --git a/core/src/test/java/org/elasticsearch/common/unit/FuzzinessTests.java b/core/src/test/java/org/elasticsearch/common/unit/FuzzinessTests.java index a6de68b3a46..bc0da6bf1df 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/FuzzinessTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/FuzzinessTests.java @@ -179,6 +179,6 @@ public class FuzzinessTests extends ESTestCase { BytesStreamOutput output = new BytesStreamOutput(); in.writeTo(output); StreamInput streamInput = StreamInput.wrap(output.bytes()); - return Fuzziness.readFuzzinessFrom(streamInput); + return new Fuzziness(streamInput); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java index 7230bb5a783..0dc43e5c22e 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java @@ -656,9 +656,9 @@ public abstract class AbstractQueryTestCase> output.writeQuery(testQuery); try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { QueryBuilder deserializedQuery = in.readQuery(); - assertEquals(deserializedQuery, testQuery); - assertEquals(deserializedQuery.hashCode(), testQuery.hashCode()); - assertNotSame(deserializedQuery, testQuery); + assertEquals(testQuery, deserializedQuery); + assertEquals(testQuery.hashCode(), deserializedQuery.hashCode()); + assertNotSame(testQuery, deserializedQuery); return (QB) deserializedQuery; } } diff --git a/core/src/test/java/org/elasticsearch/index/query/CombineFunctionTests.java b/core/src/test/java/org/elasticsearch/index/query/CombineFunctionTests.java index 94d06097b09..695330c21e2 100644 --- a/core/src/test/java/org/elasticsearch/index/query/CombineFunctionTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/CombineFunctionTests.java @@ -84,37 +84,37 @@ public class CombineFunctionTests extends ESTestCase { try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(0); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(CombineFunction.readCombineFunctionFrom(in), equalTo(CombineFunction.MULTIPLY)); + assertThat(CombineFunction.readFromStream(in), equalTo(CombineFunction.MULTIPLY)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(1); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(CombineFunction.readCombineFunctionFrom(in), equalTo(CombineFunction.REPLACE)); + assertThat(CombineFunction.readFromStream(in), equalTo(CombineFunction.REPLACE)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(2); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(CombineFunction.readCombineFunctionFrom(in), equalTo(CombineFunction.SUM)); + assertThat(CombineFunction.readFromStream(in), equalTo(CombineFunction.SUM)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(3); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(CombineFunction.readCombineFunctionFrom(in), equalTo(CombineFunction.AVG)); + assertThat(CombineFunction.readFromStream(in), equalTo(CombineFunction.AVG)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(4); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(CombineFunction.readCombineFunctionFrom(in), equalTo(CombineFunction.MIN)); + assertThat(CombineFunction.readFromStream(in), equalTo(CombineFunction.MIN)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(5); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(CombineFunction.readCombineFunctionFrom(in), equalTo(CombineFunction.MAX)); + assertThat(CombineFunction.readFromStream(in), equalTo(CombineFunction.MAX)); } } } diff --git a/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java index 5f26c0ce72e..90b692e1db2 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java @@ -96,12 +96,7 @@ public class ConstantScoreQueryBuilderTests extends AbstractQueryTestCase new ConstantScoreQueryBuilder((QueryBuilder) null)); } @Override diff --git a/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java index 69fd843ad42..6bac6392e14 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java @@ -73,16 +73,8 @@ public class ExistsQueryBuilderTests extends AbstractQueryTestCase new ExistsQueryBuilder((String) null)); + expectThrows(IllegalArgumentException.class, () -> new ExistsQueryBuilder("")); } public void testFromJson() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index f14eae7e720..9d531d2cf1f 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -19,9 +19,6 @@ package org.elasticsearch.index.query; -import org.elasticsearch.common.ParseFieldMatcher; -import org.locationtech.spatial4j.io.GeohashUtils; -import org.locationtech.spatial4j.shape.Rectangle; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; @@ -29,10 +26,13 @@ import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.spatial.geopoint.search.GeoPointInBBoxQuery; import org.elasticsearch.Version; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.index.search.geo.InMemoryGeoBoundingBoxQuery; import org.elasticsearch.test.geo.RandomShapeGenerator; +import org.locationtech.spatial4j.io.GeohashUtils; +import org.locationtech.spatial4j.shape.Rectangle; import java.io.IOException; @@ -89,12 +89,8 @@ public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase new GeoBoundingBoxQueryBuilder((String) null)); + assertEquals("Field name must not be empty.", e.getMessage()); } public void testValidationNullType() { diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java index c898a485306..398f4ac449d 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java @@ -81,7 +81,7 @@ public class GeoDistanceQueryBuilderTests extends AbstractQueryTestCase new QueryStringQueryBuilder((String) null)); } public void testToQueryMatchAllQuery() throws Exception { diff --git a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index c2102ebb448..af0dd487536 100644 --- a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -135,16 +135,8 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase new RangeQueryBuilder((String) null)); + expectThrows(IllegalArgumentException.class, () -> new RangeQueryBuilder("")); RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("test"); try { diff --git a/core/src/test/java/org/elasticsearch/index/query/ScoreModeTests.java b/core/src/test/java/org/elasticsearch/index/query/ScoreModeTests.java index 1b56d347557..0b346d59f8b 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ScoreModeTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ScoreModeTests.java @@ -84,37 +84,37 @@ public class ScoreModeTests extends ESTestCase { try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(0); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.FIRST)); + assertThat(FiltersFunctionScoreQuery.ScoreMode.readFromStream(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.FIRST)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(1); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.AVG)); + assertThat(FiltersFunctionScoreQuery.ScoreMode.readFromStream(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.AVG)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(2); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.MAX)); + assertThat(FiltersFunctionScoreQuery.ScoreMode.readFromStream(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.MAX)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(3); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.SUM)); + assertThat(FiltersFunctionScoreQuery.ScoreMode.readFromStream(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.SUM)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(4); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.MIN)); + assertThat(FiltersFunctionScoreQuery.ScoreMode.readFromStream(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.MIN)); } } try (BytesStreamOutput out = new BytesStreamOutput()) { out.writeVInt(5); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat(FiltersFunctionScoreQuery.ScoreMode.readScoreModeFrom(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY)); + assertThat(FiltersFunctionScoreQuery.ScoreMode.readFromStream(in), equalTo(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY)); } } } diff --git a/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index 453244c1545..57bc7fd55d8 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -44,12 +44,7 @@ public class ScriptQueryBuilderTests extends AbstractQueryTestCase new ScriptQueryBuilder((Script) null)); } public void testFromJson() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java index 4bc9911bf93..8853d06fdd3 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java @@ -173,12 +173,7 @@ public class SimpleQueryStringBuilderTests extends AbstractQueryTestCase new SimpleQueryStringBuilder((String) null)); } public void testFieldCannotBeNull() { diff --git a/core/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java index ab03e19f792..cb35b85d3d9 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java @@ -59,12 +59,7 @@ public class SpanMultiTermQueryBuilderTests extends AbstractQueryTestCase new SpanMultiTermQueryBuilder((MultiTermQueryBuilder) null)); } /** diff --git a/core/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java index c3d7be9ae9e..ca8dba8517d 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java @@ -52,12 +52,7 @@ public class SpanOrQueryBuilderTests extends AbstractQueryTestCase new SpanOrQueryBuilder((SpanQueryBuilder) null)); try { SpanOrQueryBuilder spanOrBuilder = new SpanOrQueryBuilder(SpanTermQueryBuilder.PROTOTYPE); diff --git a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java index cf93a5334f6..9426ef1f631 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java @@ -64,12 +64,7 @@ public class TemplateQueryBuilderTests extends AbstractQueryTestCase new TemplateQueryBuilder((Template) null)); } /** diff --git a/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java b/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java index 211aebf869e..54d1026a7f0 100644 --- a/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java +++ b/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java @@ -49,13 +49,28 @@ public class DummyQueryParserPlugin extends Plugin { } public void onModule(SearchModule module) { - module.registerQuery(new DummyQueryBuilder()::readFrom, DummyQueryBuilder::fromXContent, DummyQueryBuilder.QUERY_NAME_FIELD); + module.registerQuery(DummyQueryBuilder::new, DummyQueryBuilder::fromXContent, DummyQueryBuilder.QUERY_NAME_FIELD); } public static class DummyQueryBuilder extends AbstractQueryBuilder { private static final String NAME = "dummy"; private static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); + public DummyQueryBuilder() { + } + + /** + * Read from a stream. + */ + public DummyQueryBuilder(StreamInput in) throws IOException { + super(in); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + // only the superclass has state + } + @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME).endObject(); @@ -72,16 +87,6 @@ public class DummyQueryParserPlugin extends Plugin { return new DummyQuery(context.isFilter()); } - @Override - protected DummyQueryBuilder doReadFrom(StreamInput in) throws IOException { - return new DummyQueryBuilder(); - } - - @Override - protected void doWriteTo(StreamOutput out) throws IOException { - // Do Nothing - } - @Override protected int doHashCode() { return 0; diff --git a/core/src/test/java/org/elasticsearch/indices/TermsLookupTests.java b/core/src/test/java/org/elasticsearch/indices/TermsLookupTests.java index d711402f566..59d86ddce67 100644 --- a/core/src/test/java/org/elasticsearch/indices/TermsLookupTests.java +++ b/core/src/test/java/org/elasticsearch/indices/TermsLookupTests.java @@ -71,7 +71,7 @@ public class TermsLookupTests extends ESTestCase { try (BytesStreamOutput output = new BytesStreamOutput()) { termsLookup.writeTo(output); try (StreamInput in = StreamInput.wrap(output.bytes())) { - TermsLookup deserializedLookup = TermsLookup.readTermsLookupFrom(in); + TermsLookup deserializedLookup = new TermsLookup(in); assertEquals(deserializedLookup, termsLookup); assertEquals(deserializedLookup.hashCode(), termsLookup.hashCode()); assertNotSame(deserializedLookup, termsLookup); diff --git a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java index cd49546d7bc..68fa61b4702 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java @@ -81,7 +81,7 @@ public class SearchModuleTests extends ModuleTestCase { public void testRegisterQueryParserDuplicate() { SearchModule module = new SearchModule(Settings.EMPTY, new NamedWriteableRegistry()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> module - .registerQuery(TermQueryBuilder.PROTOTYPE::readFrom, TermQueryBuilder::fromXContent, TermQueryBuilder.QUERY_NAME_FIELD)); + .registerQuery(TermQueryBuilder::new, TermQueryBuilder::fromXContent, TermQueryBuilder.QUERY_NAME_FIELD)); assertThat(e.getMessage(), containsString("already registered for name [term] while trying to register [org.elasticsearch.")); }