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