diff options
author | Nik Everett <nik9000@gmail.com> | 2016-03-28 11:06:03 -0400 |
---|---|---|
committer | Nik Everett <nik9000@gmail.com> | 2016-03-29 17:55:01 -0400 |
commit | df08854c60a2bf1402377615e9bce644e6304e0b (patch) | |
tree | ba5090acaadd0bcc09069f2a94c417117151d49f /core/src/main/java/org/elasticsearch/search/suggest/completion | |
parent | 101a32573c0eeb3abe70cc1ead569b719ddbef75 (diff) |
Remove PROTOTYPEs from suggesters
Also stops using guice for suggesters at all and lots of checkstyle.
Diffstat (limited to 'core/src/main/java/org/elasticsearch/search/suggest/completion')
11 files changed, 84 insertions, 154 deletions
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java index cef0a33fdd..8bf35a34b2 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java @@ -30,12 +30,14 @@ import org.apache.lucene.search.suggest.document.TopSuggestDocs; import org.apache.lucene.search.suggest.document.TopSuggestDocsCollector; import org.apache.lucene.util.CharsRefBuilder; import org.apache.lucene.util.PriorityQueue; +import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.fielddata.AtomicFieldData; import org.elasticsearch.index.fielddata.ScriptDocValues; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.core.CompletionFieldMapper; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.search.suggest.Suggester; @@ -52,7 +54,9 @@ import java.util.Set; public class CompletionSuggester extends Suggester<CompletionSuggestionContext> { - public static final CompletionSuggester PROTOTYPE = new CompletionSuggester(); + public static final CompletionSuggester INSTANCE = new CompletionSuggester(); + + private CompletionSuggester() {} @Override protected Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> innerExecute(String name, @@ -267,7 +271,12 @@ public class CompletionSuggester extends Suggester<CompletionSuggestionContext> } @Override - public SuggestionBuilder<?> getBuilderPrototype() { - return CompletionSuggestionBuilder.PROTOTYPE; + public SuggestionBuilder<?> innerFromXContent(QueryParseContext context) throws IOException { + return CompletionSuggestionBuilder.innerFromXContent(context); + } + + @Override + public SuggestionBuilder<?> read(StreamInput in) throws IOException { + return new CompletionSuggestionBuilder(in); } } diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java index ca8aad7c8a..a4d2b59844 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java @@ -40,7 +40,6 @@ import org.elasticsearch.search.suggest.SuggestionBuilder; import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext; import org.elasticsearch.search.suggest.completion.context.ContextMapping; import org.elasticsearch.search.suggest.completion.context.ContextMappings; -import org.elasticsearch.search.suggest.completion.context.QueryContext; import java.io.IOException; import java.util.ArrayList; @@ -57,8 +56,6 @@ import java.util.Objects; * indexing. */ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSuggestionBuilder> { - - public static final CompletionSuggestionBuilder PROTOTYPE = new CompletionSuggestionBuilder("_na_"); static final String SUGGESTION_NAME = "completion"; static final ParseField PAYLOAD_FIELD = new ParseField("payload"); static final ParseField CONTEXTS_FIELD = new ParseField("contexts", "context"); @@ -125,6 +122,26 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug } /** + * Read from a stream. + */ + public CompletionSuggestionBuilder(StreamInput in) throws IOException { + super(in); + payloadFields = new ArrayList<>(); + Collections.addAll(payloadFields, in.readStringArray()); + fuzzyOptions = in.readOptionalWriteable(FuzzyOptions::new); + regexOptions = in.readOptionalWriteable(RegexOptions::new); + contextBytes = in.readOptionalBytesReference(); + } + + @Override + public void doWriteTo(StreamOutput out) throws IOException { + out.writeStringArray(payloadFields.toArray(new String[payloadFields.size()])); + out.writeOptionalWriteable(fuzzyOptions); + out.writeOptionalWriteable(regexOptions); + out.writeOptionalBytesReference(contextBytes); + } + + /** * Sets the prefix to provide completions for. * The prefix gets analyzed by the suggest analyzer. */ @@ -188,12 +205,12 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug * see {@link org.elasticsearch.search.suggest.completion.context.CategoryQueryContext} * and {@link org.elasticsearch.search.suggest.completion.context.GeoQueryContext} */ - public CompletionSuggestionBuilder contexts(Map<String, List<? extends QueryContext>> queryContexts) { + public CompletionSuggestionBuilder contexts(Map<String, List<? extends ToXContent>> queryContexts) { Objects.requireNonNull(queryContexts, "contexts must not be null"); try { XContentBuilder contentBuilder = XContentFactory.jsonBuilder(); contentBuilder.startObject(); - for (Map.Entry<String, List<? extends QueryContext>> contextEntry : queryContexts.entrySet()) { + for (Map.Entry<String, List<? extends ToXContent>> contextEntry : queryContexts.entrySet()) { contentBuilder.startArray(contextEntry.getKey()); for (ToXContent queryContext : contextEntry.getValue()) { queryContext.toXContent(contentBuilder, EMPTY_PARAMS); @@ -244,8 +261,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug return builder; } - @Override - protected CompletionSuggestionBuilder innerFromXContent(QueryParseContext parseContext) throws IOException { + static CompletionSuggestionBuilder innerFromXContent(QueryParseContext parseContext) throws IOException { CompletionSuggestionBuilder.InnerBuilder builder = new CompletionSuggestionBuilder.InnerBuilder(); TLP_PARSER.parse(parseContext.parser(), builder); String field = builder.field; @@ -304,52 +320,6 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug } @Override - public void doWriteTo(StreamOutput out) throws IOException { - out.writeBoolean(payloadFields.isEmpty() == false); - if (payloadFields.isEmpty() == false) { - out.writeVInt(payloadFields.size()); - for (String payloadField : payloadFields) { - out.writeString(payloadField); - } - } - out.writeBoolean(fuzzyOptions != null); - if (fuzzyOptions != null) { - fuzzyOptions.writeTo(out); - } - out.writeBoolean(regexOptions != null); - if (regexOptions != null) { - regexOptions.writeTo(out); - } - out.writeBoolean(contextBytes != null); - if (contextBytes != null) { - out.writeBytesReference(contextBytes); - } - } - - @Override - public CompletionSuggestionBuilder doReadFrom(StreamInput in, String field) throws IOException { - CompletionSuggestionBuilder completionSuggestionBuilder = new CompletionSuggestionBuilder(field); - if (in.readBoolean()) { - int numPayloadField = in.readVInt(); - List<String> payloadFields = new ArrayList<>(numPayloadField); - for (int i = 0; i < numPayloadField; i++) { - payloadFields.add(in.readString()); - } - completionSuggestionBuilder.payloadFields = payloadFields; - } - if (in.readBoolean()) { - completionSuggestionBuilder.fuzzyOptions = FuzzyOptions.readFuzzyOptions(in); - } - if (in.readBoolean()) { - completionSuggestionBuilder.regexOptions = RegexOptions.readRegexOptions(in); - } - if (in.readBoolean()) { - completionSuggestionBuilder.contextBytes = in.readBytesReference(); - } - return completionSuggestionBuilder; - } - - @Override protected boolean doEquals(CompletionSuggestionBuilder other) { return Objects.equals(payloadFields, other.payloadFields) && Objects.equals(fuzzyOptions, other.fuzzyOptions) && diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionContext.java index f9f948a551..1941bc9fb8 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionContext.java @@ -36,7 +36,7 @@ import java.util.Map; public class CompletionSuggestionContext extends SuggestionSearchContext.SuggestionContext { protected CompletionSuggestionContext(QueryShardContext shardContext) { - super(CompletionSuggester.PROTOTYPE, shardContext); + super(CompletionSuggester.INSTANCE, shardContext); } private CompletionFieldMapper.CompletionFieldType fieldType; diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java index 8f05be0469..ed2efdf456 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/FuzzyOptions.java @@ -89,7 +89,26 @@ public class FuzzyOptions implements ToXContent, Writeable<FuzzyOptions> { this.maxDeterminizedStates = maxDeterminizedStates; } - private FuzzyOptions() { + /** + * Read from a stream. + */ + FuzzyOptions(StreamInput in) throws IOException { + transpositions = in.readBoolean(); + unicodeAware = in.readBoolean(); + editDistance = in.readVInt(); + fuzzyMinLength = in.readVInt(); + fuzzyPrefixLength = in.readVInt(); + maxDeterminizedStates = in.readVInt(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeBoolean(transpositions); + out.writeBoolean(unicodeAware); + out.writeVInt(editDistance); + out.writeVInt(fuzzyMinLength); + out.writeVInt(fuzzyPrefixLength); + out.writeVInt(maxDeterminizedStates); } static FuzzyOptions parse(XContentParser parser) throws IOException { @@ -185,33 +204,6 @@ public class FuzzyOptions implements ToXContent, Writeable<FuzzyOptions> { return builder; } - public static FuzzyOptions readFuzzyOptions(StreamInput in) throws IOException { - FuzzyOptions fuzzyOptions = new FuzzyOptions(); - fuzzyOptions.readFrom(in); - return fuzzyOptions; - } - - @Override - public FuzzyOptions readFrom(StreamInput in) throws IOException { - this.transpositions = in.readBoolean(); - this.unicodeAware = in.readBoolean(); - this.editDistance = in.readVInt(); - this.fuzzyMinLength = in.readVInt(); - this.fuzzyPrefixLength = in.readVInt(); - this.maxDeterminizedStates = in.readVInt(); - return this; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeBoolean(transpositions); - out.writeBoolean(unicodeAware); - out.writeVInt(editDistance); - out.writeVInt(fuzzyMinLength); - out.writeVInt(fuzzyPrefixLength); - out.writeVInt(maxDeterminizedStates); - } - /** * Options for fuzzy queries */ diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java index 8503dbdf46..58464cddee 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/RegexOptions.java @@ -67,15 +67,26 @@ public class RegexOptions implements ToXContent, Writeable<RegexOptions> { private int flagsValue; private int maxDeterminizedStates; - private RegexOptions() { - } - private RegexOptions(int flagsValue, int maxDeterminizedStates) { this.flagsValue = flagsValue; this.maxDeterminizedStates = maxDeterminizedStates; } /** + * Read from a stream. + */ + RegexOptions(StreamInput in) throws IOException { + this.flagsValue = in.readVInt(); + this.maxDeterminizedStates = in.readVInt(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeVInt(flagsValue); + out.writeVInt(maxDeterminizedStates); + } + + /** * Returns internal regular expression syntax flag value * see {@link RegexpFlag#value()} */ @@ -126,25 +137,6 @@ public class RegexOptions implements ToXContent, Writeable<RegexOptions> { return builder; } - public static RegexOptions readRegexOptions(StreamInput in) throws IOException { - RegexOptions regexOptions = new RegexOptions(); - regexOptions.readFrom(in); - return regexOptions; - } - - @Override - public RegexOptions readFrom(StreamInput in) throws IOException { - this.flagsValue = in.readVInt(); - this.maxDeterminizedStates = in.readVInt(); - return this; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(flagsValue); - out.writeVInt(maxDeterminizedStates); - } - /** * Options for regular expression queries */ diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java index c9cb165aef..1931c4a9e0 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java @@ -139,8 +139,8 @@ public class CategoryContextMapping extends ContextMapping<CategoryQueryContext> } @Override - protected CategoryQueryContext prototype() { - return CategoryQueryContext.PROTOTYPE; + protected CategoryQueryContext fromXContent(XContentParser parser) throws IOException { + return CategoryQueryContext.fromXContent(parser); } /** diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java index a164faff8b..2e47795010 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.suggest.completion.context; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -35,7 +36,7 @@ import static org.elasticsearch.search.suggest.completion.context.CategoryContex /** * Defines the query context for {@link CategoryContextMapping} */ -public final class CategoryQueryContext implements QueryContext { +public final class CategoryQueryContext implements ToXContent { public static final String NAME = "category"; public static final CategoryQueryContext PROTOTYPE = new CategoryQueryContext("", 1, false); @@ -102,8 +103,7 @@ public final class CategoryQueryContext implements QueryContext { CATEGORY_PARSER.declareBoolean(Builder::setPrefix, new ParseField(CONTEXT_PREFIX)); } - @Override - public CategoryQueryContext fromXContext(XContentParser parser) throws IOException { + public static CategoryQueryContext fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); Builder builder = builder(); if (token == XContentParser.Token.START_OBJECT) { diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java index 959a749a85..cb445b1770 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java @@ -40,7 +40,7 @@ import java.util.Set; * * Implementations have to define how contexts are parsed at query/index time */ -public abstract class ContextMapping<T extends QueryContext> implements ToXContent { +public abstract class ContextMapping<T extends ToXContent> implements ToXContent { public static final String FIELD_TYPE = "type"; public static final String FIELD_NAME = "name"; @@ -99,7 +99,7 @@ public abstract class ContextMapping<T extends QueryContext> implements ToXConte /** * Prototype for the query context */ - protected abstract T prototype(); + protected abstract T fromXContent(XContentParser parser) throws IOException; /** * Parses query contexts for this mapper @@ -108,10 +108,10 @@ public abstract class ContextMapping<T extends QueryContext> implements ToXConte List<T> queryContexts = new ArrayList<>(); Token token = parser.nextToken(); if (token == Token.START_OBJECT || token == Token.VALUE_STRING) { - queryContexts.add((T) prototype().fromXContext(parser)); + queryContexts.add(fromXContent(parser)); } else if (token == Token.START_ARRAY) { while (parser.nextToken() != Token.END_ARRAY) { - queryContexts.add((T) prototype().fromXContext(parser)); + queryContexts.add(fromXContent(parser)); } } return toInternalQueryContexts(queryContexts); diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java index 41d78e7535..393f798c85 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java @@ -223,8 +223,8 @@ public class GeoContextMapping extends ContextMapping<GeoQueryContext> { } @Override - protected GeoQueryContext prototype() { - return GeoQueryContext.PROTOTYPE; + protected GeoQueryContext fromXContent(XContentParser parser) throws IOException { + return GeoQueryContext.fromXContent(parser); } /** diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java index 913702c18d..43873724c0 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.ParseField; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -40,7 +41,7 @@ import static org.elasticsearch.search.suggest.completion.context.GeoContextMapp /** * Defines the query context for {@link GeoContextMapping} */ -public final class GeoQueryContext implements QueryContext { +public final class GeoQueryContext implements ToXContent { public static final String NAME = "geo"; public static final GeoQueryContext PROTOTYPE = new GeoQueryContext(null, 1, 12, Collections.emptyList()); @@ -123,8 +124,7 @@ public final class GeoQueryContext implements QueryContext { GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext.Builder::setLon, new ParseField("lon")); } - @Override - public GeoQueryContext fromXContext(XContentParser parser) throws IOException { + public static GeoQueryContext fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); GeoQueryContext.Builder builder = new Builder(); if (token == XContentParser.Token.START_OBJECT) { diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/QueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/QueryContext.java deleted file mode 100644 index 9d96bf8144..0000000000 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/QueryContext.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.search.suggest.completion.context; - -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; - -import java.io.IOException; - -/** - * Interface for serializing/de-serializing completion query context - */ -public interface QueryContext extends ToXContent { - - QueryContext fromXContext(XContentParser parser) throws IOException; -} |