2

I recently got familiar with Hibernate search API. How can I have full text search on an entity and also filter the result by one specific field? For example:

final FullTextEntityManager ftem = Search.getFullTextEntityManager(em);

final QueryBuilder qb = ftem.getSearchFactory()
        .buildQueryBuilder().forEntity(Item.class).get();

final org.apache.lucene.search.Query query = qb
        .keyword()
        .onFields(Item_.CONTENT)
        .matching(match)
        .createQuery();

/* also here I want to filter the results for all the Items in which category equals to 2 */

final FullTextQuery persistenceQuery = ftem.createFullTextQuery(query, Item.class);

final List<Item> result = persistenceQuery.getResultList();
1
  • I have exactly same problem! Unfortunately I did not find anything useful yet. Please also share the answer if you find a solution. Commented Apr 4, 2015 at 13:55

1 Answer 1

2

For this case you can use Hibernate-search Filters. You also need to index that particuler field you need for filtering although you don't need it for query itself.

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

I use it myself and it works quiet good.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. But I don't know if it's right to index for example userId field and apply filter on it!! Like it's possible I have 20,000,000 records, and 1,000,000 of them have same userId. Is it fast enough to create filter on userId?
I'm not sure if it is really a problem. If you use some simple analyzer for that field. If you wanted to filtr on that field in SQL DB you would probably also use some index for that field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.