Join hibernate search query with criteria query restriction

若如初见. 提交于 2019-12-05 19:23:52
Hardy

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

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);

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

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