In Spring Data Elasticsearch, @Query lets you attach a hand-written Elasticsearch query, in JSON Query DSL, to a repository method. Spring substitutes the method arguments into placeholders such as ?0, runs the query and maps the hits to your document class. Use it when a derived method name cannot express the search, for example a bool query with must, should and filter clauses.
The annotation is org.springframework.data.elasticsearch.annotations.Query. Do not confuse it with the Spring Data JPA or MongoDB annotations of the same name, which live in different packages and use different query languages.
@Query is not the right toolA repository method normally gets its query from its name, such as findByTitleContaining. That covers simple term and range conditions. Elasticsearch offers much more: full-text match queries, multi_match across fields, bool combinations, fuzziness, boosting and nested queries. @Query gives you direct access to all of them by letting you write the query body yourself.
The string you put in the annotation is the query part of a search request, that is, what you would normally place under the query key. Spring wraps it in the search request for you.
The examples use a Book document stored in a books index.
title is a Text field, analysed for full-text search, while author is a Keyword field for exact matches. That distinction decides which query type you need.
Placeholders ?0, ?1 and so on refer to the method parameters by position.
The first method runs a full-text match on title and supports paging through Pageable. The second uses a bool query: the must clause scores relevance, while the filter clauses narrow the result without affecting the score and can be cached by Elasticsearch. Text blocks (Java 15+) avoid the escaped quotes.
Numbers such as ?2 are written without quotes so that they stay JSON numbers; string placeholders are written inside quotes.
PageRequest comes from org.springframework.data.domain. The returned Page carries the content, the total hit count and paging metadata.
@Query is not the right tool| Need | Better option |
|---|---|
| Simple equality or range conditions | Derived query methods such as findByAuthorAndYearGreaterThan |
| Filters that depend on user input at runtime | NativeQuery builder or ElasticsearchOperations |
| Programmatic, type-safe criteria | The Criteria API |
| Aggregations and highlighting | ElasticsearchOperations with a NativeQuery |
A @Query string is fixed at compile time. When the search form can include any combination of filters, building the query in code with ElasticsearchOperations is easier than maintaining many annotated methods.
match query against a Keyword field, or a term query against an analysed Text field. Use term for keywords and match for text.{"query": ...} object. The annotation already represents the query part.@Query annotation come from?It is org.springframework.data.elasticsearch.annotations.Query. Importing the JPA or MongoDB annotation by mistake is a common cause of startup errors.
@Query methods return pages and use sorting?Yes. Add a Pageable parameter and return Page or SearchHits. Sorting can be passed through the Pageable.
@Query for every search?No. Derived method names handle straightforward lookups. Reach for @Query when you need bool, multi_match, fuzziness or other DSL features.
@Query in Spring Data JPA?The JPA annotation holds JPQL or SQL; this one holds Elasticsearch JSON Query DSL with ?0 style placeholders.
@Query in Spring Data Elasticsearch binds a JSON Query DSL string to a repository method, with positional placeholders for parameters and support for paging. Use it for full-text and bool queries that method names cannot express, and switch to NativeQuery or ElasticsearchOperations when the query must be assembled dynamically.