From 0e8d7582ec3ceb13213e39678f8640ff13f24403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 7 Jul 2017 16:18:25 +0200 Subject: [PATCH] [Tests] Add tests for CompletionSuggestionBuilder#build() (#25575) This adds a unit test that checks the CompletionSuggestionContext that is the output of CompletionSuggestionBuilder#build. --- .../CompletionSuggestionBuilder.java | 51 ++++++++++--------- .../AbstractSuggestionBuilderTestCase.java | 6 +-- .../CompletionSuggesterBuilderTests.java | 28 +++++++--- .../phrase/PhraseSuggestionBuilderTests.java | 2 +- .../term/TermSuggestionBuilderTests.java | 2 +- 5 files changed, 55 insertions(+), 34 deletions(-) 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 b951a306198..462aa8e271b 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 @@ -24,10 +24,12 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.Fuzziness; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.CompletionFieldMapper; @@ -52,6 +54,7 @@ import java.util.Objects; * indexing. */ public class CompletionSuggestionBuilder extends SuggestionBuilder { + private static final XContentType CONTEXT_BYTES_XCONTENT_TYPE = XContentType.JSON; static final String SUGGESTION_NAME = "completion"; static final ParseField CONTEXTS_FIELD = new ParseField("contexts", "context"); @@ -86,7 +89,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder { // Copy the current structure. We will parse, once the mapping is provided - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); + XContentBuilder builder = XContentFactory.contentBuilder(CONTEXT_BYTES_XCONTENT_TYPE); builder.copyCurrentStructure(p); v.contextBytes = builder.bytes(); p.skipChildren(); @@ -186,7 +189,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder> queryContexts) { Objects.requireNonNull(queryContexts, "contexts must not be null"); try { - XContentBuilder contentBuilder = XContentFactory.jsonBuilder(); + XContentBuilder contentBuilder = XContentFactory.contentBuilder(CONTEXT_BYTES_XCONTENT_TYPE); contentBuilder.startObject(); for (Map.Entry> contextEntry : queryContexts.entrySet()) { contentBuilder.startArray(contextEntry.getKey()); @@ -255,33 +258,16 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder> queryContexts = new HashMap<>(contextMappings.size()); - assert contextParser.currentToken() == XContentParser.Token.START_OBJECT; - XContentParser.Token currentToken; - String currentFieldName; - while ((currentToken = contextParser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (currentToken == XContentParser.Token.FIELD_NAME) { - currentFieldName = contextParser.currentName(); - final ContextMapping mapping = contextMappings.get(currentFieldName); - queryContexts.put(currentFieldName, mapping.parseQueryContext(contextParser)); - } - } - suggestionContext.setQueryContexts(queryContexts); - } - } + Map> queryContexts = parseContextBytes(contextBytes, + context.getXContentRegistry(), type.getContextMappings()); + suggestionContext.setQueryContexts(queryContexts); } else if (contextBytes != null) { throw new IllegalArgumentException("suggester [" + type.name() + "] doesn't expect any context"); } @@ -290,6 +276,25 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder> parseContextBytes(BytesReference contextBytes, + NamedXContentRegistry xContentRegistry, ContextMappings contextMappings) throws IOException { + try (XContentParser contextParser = XContentHelper.createParser(xContentRegistry, contextBytes, CONTEXT_BYTES_XCONTENT_TYPE)) { + contextParser.nextToken(); + Map> queryContexts = new HashMap<>(contextMappings.size()); + assert contextParser.currentToken() == XContentParser.Token.START_OBJECT; + XContentParser.Token currentToken; + String currentFieldName; + while ((currentToken = contextParser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (currentToken == XContentParser.Token.FIELD_NAME) { + currentFieldName = contextParser.currentName(); + final ContextMapping mapping = contextMappings.get(currentFieldName); + queryContexts.put(currentFieldName, mapping.parseQueryContext(contextParser)); + } + } + return queryContexts; + } + } + @Override public String getWriteableName() { return SUGGESTION_NAME; diff --git a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java index dd26033ab45..553517529a2 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java @@ -201,14 +201,14 @@ public abstract class AbstractSuggestionBuilderTestCase { private static final String[] SHUFFLE_PROTECTED_FIELDS = new String[] { CompletionSuggestionBuilder.CONTEXTS_FIELD.getPreferredName() }; @@ -54,7 +56,7 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe } public static CompletionSuggestionBuilder randomCompletionSuggestionBuilder() { - // lazy initialization of context names and mappings, cannot be done in init method because other test + // lazy initialization of context names and mappings, cannot be done in some init method because other test // also create random CompletionSuggestionBuilder instances if (categoryContextName == null) { categoryContextName = randomAlphaOfLength(10); @@ -62,9 +64,9 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe if (geoQueryContextName == null) { geoQueryContextName = randomAlphaOfLength(10); } - if (contextMappings == null) { - contextMappings = Arrays.asList(new ContextMapping[] { ContextBuilder.category(categoryContextName).build(), - ContextBuilder.geo(geoQueryContextName).build() }); + if (contextMappings.isEmpty()) { + contextMappings.add(ContextBuilder.category(categoryContextName).build()); + contextMappings.add(ContextBuilder.geo(geoQueryContextName).build()); } // lazy initialization of context names and mappings, cannot be done in some init method because other test // also create random CompletionSuggestionBuilder instances @@ -165,6 +167,20 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe } @Override - protected void assertSuggester(CompletionSuggestionBuilder builder, SuggestionContext context) { + protected void assertSuggestionContext(CompletionSuggestionBuilder builder, SuggestionContext context) throws IOException { + assertThat(context, instanceOf(CompletionSuggestionContext.class)); + assertThat(context.getSuggester(), instanceOf(CompletionSuggester.class)); + CompletionSuggestionContext completionSuggestionCtx = (CompletionSuggestionContext) context; + assertThat(completionSuggestionCtx.getFieldType(), instanceOf(CompletionFieldType.class) ); + assertEquals(builder.fuzzyOptions, completionSuggestionCtx.getFuzzyOptions()); + Map> parsedContextBytes; + parsedContextBytes = CompletionSuggestionBuilder.parseContextBytes(builder.contextBytes, xContentRegistry(), + new ContextMappings(contextMappings)); + Map> queryContexts = completionSuggestionCtx.getQueryContexts(); + assertEquals(parsedContextBytes.keySet(), queryContexts.keySet()); + for (String contextName : queryContexts.keySet()) { + assertEquals(parsedContextBytes.get(contextName), queryContexts.get(contextName)); + } + assertEquals(builder.regexOptions, completionSuggestionCtx.getRegexOptions()); } } 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 4bfc7a1c8c9..d54a20f66db 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 @@ -188,7 +188,7 @@ public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestC } @Override - protected void assertSuggester(PhraseSuggestionBuilder builder, SuggestionContext context) { + protected void assertSuggestionContext(PhraseSuggestionBuilder builder, SuggestionContext context) { assertThat(context, instanceOf(PhraseSuggestionContext.class)); assertThat(context.getSuggester(), instanceOf(PhraseSuggester.class)); PhraseSuggestionContext phraseSuggesterCtx = (PhraseSuggestionContext) context; diff --git a/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java index 22144080ff2..e366a5c4508 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java @@ -227,7 +227,7 @@ public class TermSuggestionBuilderTests extends AbstractSuggestionBuilderTestCas } @Override - protected void assertSuggester(TermSuggestionBuilder builder, SuggestionContext context) { + protected void assertSuggestionContext(TermSuggestionBuilder builder, SuggestionContext context) { assertThat(context, instanceOf(TermSuggestionContext.class)); assertThat(context.getSuggester(), instanceOf(TermSuggester.class)); TermSuggestionContext termSuggesterCtx = (TermSuggestionContext) context;