Join hibernate search query with criteria query restriction

偶尔善良 提交于 2019-12-10 10:07:11

问题


I'm wondering how you would join the following two queries together.

A standard criteria query

Criteria result1 = session.createCriteria(Store.class).add(Restrictions.eq("department.name", category));

and a FullTextSearch

    QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Store.class).get();
    Query luceneQuery = queryBuilder.keyword().onFields("productTitle").matching(keyword).createQuery();

    // wrap Lucene query in a javax.persistence.Query
    org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Store.class);
    fullTextQuery.setMaxResults(15);
    fullTextQuery.setFirstResult(0);

I pass in additional parameters through the URL plus a keyword parameter, I do not want to fully rely on keyword search. Does anybody know how to make these work together?

Thanks.


回答1:


The Hibernate Search documentation actually discourages using Criteria queries combined with full text search queries (except for specifying the fetch type).

Only fetch mode can be adjusted, refrain from applying any other restriction. While it is known to work as of Hibernate Search 4, using restriction (ie a where clause) on your Criteria query should be avoided when possible. getResultSize() will throw a SearchException if used in conjunction with a Criteria with restriction.

See also http://docs.jboss.org/hibernate/search/4.4/reference/en-US/html_single/index.html#d0e5722




回答2:


For anybody who may need this in the future, this will demonstrate how to do additional query restrictions with hibernate search.

    QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Store.class).get();
    Query luceneQuery = queryBuilder.keyword().onFields("productTitle").matching(keyword).createQuery();

    org.hibernate.search.FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Store.class);

   Criteria query = session.createCriteria(Store.class)
            .createAlias("department", "department")
            .add(Restrictions.eq("department.name", category));

    fullTextQuery.setCriteriaQuery(query);
    fullTextQuery.setMaxResults(15);
    fullTextQuery.setFirstResult(0);



回答3:


The solution to this problem was to use a Filter which can be found here.

http://docs.jboss.org/hibernate/search/4.4/reference/en-US/html/search-query.html#query-filter



来源:https://stackoverflow.com/questions/19309464/join-hibernate-search-query-with-criteria-query-restriction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!