diff options
author | Tanguy Leroux <tlrx.dev@gmail.com> | 2017-02-10 10:53:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-10 10:53:38 +0100 |
commit | e2e593745546083a31ccfb9744be6174dd312858 (patch) | |
tree | b46fe6e2dfc10e03797cdb3aea88f08c22350c6a /core/src/main/java/org/elasticsearch/search | |
parent | 63ea6f7168326d1687705c87671857d29cb9a783 (diff) |
Use `typed_keys` parameter to prefix suggester names by type in search responses (#23080)
This pull request reuses the typed_keys parameter added in #22965, but this time it applies it to suggesters. When set to true, the suggester names in the search response will be prefixed with a prefix that reflects their type.
Diffstat (limited to 'core/src/main/java/org/elasticsearch/search')
5 files changed, 56 insertions, 9 deletions
diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java index 6dfd6fcc79..6af896426a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregation.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.rest.action.search.RestSearchAction; import org.elasticsearch.script.ScriptService; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationPath; @@ -164,9 +165,12 @@ public abstract class InternalAggregation implements Aggregation, ToXContent, Na @Override public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - // Concatenates the type and the name of the aggregation (ex: top_hits#foo) - String name = params.paramAsBoolean("typed_keys", false) ? String.join(TYPED_KEYS_DELIMITER, getType(), getName()) : getName(); - builder.startObject(name); + if (params.paramAsBoolean(RestSearchAction.TYPED_KEYS_PARAM, false)) { + // Concatenates the type and the name of the aggregation (ex: top_hits#foo) + builder.startObject(String.join(TYPED_KEYS_DELIMITER, getType(), getName())); + } else { + builder.startObject(getName()); + } if (this.metaData != null) { builder.field(CommonFields.META); builder.map(this.metaData); 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 ec7294f900..f1875564f8 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/Suggest.java @@ -30,6 +30,8 @@ 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.rest.action.search.RestSearchAction; +import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option; import org.elasticsearch.search.suggest.completion.CompletionSuggestion; @@ -149,7 +151,7 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex public void writeTo(StreamOutput out) throws IOException { out.writeVInt(suggestions.size()); for (Suggestion<?> command : suggestions) { - out.writeVInt(command.getType()); + out.writeVInt(command.getWriteableType()); command.writeTo(out); } } @@ -206,6 +208,8 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex */ public static class Suggestion<T extends Suggestion.Entry> implements Iterable<T>, Streamable, ToXContent { + private static final String NAME = "suggestion"; + public static final int TYPE = 0; protected String name; protected int size; @@ -223,10 +227,23 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex entries.add(entry); } - public int getType() { + /** + * Returns a integer representing the type of the suggestion. This is used for + * internal serialization over the network. + */ + public int getWriteableType() { // TODO remove this in favor of NamedWriteable return TYPE; } + /** + * Returns a string representing the type of the suggestion. This type is added to + * the suggestion name in the XContent response, so that it can later be used by + * REST clients to determine the internal type of the suggestion. + */ + protected String getType() { + return NAME; + } + @Override public Iterator<T> iterator() { return entries.iterator(); @@ -338,7 +355,12 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startArray(name); + if (params.paramAsBoolean(RestSearchAction.TYPED_KEYS_PARAM, false)) { + // Concatenates the type and the name of the suggestion (ex: completion#foo) + builder.startArray(String.join(InternalAggregation.TYPED_KEYS_DELIMITER, getType(), getName())); + } else { + builder.startArray(getName()); + } for (Entry<?> entry : entries) { entry.toXContent(builder, params); } diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java index 1d8da83271..e33e421f77 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java @@ -57,6 +57,8 @@ import static org.elasticsearch.search.suggest.Suggest.COMPARATOR; */ public final class CompletionSuggestion extends Suggest.Suggestion<CompletionSuggestion.Entry> { + private static final String NAME = "completion"; + public static final int TYPE = 4; public CompletionSuggestion() { @@ -165,11 +167,16 @@ public final class CompletionSuggestion extends Suggest.Suggestion<CompletionSug } @Override - public int getType() { + public int getWriteableType() { return TYPE; } @Override + protected String getType() { + return NAME; + } + + @Override protected Entry newEntry() { return new Entry(); } diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestion.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestion.java index 23949f1a09..e673ccb128 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestion.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestion.java @@ -31,6 +31,8 @@ import java.io.IOException; * Suggestion entry returned from the {@link PhraseSuggester}. */ public class PhraseSuggestion extends Suggest.Suggestion<PhraseSuggestion.Entry> { + + private static final String NAME = "phrase"; public static final int TYPE = 3; public PhraseSuggestion() { @@ -41,11 +43,16 @@ public class PhraseSuggestion extends Suggest.Suggestion<PhraseSuggestion.Entry> } @Override - public int getType() { + public int getWriteableType() { return TYPE; } @Override + protected String getType() { + return NAME; + } + + @Override protected Entry newEntry() { return new Entry(); } 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 b0586b7287..68aed8b80a 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 @@ -40,6 +40,8 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constru */ public class TermSuggestion extends Suggestion<TermSuggestion.Entry> { + private static final String NAME = "term"; + public static final Comparator<Suggestion.Entry.Option> SCORE = new Score(); public static final Comparator<Suggestion.Entry.Option> FREQUENCY = new Frequency(); public static final int TYPE = 1; @@ -96,11 +98,16 @@ public class TermSuggestion extends Suggestion<TermSuggestion.Entry> { } @Override - public int getType() { + public int getWriteableType() { return TYPE; } @Override + protected String getType() { + return NAME; + } + + @Override protected Comparator<Option> sortComparator() { switch (sort) { case SCORE: |