From ea7deace5dd3c293dadb508979511c28d785588d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Mon, 20 Feb 2017 15:45:10 +0100 Subject: [PATCH] Adding fromXContent to Suggest and Suggestion class (#23226) A follow up to #23202, this adds parsing from xContent and tests to the four Suggestion implementations and the top level suggest element to be used later when parsing the entire SearchResponse. --- .../common/xcontent/XContentHelper.java | 10 +- .../elasticsearch/search/suggest/Suggest.java | 67 ++++- .../completion/CompletionSuggestion.java | 2 +- .../suggest/phrase/PhraseSuggestion.java | 2 +- .../search/suggest/term/TermSuggestion.java | 2 +- .../search/suggest/SuggestTests.java | 72 ++++++ .../search/suggest/SuggestionTests.java | 240 ++++++++++++++++++ 7 files changed, 388 insertions(+), 7 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 968ad8ac6bc..0c6cb1c5cda 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -448,12 +448,20 @@ public class XContentHelper { * {@link XContentType}. Wraps the output into a new anonymous object. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, boolean humanReadable) throws IOException { + return toXContent(toXContent, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); + } + + /** + * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided + * {@link XContentType}. Wraps the output into a new anonymous object. + */ + public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.humanReadable(humanReadable); if (toXContent.isFragment()) { builder.startObject(); } - toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); + toXContent.toXContent(builder, params); if (toXContent.isFragment()) { builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java b/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java index 36a780fec35..ba5ad712f41 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.suggest; import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; @@ -47,17 +48,19 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; /** * Top level suggest result, containing the result for each suggestion. */ public class Suggest implements Iterable>>, Streamable, ToXContent { - private static final String NAME = "suggest"; + static final String NAME = "suggest"; public static final Comparator