Hibernate Lucene为ENUM添加OR运算符

时间:2014-10-09 18:33:13

标签: hibernate lucene hibernate-search

Hibernate Lucene为ENUM添加OR运算符

我实现了这段代码并且运行良好

QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(MyClass.class).get();

Query query1 = queryBuilder.keyword().onFields("title", "description").matching(keywords).createQuery();

Query query2 = queryBuilder.keyword().onFields(STATE.ENABLED).createQuery();

List<Query> listOfQuery = new ArrayList<Query>();

listOfQuery.add(query1);
listOfQuery.add(query2);

Query finalLuceneQuery=new BooleanQuery();
for (Query query : listOfQuery) {
    ((BooleanQuery) finalLuceneQuery).add(query, BooleanClause.Occur.MUST);
}

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(finalLuceneQuery, MyClass.class);

但现在我想添加到&#34;第二个查询&#34;可以询问STATE.DISABLE(如OR子句)

我试过这样做

Query query2 = queryBuilder.keyword().onFields(STATE.ENABLED + " " + STATE.DISABLE).createQuery();

但没有工作

1 个答案:

答案 0 :(得分:1)

您可以创建一个布尔查询。

Query query2 = queryBuilder.keyword().onFields(STATE.ENABLED).createQuery();
Query query3 = queryBuilder.keyword().onFields(STATE.DISABLED).createQuery();

BooleanQuery bq = new BooleanQuery();
bq.add(new BooleanClause(query2 , BooleanClause.Occur.SHOULD));
bq.add(new BooleanClause(query3 , BooleanClause.Occur.SHOULD));

然后你可以在列表中添加bq

listOfQuery.add(query1);
listOfQuery.add(bq);