diff --git a/src/main/asciidoc/reference/elasticsearch-repository-queries.adoc b/src/main/asciidoc/reference/elasticsearch-repository-queries.adoc index 2865c471b..50ab16a24 100644 --- a/src/main/asciidoc/reference/elasticsearch-repository-queries.adoc +++ b/src/main/asciidoc/reference/elasticsearch-repository-queries.adoc @@ -312,8 +312,9 @@ Repository methods can be defined to have the following return types for returni [[elasticsearch.query-methods.at-query]] == Using @Query Annotation -.Declare query at the method using the `@Query` annotation. +.Declare query on the method using the `@Query` annotation. ==== +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. [source,java] ---- interface BookRepository extends ElasticsearchRepository { @@ -338,3 +339,24 @@ It will be sent to Easticsearch as value of the query element; if for example th } ---- ==== +.`@Query` annotation on a method taking a Collection argument +==== +A repository method such as +[source,java] +---- +@Query("{\"ids\": {\"values\": ?0 }}") +List getByIds(Collection ids); +---- +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 +[source,json] +---- +{ + "query": { + "ids": { + "values": ["id1", "id2", "id3"] + } + } +} +---- +==== + diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java index a0c8ac30a..71d2e496b 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java @@ -22,6 +22,7 @@ import java.lang.Long; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.UUID; @@ -71,6 +72,7 @@ * @author Don Wellington * @author Peter-Josef Meisch * @author Rasmus Faber-Espensen + * @author James Mudd */ @SpringIntegrationTest public abstract class CustomMethodRepositoryIntegrationTests { @@ -1648,6 +1650,24 @@ void shouldStreamSearchHitsWithQueryAnnotatedMethod() { assertThat(count).isEqualTo(20); } + @Test + void shouldBeAbleToUseCollectionInQueryAnnotatedMethod() { + List entities = createSampleEntities("abc", 20); + repository.saveAll(entities); + List ids = entities.stream() + .map(SampleEntity::getId) + .limit(7) // Just get subset + .collect(Collectors.toList()); + + List sampleEntities = repository.getByIds(ids); + + assertThat(sampleEntities).hasSize(7); + + List returnedIds = sampleEntities.stream().map(SampleEntity::getId).collect(Collectors.toList()); + assertThat(returnedIds).containsAll(ids); + } + + private List createSampleEntities(String type, int numberOfEntities) { List entities = new ArrayList<>(); @@ -1881,6 +1901,9 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository getByIds(Collection ids); } /**