Hi,
I have a little Hibernate Search problem. I have a class Person with the follwoing content:
Code:
@Entity
@Indexed
@Boost(2f)
public class Person {
..
@Column(nullable = false, length = 200)
@Field(index = Index.YES, store = Store.NO, boost = @Boost(2f))
private String name;
@Column(nullable = false, length = 20)
@Enumerated(EnumType.STRING)
@Field(index = Index.YES, store = Store.NO, boost = @Boost(1.5f))
@FieldBridge(impl = EnumBridge.class)
private Country country;
@Column(nullable = false)
@Field(index = Index.YES, store = Store.NO, boost = @Boost(1.5f))
@NumericField(precisionStep = 1)
private Integer year;
Country is a simple enum:
Code:
public enum Country {
BELGIUM(),
IRELAND(),
..;
Country()
{
}
..
Now I want to write the following search method:
Code:
public List<Person> findPersons(String name, List<Country> countries, Integer yearFrom, Integer yearTo) {
QueryBuilder builder = getQueryBuilder();
// create lucene query
BooleanJunction<?> bool = builder.bool();
// name
if (StringUtils.isNotBlank(name)) {
bool.must(builder.keyword().onField("name").boostedTo(3).matching(name).createQuery());
}
// country
if (null != countries) {
// FIXME this works not in all cases
StringBuilder value = new StringBuilder();
for (Country country : countries) {
value.append(country).append(" ");
}
bool.must(builder.phrase().onField("country").sentence(value.toString()).createQuery());
}
// year
if (yearFrom != null || yearTo != null)
{
bool.must(builder.range().onField("year").from(yearFrom).to(yearTo).createQuery());
}
Query luceneQuery = bool.createQuery();
// ..
All search parameters must be fulfilled if the parameter is not null. Now I have problems with the field country.
If the countries list contains only one Country it works, otherwise not.
My solutions with
Code:
builder.phrase().onField("country").sentence(value.toString()).createQuery()
looks not good and doesn't work for multiple countries.
Is there no way with
Code:
builder.keyword().onField("country").matching(..)
and an OR relation? What's the best way?
Every help is very appreciated!
Thanks a lot,
Detlev