caio.co/de/cerberus

Start allowing empty SearchQuery as an input

This patch doesn't do much by itself, but when using a searcher with
policy now one will be able to inspect the parsed query, see that
it was empty and do whatever it wants (In my current case: rewrite
the query as a MatchAllDocsQuery).
Id
af94a69b1c518521fc171f0c2362eeceedc31c07
Author
Caio
Commit time
2019-05-03T13:51:26+02:00

Modified src/main/java/co/caio/cerberus/model/SearchQuery.java

@@ -107,19 +107,6
throw new IllegalStateException("score must be in ]0,1]");
}
});
- if (fulltext().isPresent()
- || !dietThreshold().isEmpty()
- || numIngredients().isPresent()
- || prepTime().isPresent()
- || cookTime().isPresent()
- || totalTime().isPresent()
- || calories().isPresent()
- || fatContent().isPresent()
- || proteinContent().isPresent()
- || carbohydrateContent().isPresent()) {
- return;
- }
- throw new IllegalStateException("At least one field must be set");
}

class Builder extends ImmutableSearchQuery.Builder {

Modified src/test/java/co/caio/cerberus/model/SearchQueryTest.java

@@ -7,8 +7,8

class SearchQueryTest {
@Test
- void cantBuildEmptyQuery() {
- assertThrows(IllegalStateException.class, () -> new SearchQuery.Builder().build());
+ void canBuildEmptyQuery() {
+ assertDoesNotThrow(() -> new SearchQuery.Builder().build());
}

@Test

Modified src/test/java/co/caio/cerberus/search/SearcherTest.java

@@ -434,4 +434,9
// it should be zero
assertEquals(0, searcherWithPolicy.findSimilar(text, 10).totalHits());
}
+
+ @Test
+ void emptySearchQueryYieldsEmptyResults() {
+ assertEquals(0, searcher.search(new SearchQuery.Builder().build()).totalHits());
+ }
}