Commit 44c8a0bd authored by Jean-Baptiste Nizet's avatar Jean-Baptiste Nizet
Browse files

fix: avoid NPE when suggesting with no data in the database

refs #14
parent 8c9ca1e7
......@@ -25,6 +25,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.springframework.data.domain.Pageable;
......@@ -191,13 +192,18 @@ public class GeneticResourceDaoImpl implements GeneticResourceDaoCustom {
.setFetchSource(false) // avoid getting the source documents, which are useless
.get();
List<String> suggestions = response.getSuggest()
.getSuggestion(COMPLETION)
.getEntries()
.stream()
.flatMap(entry -> entry.getOptions().stream())
.map(option -> option.getText().string())
.collect(Collectors.toList());
Suggest suggest = response.getSuggest();
if (suggest == null) {
// no data in the database
return Collections.emptyList();
}
List<String> suggestions = suggest.getSuggestion(COMPLETION)
.getEntries()
.stream()
.flatMap(entry -> entry.getOptions().stream())
.map(option -> option.getText().string())
.collect(Collectors.toList());
return removeSuggestionsDifferingByCase(suggestions);
}
......
......@@ -312,6 +312,11 @@ class GeneticResourceDaoTest {
assertThat(ignoringCaseSet).hasSize(2);
}
@Test
void shouldNotFailSuggestingIfNoData() {
assertThat(geneticResourceDao.suggest("vitis")).isEmpty();
}
private void shouldSearch(BiConsumer<GeneticResource.Builder, String> config) {
GeneticResource.Builder geneticResourceBuilder = GeneticResource.builder();
config.accept(geneticResourceBuilder, "foo bar baz");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment