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; ...@@ -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.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; 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.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders; import org.elasticsearch.search.suggest.SuggestBuilders;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
...@@ -191,13 +192,18 @@ public class GeneticResourceDaoImpl implements GeneticResourceDaoCustom { ...@@ -191,13 +192,18 @@ public class GeneticResourceDaoImpl implements GeneticResourceDaoCustom {
.setFetchSource(false) // avoid getting the source documents, which are useless .setFetchSource(false) // avoid getting the source documents, which are useless
.get(); .get();
List<String> suggestions = response.getSuggest() Suggest suggest = response.getSuggest();
.getSuggestion(COMPLETION) if (suggest == null) {
.getEntries() // no data in the database
.stream() return Collections.emptyList();
.flatMap(entry -> entry.getOptions().stream()) }
.map(option -> option.getText().string())
.collect(Collectors.toList()); List<String> suggestions = suggest.getSuggestion(COMPLETION)
.getEntries()
.stream()
.flatMap(entry -> entry.getOptions().stream())
.map(option -> option.getText().string())
.collect(Collectors.toList());
return removeSuggestionsDifferingByCase(suggestions); return removeSuggestionsDifferingByCase(suggestions);
} }
......
...@@ -312,6 +312,11 @@ class GeneticResourceDaoTest { ...@@ -312,6 +312,11 @@ class GeneticResourceDaoTest {
assertThat(ignoringCaseSet).hasSize(2); assertThat(ignoringCaseSet).hasSize(2);
} }
@Test
void shouldNotFailSuggestingIfNoData() {
assertThat(geneticResourceDao.suggest("vitis")).isEmpty();
}
private void shouldSearch(BiConsumer<GeneticResource.Builder, String> config) { private void shouldSearch(BiConsumer<GeneticResource.Builder, String> config) {
GeneticResource.Builder geneticResourceBuilder = GeneticResource.builder(); GeneticResource.Builder geneticResourceBuilder = GeneticResource.builder();
config.accept(geneticResourceBuilder, "foo bar baz"); config.accept(geneticResourceBuilder, "foo bar baz");
......
Supports Markdown
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