You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/elasticsearch/misc.adoc
+2-1
Original file line number
Diff line number
Diff line change
@@ -365,6 +365,8 @@ operations.putScript( <.>
365
365
366
366
To use a search template in a search query, Spring Data Elasticsearch provides the `SearchTemplateQuery`, an implementation of the `org.springframework.data.elasticsearch.core.query.Query` interface.
367
367
368
+
NOTE: Although `SearchTemplateQuery` is an implementation of the `Query` interface, not all of the functionality provided by the base class is available for a `SearchTemplateQuery` like setting a `Pageable` or a `Sort`. Values for this functionality must be added to the stored script like shown in the following example for paging parameters. If these values are set on the `Query` object, they will be ignored.
369
+
368
370
In the following code, we will add a call using a search template query to a custom repository implementation (see
369
371
xref:repositories/custom-implementations.adoc[]) as an example how this can be integrated into a repository call.
370
372
@@ -449,4 +451,3 @@ var query = Query.findAll().addSort(Sort.by(order));
449
451
About the filter query: It is not possible to use a `CriteriaQuery` here, as this query would be converted into a Elasticsearch nested query which does not work in the filter context. So only `StringQuery` or `NativeQuery` can be used here. When using one of these, like the term query above, the Elasticsearch field names must be used, so take care, when these are redefined with the `@Field(name="...")` definition.
450
452
451
453
For the definition of the order path and the nested paths, the Java entity property names should be used.
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/elasticsearch/repositories/elasticsearch-repository-queries.adoc
+36-6
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,9 @@ The Elasticsearch module supports all basic query building feature as string que
10
10
=== Declared queries
11
11
12
12
Deriving the query from the method name is not always sufficient and/or may result in unreadable method names.
13
-
In this case one might make use of the `@Query` annotation (see xref:elasticsearch/repositories/elasticsearch-repository-queries.adoc#elasticsearch.query-methods.at-query[Using @Query Annotation] ).
13
+
In this case one might make use of the `@Query` annotation (see xref:elasticsearch/repositories/elasticsearch-repository-queries.adoc#elasticsearch.query-methods.at-query[Using the @Query Annotation] ).
14
+
15
+
Another possibility is the use of a search-template, (see xref:elasticsearch/repositories/elasticsearch-repository-queries.adoc#elasticsearch.query-methods.at-searchtemplate-query[Using the @SearchTemplateQuery Annotation] ).
14
16
15
17
[[elasticsearch.query-methods.criterions]]
16
18
== Query creation
@@ -312,11 +314,13 @@ Repository methods can be defined to have the following return types for returni
312
314
* `SearchPage<T>`
313
315
314
316
[[elasticsearch.query-methods.at-query]]
315
-
== Using @Query Annotation
317
+
== Using the @Query Annotation
316
318
317
319
.Declare query on the method using the `@Query` annotation.
318
320
====
319
-
The arguments passed to the method can be inserted into placeholders in the query string. The placeholders are of the form `?0`, `?1`, `?2` etc. for the first, second, third parameter and so on.
321
+
The arguments passed to the method can be inserted into placeholders in the query string.
322
+
The placeholders are of the form `?0`, `?1`, `?2` etc. for the first, second, third parameter and so on.
would make an https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html[IDs query] to return all the matching documents. So calling the method with a `List` of `["id1", "id2", "id3"]` would produce the query body
358
+
359
+
would make an https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html[IDs query] to return all the matching documents.
360
+
So calling the method with a `List` of `["id1", "id2", "id3"]` would produce the query body
361
+
353
362
[source,json]
354
363
----
355
364
{
@@ -369,7 +378,6 @@ would make an https://www.elastic.co/guide/en/elasticsearch/reference/current/qu
369
378
====
370
379
https://docs.spring.io/spring-framework/reference/core/expressions.html[SpEL expression] is also supported when defining query in `@Query`.
@@ -411,6 +419,7 @@ If for example the function is called with the parameter _John_, it would produc
411
419
.accessing parameter property.
412
420
====
413
421
Supposing that we have the following class as query parameter type:
422
+
414
423
[source,java]
415
424
----
416
425
public record QueryParameter(String value) {
@@ -444,7 +453,9 @@ We can pass `new QueryParameter("John")` as the parameter now, and it will produ
444
453
445
454
.accessing bean property.
446
455
====
447
-
https://docs.spring.io/spring-framework/reference/core/expressions/language-ref/bean-references.html[Bean property] is also supported to access. Given that there is a bean named `queryParameter` of type `QueryParameter`, we can access the bean with symbol `@` rather than `#`, and there is no need to declare a parameter of type `QueryParameter` in the query method:
456
+
https://docs.spring.io/spring-framework/reference/core/expressions/language-ref/bean-references.html[Bean property] is also supported to access.
457
+
Given that there is a bean named `queryParameter` of type `QueryParameter`, we can access the bean with symbol `@` rather than `#`, and there is no need to declare a parameter of type `QueryParameter` in the query method:
When using Elasticsearch search templates - (see xref:elasticsearch/misc.adoc#elasticsearch.misc.searchtemplates [Search Template support]) it is possible to specify that a repository method should use a template by adding the `@SearchTemplateQuery` annotation to that method.
581
+
582
+
Let's assume that there is a search template stored with the name "book-by-title" and this template need a parameter named "title", then a repository method using that search template can be defined like this:
The parameters of the repository method are sent to the seacrh template as key/value pairs where the key is the parameter name and the value is taken from the actual value when the method is invoked.
Copy file name to clipboardExpand all lines: src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractElasticsearchRepositoryQuery.java
Copy file name to clipboardExpand all lines: src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractReactiveElasticsearchRepositoryQuery.java
0 commit comments