I have a Spring Boot 3.3.x application that uses Spring Data Elasticsearch to query a single Elasticsearch index.
The query is built dynamically using the Criteria API. Now, I need to add a new condition using a match_phrase query.
However, I couldn't find any documentation or examples showing how to use match_phrase with the Criteria API.
One option would be to refactor the entire solution to use native queries, but that doesn't seem reasonable or trivial in our case.
Another workaround I found is to use NativeQueryBuilder, set the CriteriaQuery as the main query, and then add a manually constructed match_phrase query as a filter:
return new NativeQueryBuilder()
.withQuery(criteriaQuery)
.withFilter(nativeMatchPhraseQuery)
.withPageable(pageable)
.build();
While this kind of works, it doesn't feel clean or intuitive.
Is there a way to use match_phrase within a Criteria query directly? Or a better pattern to combine Criteria and native queries more elegantly?