Hibernate QueryBuilder how to mimic 'like'

2019-08-16 12:58发布

问题:

I am learning this API now. I have some code as below, but it does an exact match instead of a 'like'. So when I have a String JMeter, and I use Meter, it does not bring it back in the search result but it should. Any help greatly appreciated

SearchManager searchManager = Search.getSearchManager(listingIndex);

 QueryBuilder qb = searchManager.buildQueryBuilderForClass(ListingIndexEntry.class).get();

Query q = qb.keyword().onField("title").matching(title).createQuery();

Thank you

Karthik

回答1:

If you are using hibernate search then I think you are looking for wildcard queries.

Check the documentation about it there section 5.1.2.3

So your code will become :

Query q = qb.keyword().wildcard().onField("title").matching("*"+title).createQuery();

Watchout for the performance as specified in the documentation, starting with a wildcard is not very good.