In Spring Data Elasticsearch, Query is the interface that describes a search request, and Criteria is a fluent builder for the filter conditions inside a CriteriaQuery. Together they let you build searches in Java instead of writing raw JSON. Criteria suits simple to moderate conditions on fields, while NativeQuery and StringQuery give access to the full Elasticsearch Query DSL.
You run any Query through ElasticsearchOperations (or the older ElasticsearchRestTemplate) and receive a SearchHits result with scores and metadata.
org.springframework.data.elasticsearch.core.query.Query is the common interface. It carries paging, sorting, source filtering and similar settings. The main implementations are:
| Type | Built from | Best for |
|---|---|---|
CriteriaQuery | Criteria objects | Field conditions, dynamic filters |
StringQuery | A JSON query string | Reusing a query written in Kibana |
NativeQuery | The Elasticsearch Java client query objects | Aggregations, full-text scoring, anything advanced |
NativeQuery replaced NativeSearchQuery in Spring Data Elasticsearch 5.0, which was built for the new Elasticsearch Java client in Spring Boot 3.x. If you are on an older stack, the class names differ (NativeSearchQueryBuilder with QueryBuilders), so check your version.
Criteria starts with a field name and chains operators. Chaining with and() and or() combines conditions, and not() negates the next one.
Common operators:
is(value) and not() for exact matches and negationcontains, startsWith, endsWith and matches for text patternsgreaterThan, lessThan, between for rangesin(values) and notIn(values) for setsexists() for fields that must be presentboost(float) to raise the score of a clauseThe wildcard-based text operators can be slow on large indices. Prefer analyzed full-text queries with NativeQuery for search-box behaviour.
PageRequest supplies paging, and Sort can be added the same way. SearchHits also exposes getTotalHits(), scores and highlights.
When you need relevance-scored text search or aggregations, build the query with the Elasticsearch Java client lambdas:
Clauses in filter do not affect scoring and are cached, so put exact conditions there and keep must for scored full-text clauses.
@Query on repositories for fixed, simple searches.Criteria when conditions are assembled dynamically from optional request parameters.NativeQuery when you need analyzers, scoring control, aggregations or nested and geo queries.StringQuery when you already have working JSON and want to parameterize it.A practical pattern for optional filters is to start with an empty Criteria and add and(...) only for parameters that are present.
Query represents the entire search request including paging and sorting. Criteria only describes the filter conditions and is wrapped by CriteriaQuery.
It was replaced by NativeQuery in Spring Data Elasticsearch 5.0. Older projects on 4.x still use NativeSearchQuery.
Operators such as matches and contains work, but they translate to simpler queries. Use NativeQuery with match clauses for analyzer-aware, scored search.
Pass it to ElasticsearchOperations.search(query, Entity.class) and read the returned SearchHits.
Query models the whole search request and Criteria builds readable field conditions for a CriteriaQuery. Use Criteria for dynamic filters, NativeQuery for the full Elasticsearch DSL, and run everything through ElasticsearchOperations.