Glide
Glide

Reputation: 21275

Hibernate Search - specify two types of searches on the same field of a class

If I specify two types of searches (matching and fuzzy) on the same field of a class like the following:

  QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(User.class).get();
  qb.bool().should(qb.keyword().onField("name").matching(searchQuery).createQuery())
     .should(qb.keyword().fuzzy().withPrefixLength(1).onField("name").matching(searchQuery).createQuery());

Will the search above end up being:

MATCHING searchQuery against "name" OR Fuzzy searchQuery against "name"

Upvotes: 0

Views: 251

Answers (1)

femtoRgon
femtoRgon

Reputation: 33351

I believe you may be missing a createQuery() at the end, but otherwise, looks reasonable enough to me, but you can always check for yourself. Once you have created the final query, just use the Query.toString() method, which should give you a human readable representation of the query, provided you are reasonably familiar with Lucene query parser syntax. Like:

QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(User.class).get();
Query query = qb.bool()
    .should(qb.keyword().onField("name").matching(searchQuery).createQuery())
    .should(qb.keyword().fuzzy().withPrefixLength(1).onField("name").matching(searchQuery).createQuery())
.createQuery();

System.out.println(query.toString())
//Or however you like to output debugging information...

Upvotes: 3

Related Questions