diff --git a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index 05aebb0ca47..053c4af5382 100644 --- a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -252,7 +252,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ } } if (in.readBoolean()) { - suggestBuilder = SuggestBuilder.PROTOTYPE.readFrom(in); + suggestBuilder = new SuggestBuilder(in); } terminateAfter = in.readVInt(); timeoutInMillis = in.readLong(); diff --git a/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilder.java index aed44c426d3..ff56f395a5c 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/SuggestBuilder.java @@ -47,13 +47,39 @@ import java.util.Objects; * to the terms in provided text. These suggestions are based on several options described in this class. */ public class SuggestBuilder extends ToXContentToBytes implements Writeable { - - public static final SuggestBuilder PROTOTYPE = new SuggestBuilder(); protected static final ParseField GLOBAL_TEXT_FIELD = new ParseField("text"); private String globalText; private final Map> suggestions = new HashMap<>(); + /** + * Build an empty SuggestBuilder. + */ + public SuggestBuilder() { + } + + /** + * Read from a stream. + */ + public SuggestBuilder(StreamInput in) throws IOException { + globalText = in.readOptionalString(); + final int size = in.readVInt(); + for (int i = 0; i < size; i++) { + suggestions.put(in.readString(), in.readSuggestion()); + } + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeOptionalString(globalText); + final int size = suggestions.size(); + out.writeVInt(size); + for (Entry> suggestion : suggestions.entrySet()) { + out.writeString(suggestion.getKey()); + out.writeSuggestion(suggestion.getValue()); + } + } + /** * Sets the text to provide suggestions for. The suggest text is a required option that needs * to be set either via this setter or via the {@link org.elasticsearch.search.suggest.SuggestionBuilder#text(String)} method. @@ -161,28 +187,6 @@ public class SuggestBuilder extends ToXContentToBytes implements Writeable> suggestion : suggestions.entrySet()) { - out.writeString(suggestion.getKey()); - out.writeSuggestion(suggestion.getValue()); - } - } - @Override public boolean equals(Object other) { if (this == other) { 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 2e477950106..62892216c68 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 @@ -38,7 +38,6 @@ import static org.elasticsearch.search.suggest.completion.context.CategoryContex */ public final class CategoryQueryContext implements ToXContent { public static final String NAME = "category"; - public static final CategoryQueryContext PROTOTYPE = new CategoryQueryContext("", 1, false); private final String category; private final boolean isPrefix; 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 43873724c0b..1d27f171594 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 @@ -43,7 +43,6 @@ import static org.elasticsearch.search.suggest.completion.context.GeoContextMapp */ public final class GeoQueryContext implements ToXContent { public static final String NAME = "geo"; - public static final GeoQueryContext PROTOTYPE = new GeoQueryContext(null, 1, 12, Collections.emptyList()); private final GeoPoint geoPoint; private final int boost; diff --git a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java index 67ae0d9463f..31d9722eced 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.common.inject.ModuleTestCase; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.common.xcontent.XContentLocation; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParser; import org.elasticsearch.index.query.TermQueryBuilder; @@ -65,9 +64,9 @@ public class SearchModuleTests extends ModuleTestCase { public void testRegisterSuggester() { SearchModule module = new SearchModule(Settings.EMPTY, new NamedWriteableRegistry()); - module.registerSuggester("custom", CustomSuggester.PROTOTYPE); + module.registerSuggester("custom", CustomSuggester.INSTANCE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> module.registerSuggester("custom", CustomSuggester.PROTOTYPE)); + () -> module.registerSuggester("custom", CustomSuggester.INSTANCE)); assertEquals("Can't register the same [suggester] more than once for [custom]", e.getMessage()); } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggester.java b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggester.java index 00c9ef9b8b9..9c3e0fe3428 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggester.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggester.java @@ -32,7 +32,7 @@ import java.util.Map; public class CustomSuggester extends Suggester { - public static CustomSuggester PROTOTYPE = new CustomSuggester(); + public static CustomSuggester INSTANCE = new CustomSuggester(); // This is a pretty dumb implementation which returns the original text + fieldName + custom config option + 12 or 123 @Override diff --git a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterPlugin.java b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterPlugin.java index 19d6ed4e098..eb12c4def70 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterPlugin.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterPlugin.java @@ -37,7 +37,7 @@ public class CustomSuggesterPlugin extends Plugin { } public void onModule(SearchModule searchModule) { - searchModule.registerSuggester("custom", CustomSuggester.PROTOTYPE); + searchModule.registerSuggester("custom", CustomSuggester.INSTANCE); } } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java index 19837e577d8..57dacf036a9 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/CustomSuggesterSearchIT.java @@ -91,8 +91,6 @@ public class CustomSuggesterSearchIT extends ESIntegTestCase { } public static class CustomSuggestionBuilder extends SuggestionBuilder { - - public final static CustomSuggestionBuilder PROTOTYPE = new CustomSuggestionBuilder("_na_", "_na_"); protected static final ParseField RANDOM_SUFFIX_FIELD = new ParseField("suffix"); private String randomSuffix; diff --git a/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java index 4086df0943e..2ce075cc60e 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java @@ -31,21 +31,13 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.suggest.completion.CompletionSuggesterBuilderTests; -import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.completion.WritableTestCase; -import org.elasticsearch.search.suggest.phrase.Laplace; -import org.elasticsearch.search.suggest.phrase.LinearInterpolation; -import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilderTests; -import org.elasticsearch.search.suggest.phrase.SmoothingModel; -import org.elasticsearch.search.suggest.phrase.StupidBackoff; -import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilderTests; import org.junit.AfterClass; import org.junit.BeforeClass; import java.io.IOException; -import java.util.Collections; import java.util.Map.Entry; public class SuggestBuilderTests extends WritableTestCase { @@ -134,7 +126,7 @@ public class SuggestBuilderTests extends WritableTestCase { @Override protected SuggestBuilder readFrom(StreamInput in) throws IOException { - return SuggestBuilder.PROTOTYPE.readFrom(in); + return new SuggestBuilder(in); } public static SuggestBuilder randomSuggestBuilder() {