simplify handling top-level suggest results

This commit is contained in:
Areek Zillur 2016-03-16 19:40:25 -04:00
parent 22e12ab7c3
commit da24bfe542
6 changed files with 21 additions and 34 deletions

View File

@ -97,7 +97,7 @@ public class RestSuggestAction extends BaseRestHandler {
buildBroadcastShardsHeader(builder, request, response); buildBroadcastShardsHeader(builder, request, response);
Suggest suggest = response.getSuggest(); Suggest suggest = response.getSuggest();
if (suggest != null) { if (suggest != null) {
suggest.toXContent(builder, request); suggest.toInnerXContent(builder, request);
} }
builder.endObject(); builder.endObject();
return new BytesRestResponse(restStatus, builder); return new BytesRestResponse(restStatus, builder);

View File

@ -386,7 +386,7 @@ public class SearchPhaseController extends AbstractComponent {
Suggest.group(groupedSuggestions, shardResult); Suggest.group(groupedSuggestions, shardResult);
} }
suggest = hasSuggestions ? new Suggest(Suggest.Fields.SUGGEST, Suggest.reduce(groupedSuggestions)) : null; suggest = hasSuggestions ? new Suggest(Suggest.reduce(groupedSuggestions)) : null;
} }
// merge addAggregation // merge addAggregation

View File

@ -134,7 +134,7 @@ public class InternalSearchResponse implements Streamable, ToXContent {
aggregations = InternalAggregations.readAggregations(in); aggregations = InternalAggregations.readAggregations(in);
} }
if (in.readBoolean()) { if (in.readBoolean()) {
suggest = Suggest.readSuggest(Suggest.Fields.SUGGEST, in); suggest = Suggest.readSuggest(in);
} }
timedOut = in.readBoolean(); timedOut = in.readBoolean();

View File

@ -207,7 +207,7 @@ public class QuerySearchResult extends QuerySearchResultProvider {
this.pipelineAggregators = pipelineAggregators; this.pipelineAggregators = pipelineAggregators;
} }
if (in.readBoolean()) { if (in.readBoolean()) {
suggest = Suggest.readSuggest(Suggest.Fields.SUGGEST, in); suggest = Suggest.readSuggest(in);
} }
searchTimedOut = in.readBoolean(); searchTimedOut = in.readBoolean();
terminatedEarly = in.readOptionalBoolean(); terminatedEarly = in.readOptionalBoolean();

View File

@ -46,9 +46,7 @@ import java.util.Map;
*/ */
public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? extends Option>>>, Streamable, ToXContent { public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? extends Option>>>, Streamable, ToXContent {
public static class Fields { private static final XContentBuilderString NAME = new XContentBuilderString("suggest");
public static final XContentBuilderString SUGGEST = new XContentBuilderString("suggest");
}
private static final Comparator<Option> COMPARATOR = new Comparator<Suggest.Suggestion.Entry.Option>() { private static final Comparator<Option> COMPARATOR = new Comparator<Suggest.Suggestion.Entry.Option>() {
@Override @Override
@ -61,26 +59,14 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex
} }
}; };
private final XContentBuilderString name;
private List<Suggestion<? extends Entry<? extends Option>>> suggestions; private List<Suggestion<? extends Entry<? extends Option>>> suggestions;
private Map<String, Suggestion<? extends Entry<? extends Option>>> suggestMap; private Map<String, Suggestion<? extends Entry<? extends Option>>> suggestMap;
public Suggest() { public Suggest() {
this.name = null;
}
public Suggest(XContentBuilderString name) {
this.name = name;
} }
public Suggest(List<Suggestion<? extends Entry<? extends Option>>> suggestions) { public Suggest(List<Suggestion<? extends Entry<? extends Option>>> suggestions) {
this(null, suggestions);
}
public Suggest(XContentBuilderString name, List<Suggestion<? extends Entry<? extends Option>>> suggestions) {
this.name = name;
this.suggestions = suggestions; this.suggestions = suggestions;
} }
@ -148,23 +134,24 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if(name == null) { builder.startObject(NAME);
for (Suggestion<?> suggestion : suggestions) { toInnerXContent(builder, params);
suggestion.toXContent(builder, params); builder.endObject();
}
} else {
builder.startObject(name);
for (Suggestion<?> suggestion : suggestions) {
suggestion.toXContent(builder, params);
}
builder.endObject();
}
return builder; return builder;
} }
public static Suggest readSuggest(XContentBuilderString name, StreamInput in) throws IOException { /**
Suggest result = new Suggest(name); * use to write suggestion entries without <code>NAME</code> object
*/
public XContentBuilder toInnerXContent(XContentBuilder builder, Params params) throws IOException {
for (Suggestion<?> suggestion : suggestions) {
suggestion.toXContent(builder, params);
}
return builder;
}
public static Suggest readSuggest(StreamInput in) throws IOException {
Suggest result = new Suggest();
result.readFrom(in); result.readFrom(in);
return result; return result;
} }

View File

@ -83,7 +83,7 @@ public class SuggestPhase extends AbstractComponent implements SearchPhase {
} }
} }
return new Suggest(Suggest.Fields.SUGGEST, suggestions); return new Suggest(suggestions);
} catch (IOException e) { } catch (IOException e) {
throw new ElasticsearchException("I/O exception during suggest phase", e); throw new ElasticsearchException("I/O exception during suggest phase", e);
} }