mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 21:22:12 +00:00
findAllById returns all requested documents.
Original Pull Request #2421 Closes #2417
This commit is contained in:
parent
605c83f628
commit
28489ffee8
@ -147,9 +147,11 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
|
||||
Assert.notNull(ids, "ids can't be null.");
|
||||
|
||||
List<T> result = new ArrayList<>();
|
||||
Query query = getIdQuery(ids);
|
||||
|
||||
List<String> stringIds = stringIdsRepresentation(ids);
|
||||
Query query = getIdQuery(stringIds);
|
||||
if (!stringIds.isEmpty()) {
|
||||
query.setPageable(PageRequest.of(0, stringIds.size()));
|
||||
}
|
||||
List<SearchHit<T>> searchHitList = execute(
|
||||
operations -> operations.search(query, entityClass, getIndexCoordinates()).getSearchHits());
|
||||
// noinspection ConstantConditions
|
||||
@ -320,9 +322,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
return operations.getIndexCoordinatesFor(entityClass);
|
||||
}
|
||||
|
||||
private Query getIdQuery(Iterable<? extends ID> ids) {
|
||||
List<String> stringIds = stringIdsRepresentation(ids);
|
||||
|
||||
private Query getIdQuery(List<String> stringIds) {
|
||||
return operations.idsQuery(stringIds);
|
||||
}
|
||||
// endregion
|
||||
|
@ -203,29 +203,27 @@ abstract class ElasticsearchRepositoryIntegrationTests {
|
||||
assertThat(entityFromElasticSearch).isNotPresent();
|
||||
}
|
||||
|
||||
@Test // DATAES-82
|
||||
@Test // DATAES-82, #2417
|
||||
void shouldFindAllByIdQuery() {
|
||||
|
||||
// given
|
||||
String documentId = nextIdAsString();
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("hello world.");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity);
|
||||
// create more than 10 documents to see that the number of input ids is set as requested size
|
||||
int numEntities = 20;
|
||||
List<String> ids = new ArrayList<>(numEntities);
|
||||
List<SampleEntity> entities = new ArrayList<>(numEntities);
|
||||
for (int i = 0; i < numEntities; i++) {
|
||||
String documentId = nextIdAsString();
|
||||
ids.add(documentId);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("hello world.");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
entities.add(sampleEntity);
|
||||
}
|
||||
repository.saveAll(entities);
|
||||
|
||||
String documentId2 = nextIdAsString();
|
||||
SampleEntity sampleEntity2 = new SampleEntity();
|
||||
sampleEntity2.setId(documentId2);
|
||||
sampleEntity2.setMessage("hello world.");
|
||||
sampleEntity2.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity2);
|
||||
Iterable<SampleEntity> sampleEntities = repository.findAllById(ids);
|
||||
|
||||
// when
|
||||
Iterable<SampleEntity> sampleEntities = repository.findAllById(Arrays.asList(documentId, documentId2));
|
||||
|
||||
// then
|
||||
assertThat(sampleEntities).isNotNull().hasSize(2);
|
||||
assertThat(sampleEntities).isNotNull().hasSize(numEntities);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -17,11 +17,13 @@ package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.elasticsearch.core.query.Query.*;
|
||||
import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -164,18 +166,27 @@ abstract class SimpleReactiveElasticsearchRepositoryIntegrationTests {
|
||||
repository.findAllById(Arrays.asList("id-two", "id-two")).as(StepVerifier::create).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAES-519
|
||||
@Test // DATAES-519, #2417
|
||||
void findAllByIdShouldRetrieveMatchingDocuments() {
|
||||
|
||||
bulkIndex(new SampleEntity("id-one"), //
|
||||
new SampleEntity("id-two"), //
|
||||
new SampleEntity("id-three")) //
|
||||
.block();
|
||||
// create more than 10 documents to see that the number of input ids is set as requested size
|
||||
int numEntities = 20;
|
||||
List<String> ids = new ArrayList<>(numEntities);
|
||||
List<SampleEntity> entities = new ArrayList<>(numEntities);
|
||||
for (int i = 0; i < numEntities; i++) {
|
||||
String documentId = nextIdAsString();
|
||||
ids.add(documentId);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("hello world.");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
entities.add(sampleEntity);
|
||||
}
|
||||
repository.saveAll(entities).blockLast();
|
||||
|
||||
repository.findAllById(Arrays.asList("id-one", "id-two")) //
|
||||
repository.findAllById(ids) //
|
||||
.as(StepVerifier::create)//
|
||||
.expectNextMatches(entity -> entity.getId().equals("id-one") || entity.getId().equals("id-two")) //
|
||||
.expectNextMatches(entity -> entity.getId().equals("id-one") || entity.getId().equals("id-two")) //
|
||||
.expectNextCount(numEntities) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user