ensure that suggestion only added on reduce if they are present in the shard response

This commit is contained in:
Simon Willnauer 2013-03-01 21:08:50 +01:00
parent d99b532f0f
commit fced68c22d
2 changed files with 11 additions and 3 deletions

View File

@ -380,12 +380,14 @@ public class SearchPhaseController extends AbstractComponent {
// merge suggest results
Suggest suggest = null;
if (!queryResults.isEmpty()) {
Map<String, List<Suggest.Suggestion>> groupedSuggestions = new HashMap<String, List<Suggest.Suggestion>>();
final Map<String, List<Suggest.Suggestion>> groupedSuggestions = new HashMap<String, List<Suggest.Suggestion>>();
boolean hasSuggestions = false;
for (QuerySearchResultProvider resultProvider : queryResults.values()) {
Suggest shardResult = resultProvider.queryResult().suggest();
if (shardResult == null) {
continue;
}
hasSuggestions = true;
for (Suggestion<? extends Entry<? extends Option>> suggestion : shardResult) {
List<Suggestion> list = groupedSuggestions.get(suggestion.getName());
if (list == null) {
@ -404,7 +406,7 @@ public class SearchPhaseController extends AbstractComponent {
reduced.add(reduce);
}
suggest = new Suggest(reduced);
suggest = hasSuggestions ? new Suggest(reduced) : null;
}
InternalSearchHits searchHits = new InternalSearchHits(hits.toArray(new InternalSearchHit[hits.size()]), totalHits, maxScore);

View File

@ -28,6 +28,8 @@ import static org.elasticsearch.search.suggest.SuggestBuilder.termSuggestion;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import java.io.BufferedReader;
import java.io.IOException;
@ -115,8 +117,12 @@ public class SuggestSearchTests extends AbstractNodesTests {
)
.execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse search = client.prepareSearch()
.setQuery(matchQuery("text", "spellcecker")).execute().actionGet();
assertThat("didn't ask for suggestions but got some", search.getSuggest(), nullValue());
search = client.prepareSearch()
.setQuery(matchQuery("text", "spellcecker"))
.addSuggestion(
termSuggestion("test").suggestMode("always") // Always, otherwise the results can vary between requests.