Hi,
I have a very similar (confusing) issue.
Using hibernate-4.2.2 and hibernate-search-4.2.0.
I want my users to enter a searchstring and be able to use boolean operators like +/- or AND/OR/NOT.
This demands that I can build up a query dynamically with must, mustNot and should words and if possible, phrases.
As the code is below it seems to work, eaven though I haven't used any phrases.
If any of the shouldQuery, mustQuery or notQuery in the code is empty or null this will not work.
Am I on the right track?
How do I combine my queries dynamically with this line if one of the mustQuery, notQuery or shouldQuery is empty or null.
booleanJunction = qb.bool().must(mustQuery).must(notQuery).not().should(shouldQuery);
Also, I do now parse the - and + manually myself in a helper class searchWordUtil, should this be neccessary?
If I should use Lucine to 'feed' Hibernate search with queries, how is this possible, do you have any example?
Code:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait(); // indexing the h2 database
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(EducationMoment.class).get();
BooleanJunction booleanJunction = null;
// searchWordUtil.createWordQ retun 3 lists containing the +words, -words and the rest of the words.
ListMultimap listMultimap = searchWordUtil.createWordQ("aaaaa bbbb cccc -dddd +eeee");
org.apache.lucene.search.Query shouldQuery = qb
.keyword().fuzzy().withThreshold(.3f).withPrefixLength(1)
.onFields("education.desciption","education.name")
.matching(listMultimap.get(SearchWordUtil.WORD)).createQuery();
//BooleanJunction booleanJunction1 = qb.bool().should(shouldQuery);
org.apache.lucene.search.Query mustQuery = qb
.keyword().fuzzy().withThreshold(.3f).withPrefixLength(1)
.onField("education.desciption","education.name")
.matching(listMultimap.get(SearchWordUtil.PLUS)).createQuery();
//BooleanJunction booleanJunction2 = qb.bool().must(mustQuery);
org.apache.lucene.search.Query notQuery = qb
.keyword().fuzzy().withThreshold(.3f).withPrefixLength(1)
.onFields("education.desciption","education.name")
.matching(listMultimap.get(SearchWordUtil.MINUS)).createQuery();
//BooleanJunction booleanJunction3 = qb.bool().must(notQuery).not();
booleanJunction = qb.bool().must(mustQuery).must(notQuery).not().should(shouldQuery);
javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(booleanJunction.createQuery(), EducationMoment.class);
List result = persistenceQuery.getResultList();
Assert.assertEquals(1, result.size());
/Mike