Remove remaining PROTOTYPEs from org.elasticsearch.search.suggest

This commit is contained in:
Nik Everett 2016-04-13 16:56:43 -04:00
parent c595322d90
commit 8ad0070cea
9 changed files with 34 additions and 43 deletions

View File

@ -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();

View File

@ -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<SuggestBuilder> {
public static final SuggestBuilder PROTOTYPE = new SuggestBuilder();
protected static final ParseField GLOBAL_TEXT_FIELD = new ParseField("text");
private String globalText;
private final Map<String, SuggestionBuilder<?>> 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<String, SuggestionBuilder<?>> 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<Sugge
return suggestionSearchContext;
}
@Override
public SuggestBuilder readFrom(StreamInput in) throws IOException {
final SuggestBuilder builder = new SuggestBuilder();
builder.globalText = in.readOptionalString();
final int size = in.readVInt();
for (int i = 0; i < size; i++) {
builder.suggestions.put(in.readString(), in.readSuggestion());
}
return builder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(globalText);
final int size = suggestions.size();
out.writeVInt(size);
for (Entry<String, SuggestionBuilder<?>> suggestion : suggestions.entrySet()) {
out.writeString(suggestion.getKey());
out.writeSuggestion(suggestion.getValue());
}
}
@Override
public boolean equals(Object other) {
if (this == other) {

View File

@ -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;

View File

@ -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;

View File

@ -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());
}

View File

@ -32,7 +32,7 @@ import java.util.Map;
public class CustomSuggester extends Suggester<CustomSuggester.CustomSuggestionsContext> {
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

View File

@ -37,7 +37,7 @@ public class CustomSuggesterPlugin extends Plugin {
}
public void onModule(SearchModule searchModule) {
searchModule.registerSuggester("custom", CustomSuggester.PROTOTYPE);
searchModule.registerSuggester("custom", CustomSuggester.INSTANCE);
}
}

View File

@ -91,8 +91,6 @@ public class CustomSuggesterSearchIT extends ESIntegTestCase {
}
public static class CustomSuggestionBuilder extends SuggestionBuilder<CustomSuggestionBuilder> {
public final static CustomSuggestionBuilder PROTOTYPE = new CustomSuggestionBuilder("_na_", "_na_");
protected static final ParseField RANDOM_SUFFIX_FIELD = new ParseField("suffix");
private String randomSuffix;

View File

@ -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<SuggestBuilder> {
@ -134,7 +126,7 @@ public class SuggestBuilderTests extends WritableTestCase<SuggestBuilder> {
@Override
protected SuggestBuilder readFrom(StreamInput in) throws IOException {
return SuggestBuilder.PROTOTYPE.readFrom(in);
return new SuggestBuilder(in);
}
public static SuggestBuilder randomSuggestBuilder() {