如何在Hibernate Lucene Search中添加多个限制?

时间:2017-04-10 06:57:42

标签: java hibernate lucene hibernate-search

我无法在使用hibernate lucene search的情况下写多个条件这里我可以根据以下任何keyword进行搜索,但我在这里有多个关键字,那么如何使用此关键字来应用限制请帮助我在hibernate lucene search中应用多个限制。提前感谢以下是我的查询。

@Override
public List getCompaniesBasedOnSearch(String keyword,String keyword2,String keyword3) throws InterruptedException {FullTextSession fullTextSession = Search.getFullTextSession(session);
    fullTextSession.createIndexer(Company.class).startAndWait();

    QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Company.class).get();
    org.apache.lucene.search.Query query = qb.keyword().onFields("company_name","locations.city","locations.state").matching(keyword1).createQuery();

    org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Company_Info.class);

1 个答案:

答案 0 :(得分:1)

烨....!最后,我得到了解决方案,我们可以通过使用组合查询在Hibernate Lucene Search中添加多个限制。请仔细阅读以下内容

QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Student.class).get();
    org.apache.lucene.search.Query query = qb.bool().must( qb.keyword().onFields("name","country","skills.skill").matching(keyword).createQuery())
            .must( qb.keyword().onField("skills.skill").matching(skill).createQuery())
            .must( qb.keyword().onField("locations.city").matching(location).createQuery())
            .must( qb.keyword().onField("jobStatus").matching("active").createQuery())
            .must( qb.keyword().onField("postedOrSaved").matching("posted").createQuery())
            .createQuery();
    org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Jobs.class);
    List<Jobs> result = hibQuery.list();

这里我在(("a" OR "b" OR "c") AND ("d") AND ("e") AND ("f")实体字段中执行student操作。