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);
Suggest suggest = response.getSuggest();
if (suggest != null) {
suggest.toXContent(builder, request);
suggest.toInnerXContent(builder, request);
}
builder.endObject();
return new BytesRestResponse(restStatus, builder);

View File

@ -386,7 +386,7 @@ public class SearchPhaseController extends AbstractComponent {
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

View File

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

View File

@ -207,7 +207,7 @@ public class QuerySearchResult extends QuerySearchResultProvider {
this.pipelineAggregators = pipelineAggregators;
}
if (in.readBoolean()) {
suggest = Suggest.readSuggest(Suggest.Fields.SUGGEST, in);
suggest = Suggest.readSuggest(in);
}
searchTimedOut = in.readBoolean();
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 static class Fields {
public static final XContentBuilderString SUGGEST = new XContentBuilderString("suggest");
}
private static final XContentBuilderString NAME = new XContentBuilderString("suggest");
private static final Comparator<Option> COMPARATOR = new Comparator<Suggest.Suggestion.Entry.Option>() {
@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 Map<String, Suggestion<? extends Entry<? extends Option>>> suggestMap;
public Suggest() {
this.name = null;
}
public Suggest(XContentBuilderString name) {
this.name = name;
}
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;
}
@ -148,23 +134,24 @@ public class Suggest implements Iterable<Suggest.Suggestion<? extends Entry<? ex
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if(name == null) {
for (Suggestion<?> suggestion : suggestions) {
suggestion.toXContent(builder, params);
}
} else {
builder.startObject(name);
for (Suggestion<?> suggestion : suggestions) {
suggestion.toXContent(builder, params);
}
builder.endObject();
}
builder.startObject(NAME);
toInnerXContent(builder, params);
builder.endObject();
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);
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) {
throw new ElasticsearchException("I/O exception during suggest phase", e);
}