Skip to content

findAllById returns all requested documents. #2421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ public Iterable<T> findAllById(Iterable<ID> ids) {

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
Expand Down Expand Up @@ -320,9 +322,7 @@ private IndexCoordinates getIndexCoordinates() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,27 @@ void shouldDeleteDocument() {
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);

String documentId2 = nextIdAsString();
SampleEntity sampleEntity2 = new SampleEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
// 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);

// when
Iterable<SampleEntity> sampleEntities = repository.findAllById(Arrays.asList(documentId, documentId2));
Iterable<SampleEntity> sampleEntities = repository.findAllById(ids);

// then
assertThat(sampleEntities).isNotNull().hasSize(2);
assertThat(sampleEntities).isNotNull().hasSize(numEntities);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

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;
Expand Down Expand Up @@ -164,18 +166,27 @@ void findAllByIdByIdShouldCompleteIfIndexDoesNotExist() {
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();
}

Expand Down