diff options
author | Christoph Büscher <christoph@elastic.co> | 2017-02-08 19:03:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-08 19:03:12 +0100 |
commit | e09f3ecbb3ab0af58d5ff5edb024e166df12a5ae (patch) | |
tree | 2ee585c36841ed0ba61a8590b7962c657765c76a /core/src/main/java/org/elasticsearch/search/suggest | |
parent | 735e5b1983e1ad89001daaeaf9284abe64eb1b9e (diff) |
Add xcontent parsing to suggestion options (#23018)
This adds parsing from xContent to Suggestion.Entry.Option and
Termsuggestion.Entry.Option.
Diffstat (limited to 'core/src/main/java/org/elasticsearch/search/suggest')
-rw-r--r-- | core/src/main/java/org/elasticsearch/search/suggest/Suggest.java | 51 | ||||
-rw-r--r-- | core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestion.java | 32 |
2 files changed, 64 insertions, 19 deletions
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 fc372ee6b2..ec7294f900 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java @@ -19,13 +19,17 @@ package org.elasticsearch.search.suggest; import org.apache.lucene.util.CollectionUtil; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option; import org.elasticsearch.search.suggest.completion.CompletionSuggestion; @@ -42,6 +46,9 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; + /** * Top level suggest result, containing the result for each suggestion. */ @@ -525,16 +532,12 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex /** * Contains the suggested text with its document frequency and score. */ - public static class Option implements Streamable, ToXContent { - - static class Fields { + public static class Option implements Streamable, ToXContentObject { - static final String TEXT = "text"; - static final String HIGHLIGHTED = "highlighted"; - static final String SCORE = "score"; - static final String COLLATE_MATCH = "collate_match"; - - } + public static final ParseField TEXT = new ParseField("text"); + public static final ParseField HIGHLIGHTED = new ParseField("highlighted"); + public static final ParseField SCORE = new ParseField("score"); + public static final ParseField COLLATE_MATCH = new ParseField("collate_match"); private Text text; private Text highlighted; @@ -618,17 +621,38 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex } protected XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { - builder.field(Fields.TEXT, text); + builder.field(TEXT.getPreferredName(), text); if (highlighted != null) { - builder.field(Fields.HIGHLIGHTED, highlighted); + builder.field(HIGHLIGHTED.getPreferredName(), highlighted); } - builder.field(Fields.SCORE, score); + builder.field(SCORE.getPreferredName(), score); if (collateMatch != null) { - builder.field(Fields.COLLATE_MATCH, collateMatch.booleanValue()); + builder.field(COLLATE_MATCH.getPreferredName(), collateMatch.booleanValue()); } return builder; } + private static final ConstructingObjectParser<Option, Void> PARSER = new ConstructingObjectParser<>("SuggestOptionParser", + true, args -> { + Text text = new Text((String) args[0]); + float score = (Float) args[1]; + String highlighted = (String) args[2]; + Text highlightedText = highlighted == null ? null : new Text(highlighted); + Boolean collateMatch = (Boolean) args[3]; + return new Option(text, highlightedText, score, collateMatch); + }); + + static { + PARSER.declareString(constructorArg(), TEXT); + PARSER.declareFloat(constructorArg(), SCORE); + PARSER.declareString(optionalConstructorArg(), HIGHLIGHTED); + PARSER.declareBoolean(optionalConstructorArg(), COLLATE_MATCH); + } + + public static Option fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + protected void mergeInto(Option otherOption) { score = Math.max(score, otherOption.score); } @@ -640,7 +664,6 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex Option that = (Option) o; return text.equals(that.text); - } @Override diff --git a/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestion.java b/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestion.java index 7673267ecf..b0586b7287 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestion.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/term/TermSuggestion.java @@ -19,10 +19,13 @@ package org.elasticsearch.search.suggest.term; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.suggest.SortBy; import org.elasticsearch.search.suggest.Suggest.Suggestion; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option; @@ -30,6 +33,8 @@ import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option; import java.io.IOException; import java.util.Comparator; +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + /** * The suggestion responses corresponding with the suggestions in the request. */ @@ -147,13 +152,11 @@ public class TermSuggestion extends Suggestion<TermSuggestion.Entry> { */ public static class Option extends org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option { - static class Fields { - static final String FREQ = "freq"; - } + public static final ParseField FREQ = new ParseField("freq"); private int freq; - protected Option(Text text, int freq, float score) { + public Option(Text text, int freq, float score) { super(text, score); this.freq = freq; } @@ -194,9 +197,28 @@ public class TermSuggestion extends Suggestion<TermSuggestion.Entry> { @Override protected XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { builder = super.innerToXContent(builder, params); - builder.field(Fields.FREQ, freq); + builder.field(FREQ.getPreferredName(), freq); return builder; } + + private static final ConstructingObjectParser<Option, Void> PARSER = new ConstructingObjectParser<>( + "TermSuggestionOptionParser", true, + args -> { + Text text = new Text((String) args[0]); + int freq = (Integer) args[1]; + float score = (Float) args[2]; + return new Option(text, freq, score); + }); + + static { + PARSER.declareString(constructorArg(), Suggestion.Entry.Option.TEXT); + PARSER.declareInt(constructorArg(), FREQ); + PARSER.declareFloat(constructorArg(), Suggestion.Entry.Option.SCORE); + } + + public static final Option fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } } } |