Skip to content

Commit 1331e26

Browse files
authored
Add example and test for @query with Collection (#2066)
Original Pull Request: #2066
1 parent 056a4a7 commit 1331e26

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Diff for: src/main/asciidoc/reference/elasticsearch-repository-queries.adoc

+23-1
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ Repository methods can be defined to have the following return types for returni
312312
[[elasticsearch.query-methods.at-query]]
313313
== Using @Query Annotation
314314

315-
.Declare query at the method using the `@Query` annotation.
315+
.Declare query on the method using the `@Query` annotation.
316316
====
317+
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.
317318
[source,java]
318319
----
319320
interface BookRepository extends ElasticsearchRepository<Book, String> {
@@ -338,3 +339,24 @@ It will be sent to Easticsearch as value of the query element; if for example th
338339
}
339340
----
340341
====
342+
.`@Query` annotation on a method taking a Collection argument
343+
====
344+
A repository method such as
345+
[source,java]
346+
----
347+
@Query("{\"ids\": {\"values\": ?0 }}")
348+
List<SampleEntity> getByIds(Collection<String> ids);
349+
----
350+
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
351+
[source,json]
352+
----
353+
{
354+
"query": {
355+
"ids": {
356+
"values": ["id1", "id2", "id3"]
357+
}
358+
}
359+
}
360+
----
361+
====
362+

Diff for: src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.Long;
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.UUID;
@@ -71,6 +72,7 @@
7172
* @author Don Wellington
7273
* @author Peter-Josef Meisch
7374
* @author Rasmus Faber-Espensen
75+
* @author James Mudd
7476
*/
7577
@SpringIntegrationTest
7678
public abstract class CustomMethodRepositoryIntegrationTests {
@@ -1648,6 +1650,24 @@ void shouldStreamSearchHitsWithQueryAnnotatedMethod() {
16481650
assertThat(count).isEqualTo(20);
16491651
}
16501652

1653+
@Test
1654+
void shouldBeAbleToUseCollectionInQueryAnnotatedMethod() {
1655+
List<SampleEntity> entities = createSampleEntities("abc", 20);
1656+
repository.saveAll(entities);
1657+
List<String> ids = entities.stream()
1658+
.map(SampleEntity::getId)
1659+
.limit(7) // Just get subset
1660+
.collect(Collectors.toList());
1661+
1662+
List<SampleEntity> sampleEntities = repository.getByIds(ids);
1663+
1664+
assertThat(sampleEntities).hasSize(7);
1665+
1666+
List<String> returnedIds = sampleEntities.stream().map(SampleEntity::getId).collect(Collectors.toList());
1667+
assertThat(returnedIds).containsAll(ids);
1668+
}
1669+
1670+
16511671
private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {
16521672

16531673
List<SampleEntity> entities = new ArrayList<>();
@@ -1881,6 +1901,9 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
18811901

18821902
@CountQuery("{\"bool\" : {\"must\" : {\"term\" : {\"type\" : \"?0\"}}}}")
18831903
long countWithQueryByType(String type);
1904+
1905+
@Query("{\"ids\" : {\"values\" : ?0 }}")
1906+
List<SampleEntity> getByIds(Collection<String> ids);
18841907
}
18851908

18861909
/**

0 commit comments

Comments
 (0)