Skip to content

Commit 5c1a910

Browse files
committed
[Tests] Add tests for PhraseSuggestionBuilder#build()
1 parent 31f73cc commit 5c1a910

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ public PhraseSuggestionBuilder clearCandidateGenerators() {
311311
return this;
312312
}
313313

314+
/**
315+
* get the candidate generators.
316+
*/
317+
Map<String, List<CandidateGenerator>> getCandidateGenerators() {
318+
return this.generators;
319+
}
320+
314321
/**
315322
* If set to <code>true</code> the phrase suggester will fail if the analyzer only
316323
* produces ngrams. the default it <code>true</code>.
@@ -607,7 +614,6 @@ public SuggestionContext build(QueryShardContext context) throws IOException {
607614
suggestionContext.setRealWordErrorLikelihood(this.realWordErrorLikelihood);
608615
suggestionContext.setConfidence(this.confidence);
609616
suggestionContext.setMaxErrors(this.maxErrors);
610-
suggestionContext.setSeparator(BytesRefs.toBytesRef(this.separator));
611617
suggestionContext.setRequireUnigram(this.forceUnigrams);
612618
suggestionContext.setTokenLimit(this.tokenLimit);
613619
suggestionContext.setPreTag(BytesRefs.toBytesRef(this.preTag));

core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import org.elasticsearch.index.mapper.MappedFieldType;
3939
import org.elasticsearch.index.mapper.MapperService;
4040
import org.elasticsearch.index.query.QueryShardContext;
41+
import org.elasticsearch.ingest.TestTemplateService;
4142
import org.elasticsearch.mock.orig.Mockito;
4243
import org.elasticsearch.script.Script;
4344
import org.elasticsearch.script.ScriptService;
44-
import org.elasticsearch.script.TemplateScript;
4545
import org.elasticsearch.search.SearchModule;
4646
import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext;
4747
import org.elasticsearch.test.ESTestCase;
@@ -175,7 +175,8 @@ public void testBuild() throws IOException {
175175
when(mapperService.fullName(any(String.class))).thenReturn(fieldType);
176176
when(mapperService.getNamedAnalyzer(any(String.class))).then(
177177
invocation -> new NamedAnalyzer((String) invocation.getArguments()[0], AnalyzerScope.INDEX, new SimpleAnalyzer()));
178-
when(scriptService.compile(any(Script.class), any())).thenReturn(mock(TemplateScript.Factory.class));
178+
when(scriptService.compile(any(Script.class), any())).then(invocation -> new TestTemplateService.MockTemplateScript.Factory(
179+
((Script) invocation.getArguments()[0]).getIdOrCode()));
179180
QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, mapperService, null, scriptService,
180181
xContentRegistry(), null, null, System::currentTimeMillis);
181182

core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe
4646
private static final Map<String, List<? extends ToXContent>> contextMap = new HashMap<>();
4747
private static String categoryContextName;
4848
private static String geoQueryContextName;
49-
private static List<ContextMapping> contextMappings;
49+
private static List<ContextMapping> contextMappings = new ArrayList<>();
5050

5151
@Override
5252
protected CompletionSuggestionBuilder randomSuggestionBuilder() {
@@ -66,6 +66,18 @@ public static CompletionSuggestionBuilder randomCompletionSuggestionBuilder() {
6666
contextMappings = Arrays.asList(new ContextMapping[] { ContextBuilder.category(categoryContextName).build(),
6767
ContextBuilder.geo(geoQueryContextName).build() });
6868
}
69+
// lazy initialization of context names and mappings, cannot be done in some init method because other test
70+
// also create random CompletionSuggestionBuilder instances
71+
if (categoryContextName == null) {
72+
categoryContextName = randomAlphaOfLength(10);
73+
}
74+
if (geoQueryContextName == null) {
75+
geoQueryContextName = randomAlphaOfLength(10);
76+
}
77+
if (contextMappings.isEmpty()) {
78+
contextMappings.add(ContextBuilder.category(categoryContextName).build());
79+
contextMappings.add(ContextBuilder.geo(geoQueryContextName).build());
80+
}
6981
CompletionSuggestionBuilder testBuilder = new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(2, 20));
7082
setCommonPropertiesOnRandomBuilder(testBuilder);
7183
switch (randomIntBetween(0, 3)) {

core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.HashMap;
2828
import java.util.Map;
2929

30+
import static org.hamcrest.Matchers.instanceOf;
31+
3032
public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestCase<PhraseSuggestionBuilder> {
3133
@Override
3234
protected PhraseSuggestionBuilder randomSuggestionBuilder() {
@@ -187,5 +189,41 @@ public void testInvalidParameters() {
187189

188190
@Override
189191
protected void assertSuggester(PhraseSuggestionBuilder builder, SuggestionContext context) {
192+
assertThat(context, instanceOf(PhraseSuggestionContext.class));
193+
assertThat(context.getSuggester(), instanceOf(PhraseSuggester.class));
194+
PhraseSuggestionContext phraseSuggesterCtx = (PhraseSuggestionContext) context;
195+
assertOptionalEquals(builder.confidence(), phraseSuggesterCtx.confidence(), PhraseSuggestionContext.DEFAULT_CONFIDENCE);
196+
assertOptionalEquals(builder.collatePrune(), phraseSuggesterCtx.collatePrune(), PhraseSuggestionContext.DEFAULT_COLLATE_PRUNE);
197+
assertEquals(builder.separator(), phraseSuggesterCtx.separator().utf8ToString());
198+
assertOptionalEquals(builder.realWordErrorLikelihood(), phraseSuggesterCtx.realworldErrorLikelyhood(),
199+
PhraseSuggestionContext.DEFAULT_RWE_ERRORLIKELIHOOD);
200+
assertOptionalEquals(builder.maxErrors(), phraseSuggesterCtx.maxErrors(), PhraseSuggestionContext.DEFAULT_MAX_ERRORS);
201+
assertOptionalEquals(builder.forceUnigrams(), phraseSuggesterCtx.getRequireUnigram(),
202+
PhraseSuggestionContext.DEFAULT_REQUIRE_UNIGRAM);
203+
assertOptionalEquals(builder.tokenLimit(), phraseSuggesterCtx.getTokenLimit(), NoisyChannelSpellChecker.DEFAULT_TOKEN_LIMIT);
204+
assertEquals(builder.preTag(), phraseSuggesterCtx.getPreTag() != null ? phraseSuggesterCtx.getPreTag().utf8ToString() : null);
205+
assertEquals(builder.postTag(), phraseSuggesterCtx.getPostTag() != null ? phraseSuggesterCtx.getPostTag().utf8ToString() : null);
206+
assertOptionalEquals(builder.gramSize(), phraseSuggesterCtx.gramSize(), PhraseSuggestionContext.DEFAULT_GRAM_SIZE);
207+
if (builder.collateQuery() != null) {
208+
assertEquals(builder.collateQuery().getIdOrCode(), phraseSuggesterCtx.getCollateQueryScript().newInstance(null).execute());
209+
}
210+
if (builder.collateParams() != null) {
211+
assertEquals(builder.collateParams(), phraseSuggesterCtx.getCollateScriptParams());
212+
}
213+
if (builder.smoothingModel() != null) {
214+
assertEquals(builder.smoothingModel().buildWordScorerFactory().getClass(), phraseSuggesterCtx.model().getClass());
215+
}
216+
if (builder.getCandidateGenerators().isEmpty() == false) {
217+
// currently, "direct_generator" is the only one available. Only compare size of the lists
218+
assertEquals(builder.getCandidateGenerators().get("direct_generator").size(), phraseSuggesterCtx.generators().size());
219+
}
220+
}
221+
222+
private static <T> void assertOptionalEquals(T optional, T actual, T defaultValue) {
223+
if (optional != null) {
224+
assertEquals(optional, actual);
225+
} else {
226+
assertEquals(defaultValue, actual);
227+
}
190228
}
191229
}

0 commit comments

Comments
 (0)