Hi,
i really like Hibernate Search but can’t figure the design of Filters and when to use them.
We have a set of ca. 20 “Filterdefinitions” that will be reused at several occasions. Currently we use the enableFullTextFilter Feature. They work nicely but the thing that bugs me most is that the Lucene Queries are very difficult to write and they don’t throw exceptions on bad index or automatically use the same analyzers at query time.
- Is there a way to use the QueryBuilder in the Filters? (For exceptions on bad index configuration and automatic analyzer usage)
- Should i rather use the QueryBuilder with a large bool() Set and build my own Setup to “enable a Filter” in the bool() query? (Maintainability, Easier for developers)
- How will Hibernate Search 6 effect this? What is the recommended way that could work or would be easy to adapt for Hibernate Search 6?
With best wishes,
Peter
WIP Code of Filter usage:
if(kauffallSuche.verkaufsjahrVon != null || kauffallSuche.verkaufsjahrBis != null) {
final LocalDate von = Optional.ofNullable(kauffallSuche.verkaufsjahrVon)
.map(j -> LocalDate.ofYearDay(j, 1).with(TemporalAdjusters.firstDayOfYear()))
.orElse(LocalDate.MIN);
final LocalDate bis = Optional.ofNullable(kauffallSuche.verkaufsjahrBis)
.map(j -> LocalDate.ofYearDay(j, 1).with(TemporalAdjusters.lastDayOfYear()))
.orElse(LocalDate.MAX);
fullTextQuery.enableFullTextFilter(FilterVerkaufsdatum.NAME)
.setParameter(FilterVerkaufsdatum.Param.verkaufsdatumVon.name(), von)
.setParameter(FilterVerkaufsdatum.Param.verkaufsdatumBis.name(), bis);
}
WIP Code of Filter
public class FilterVerkaufsdatum {
public static final String NAME = "verkaufsdatum";
public enum Param {
verkaufsdatumVon, verkaufsdatumBis
}
@Nullable
private LocalDate verkaufsdatumVon;
@Nullable
private LocalDate verkaufsdatumBis;
/**
* injected parameter
*/
public void setVerkaufsdatumVon(LocalDate von) {
this.verkaufsdatumVon = von;
}
/**
* injected parameter
*/
public void setVerkaufsdatumBis(LocalDate bis) {
this.verkaufsdatumBis = bis;
}
@Factory
public Query getFilter() {
final String path = String.join(".",
Kauffall_.URKUNDE,
Urkunde_.URKUNDEN_DATUM);
return TermRangeQuery.newStringRange(
path,
// in case of search bugs, check if LocalDateBridge is really used on the field
LocalDateBridge.INSTANCE.objectToString(Optional.ofNullable(verkaufsdatumVon).orElse(LocalDate.MIN)),
LocalDateBridge.INSTANCE.objectToString(Optional.ofNullable(verkaufsdatumBis).orElse(LocalDate.MAX)),
true, true
);
}
}