Combine Hibernate Search query with Lucene query

We have been using Hibernate Search 5.11.7.Final but because of some features with syntax highlighting we decided to upgrade to Hibernate Search 6.

Until now we have been using a MultiFieldQueryParser for the search term adding some date and numerice range queries like e.g.

final Query fromRangeQuery = builder.range().onField(nqd.getIndexFromDate())
		.above(nqd.getFromDate().atStartOfDay()).createQuery();
booleanQuery.add(fromRangeQuery, BooleanClause.Occur.MUST);

and passing the booleanQuery to Hibernate Search FullTextQuery, working like a charm.

Now we try using something like this:

final SearchQuery<MyEntity> query = session.search(MyEntity.class)
	.where(f -> f.bool(b -> {
		b.must(f.match()
			.fields(IndexField1,
				IndexField1,
				IndexField3)
			.matching("SearchTerm"));
		b.must(f.range().fields(queryDto.getIndex).atLeast(queryDto.getFrom()));
	})).sort(SortOrder.DESC).toQuery();

which is a Hibernate Search Query but the MultiFieldQueryParser seems to be available only in Lucene
and this works:

QueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
List<MyClass> hits = searchSession.search( MyClass.class )
        .extension( LuceneExtension.get() )
        .predicate( f -> f.fromLuceneQuery( parser.parse(terms) ) )
        .fetchHits();

Hibernate-search 6 MultiFieldQueryParser equivalent

The only problem left is:
How can I combine those two queries?

Best regards

The result of f.fromLuceneQuery is a predicate that you can combine like any other.

So, something like this?

		final SearchQuery<MyEntity> query = session.search(MyEntity.class)
			.extension( LuceneExtension.get() )
			.where(f -> f.bool(b -> {
				b.must(f.match()
					.fields(IndexField1,
						IndexField1,
						IndexField3)
					.matching("SearchTerm"));

				b.must(f.range().fields(queryDto.getIndex).atLeast(queryDto.getFrom()));

				QueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
				b.must(f.fromLuceneQuery(parser.parse(terms)));
			})).sort(SortOrder.DESC).toQuery();
2 Likes

Man this was a fast reply :smiley:
I will try this out and give you feedback.

Thanks in advance
Best regards

I just gave it a try and it seems to work.

Thank you very much.

1 Like