Skip to content

Commit b439aca

Browse files
authored
DATAES-832 - findAllById repository method returns iterable with null elements for not found ids.
#Original PR: spring-projects#460
1 parent 506f79a commit b439aca

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.Objects;
2223
import java.util.Optional;
2324
import java.util.stream.Collectors;
2425

@@ -153,8 +154,8 @@ public Iterable<T> findAll(Sort sort) {
153154
public Iterable<T> findAllById(Iterable<ID> ids) {
154155
Assert.notNull(ids, "ids can't be null.");
155156
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build();
156-
// noinspection ConstantConditions
157-
return execute(operations -> operations.multiGet(query, entityClass, getIndexCoordinates()));
157+
return execute(operations1 -> operations1.multiGet(query, entityClass, getIndexCoordinates())).stream()
158+
.filter(Objects::nonNull).collect(Collectors.toList());
158159
}
159160

160161
@Override
@@ -214,7 +215,7 @@ public Iterable<T> search(QueryBuilder query) {
214215
if (count == 0) {
215216
return new PageImpl<>(Collections.emptyList());
216217
}
217-
searchQuery.setPageable(PageRequest.of(0, (int)count));
218+
searchQuery.setPageable(PageRequest.of(0, (int) count));
218219
SearchHits<T> searchHits = execute(
219220
operations -> operations.search(searchQuery, entityClass, getIndexCoordinates()));
220221
AggregatedPage<SearchHit<T>> page = SearchHitSupport.page(searchHits, searchQuery.getPageable());

src/test/java/org/springframework/data/elasticsearch/repository/support/simple/SimpleElasticsearchRepositoryTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import lombok.Data;
2626
import lombok.NoArgsConstructor;
2727

28+
import java.io.IOException;
2829
import java.lang.Long;
2930
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Optional;
35+
import java.util.stream.Collectors;
3436

3537
import org.junit.jupiter.api.AfterEach;
3638
import org.junit.jupiter.api.BeforeEach;
@@ -56,6 +58,7 @@
5658
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
5759
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
5860
import org.springframework.data.elasticsearch.utils.IndexInitializer;
61+
import org.springframework.data.util.StreamUtils;
5962
import org.springframework.test.context.ContextConfiguration;
6063

6164
/**
@@ -361,6 +364,14 @@ public void shouldReturnResultsForGivenSearchQuery() {
361364
@Test
362365
public void shouldDeleteAll() {
363366

367+
// given
368+
String documentId = randomNumeric(5);
369+
SampleEntity sampleEntity = new SampleEntity();
370+
sampleEntity.setId(documentId);
371+
sampleEntity.setMessage("hello world.");
372+
sampleEntity.setVersion(System.currentTimeMillis());
373+
repository.save(sampleEntity);
374+
364375
// when
365376
repository.deleteAll();
366377

@@ -677,6 +688,32 @@ public void shouldNotFailOnIndexingEmptyList() {
677688
assertThat(savedEntities).hasSize(0);
678689
}
679690

691+
@Test // DATAES-832
692+
void shouldNotReturnNullValuesInFindAllById() throws IOException {
693+
694+
// given
695+
String documentId1 = "id-one";
696+
SampleEntity sampleEntity1 = new SampleEntity();
697+
sampleEntity1.setId(documentId1);
698+
repository.save(sampleEntity1);
699+
String documentId2 = "id-two";
700+
SampleEntity sampleEntity2 = new SampleEntity();
701+
sampleEntity2.setId(documentId2);
702+
repository.save(sampleEntity2);
703+
String documentId3 = "id-three";
704+
SampleEntity sampleEntity3 = new SampleEntity();
705+
sampleEntity3.setId(documentId3);
706+
repository.save(sampleEntity3);
707+
708+
Iterable<SampleEntity> allById = repository
709+
.findAllById(Arrays.asList("id-one", "does-not-exist", "id-two", "where-am-i", "id-three"));
710+
List<SampleEntity> results = StreamUtils.createStreamFromIterator(allById.iterator()).collect(Collectors.toList());
711+
712+
assertThat(results).hasSize(3);
713+
assertThat(results.stream().map(SampleEntity::getId).collect(Collectors.toList()))
714+
.containsExactlyInAnyOrder("id-one", "id-two", "id-three");
715+
}
716+
680717
private static List<SampleEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities) {
681718

682719
List<SampleEntity> sampleEntities = new ArrayList<>();

0 commit comments

Comments
 (0)