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 3f44b61f212..d00499798b5 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 @@ -39,6 +39,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder; import org.elasticsearch.search.rescore.RescoreBuilder; import org.elasticsearch.search.suggest.SuggestionBuilder; +import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.SmoothingModel; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -706,6 +707,13 @@ public abstract class StreamInput extends InputStream { return readNamedWriteable(ScoreFunctionBuilder.class); } + /** + * Reads a {@link SmoothingModel} from the current stream + */ + public SmoothingModel readSmoothingModel() throws IOException { + return readNamedWriteable(SmoothingModel.class); + } + /** * Reads a list of objects */ 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 5e0af597b09..e903ddae1dc 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 @@ -38,6 +38,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder; import org.elasticsearch.search.rescore.RescoreBuilder; import org.elasticsearch.search.suggest.SuggestionBuilder; +import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.SmoothingModel; import org.joda.time.ReadableInstant; import java.io.EOFException; @@ -670,6 +671,13 @@ public abstract class StreamOutput extends OutputStream { writeNamedWriteable(scoreFunctionBuilder); } + /** + * Writes the given {@link SmoothingModel} to the stream + */ + public void writeSmoothingModel(SmoothingModel smoothinModel) throws IOException { + writeNamedWriteable(smoothinModel); + } + /** * Writes the given {@link GeoPoint} to the stream */ diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java index 09d108b6356..a03f9109eda 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java @@ -216,6 +216,13 @@ public final class PhraseSuggestionBuilder extends SuggestionBuilder> extends ESTestCase { private static final int NUMBER_OF_TESTBUILDERS = 20; - private static NamedWriteableRegistry namedWriteableRegistry; + protected static NamedWriteableRegistry namedWriteableRegistry; /** * setup for the whole base test class diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/LaplaceModelTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/LaplaceModelTests.java index 87ad654e0cd..1a939018b8f 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/LaplaceModelTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/LaplaceModelTests.java @@ -28,6 +28,11 @@ public class LaplaceModelTests extends SmoothingModelTestCase { @Override protected SmoothingModel createTestModel() { + return createRandomModel(); + } + + + static SmoothingModel createRandomModel() { return new Laplace(randomDoubleBetween(0.0, 10.0, false)); } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/LinearInterpolationModelTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/LinearInterpolationModelTests.java index 1112b7a5ed7..7984395abcc 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/LinearInterpolationModelTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/LinearInterpolationModelTests.java @@ -28,6 +28,10 @@ public class LinearInterpolationModelTests extends SmoothingModelTestCase { @Override protected SmoothingModel createTestModel() { + return createRandomModel(); + } + + static LinearInterpolation createRandomModel() { double trigramLambda = randomDoubleBetween(0.0, 10.0, false); double bigramLambda = randomDoubleBetween(0.0, 10.0, false); double unigramLambda = randomDoubleBetween(0.0, 10.0, false); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java index 74f655b6aa1..63fb937f3d0 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java @@ -21,6 +21,11 @@ package org.elasticsearch.search.suggest.phrase; import org.elasticsearch.script.Template; import org.elasticsearch.search.suggest.AbstractSuggestionBuilderTestCase; +import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.Laplace; +import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.LinearInterpolation; +import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.SmoothingModel; +import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder.StupidBackoff; +import org.junit.BeforeClass; import java.io.IOException; import java.util.HashMap; @@ -28,6 +33,13 @@ import java.util.Map; public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestCase { + @BeforeClass + public static void initSmoothingModels() { + namedWriteableRegistry.registerPrototype(SmoothingModel.class, Laplace.PROTOTYPE); + namedWriteableRegistry.registerPrototype(SmoothingModel.class, LinearInterpolation.PROTOTYPE); + namedWriteableRegistry.registerPrototype(SmoothingModel.class, StupidBackoff.PROTOTYPE); + } + @Override protected PhraseSuggestionBuilder randomSuggestionBuilder() { PhraseSuggestionBuilder testBuilder = new PhraseSuggestionBuilder(randomAsciiOfLength(10)); @@ -50,7 +62,7 @@ public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestC testBuilder.collateParams(collateParams ); } if (randomBoolean()) { - // NORELEASE add random model + randomSmoothingModel(); } if (randomBoolean()) { @@ -59,6 +71,22 @@ public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestC return testBuilder; } + private static SmoothingModel randomSmoothingModel() { + SmoothingModel model = null; + switch (randomIntBetween(0,2)) { + case 0: + model = LaplaceModelTests.createRandomModel(); + break; + case 1: + model = StupidBackoffModelTests.createRandomModel(); + break; + case 2: + model = LinearInterpolationModelTests.createRandomModel(); + break; + } + return model; + } + @Override protected void mutateSpecificParameters(PhraseSuggestionBuilder builder) throws IOException { switch (randomIntBetween(0, 7)) { @@ -107,6 +135,9 @@ public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestC case 10: builder.collateParams().put(randomAsciiOfLength(5), randomAsciiOfLength(5)); break; + case 11: + builder.smoothingModel(randomValueOtherThan(builder.smoothingModel(), PhraseSuggestionBuilderTests::randomSmoothingModel)); + break; // TODO mutate random Model && generator } } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/StupidBackoffModelTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/StupidBackoffModelTests.java index c3bd66d2a81..3a59c19b13e 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/StupidBackoffModelTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/StupidBackoffModelTests.java @@ -28,6 +28,10 @@ public class StupidBackoffModelTests extends SmoothingModelTestCase { @Override protected SmoothingModel createTestModel() { + return createRandomModel(); + } + + static SmoothingModel createRandomModel() { return new StupidBackoff(randomDoubleBetween(0.0, 10.0, false)); }