mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-31 09:12:11 +00:00
DATAES-832 - findAllById repository method returns iterable with null elements for not found ids.
#Original PR: #460
This commit is contained in:
parent
506f79a45a
commit
b439acac16
@ -19,6 +19,7 @@ import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -153,8 +154,8 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
public Iterable<T> findAllById(Iterable<ID> ids) {
|
||||
Assert.notNull(ids, "ids can't be null.");
|
||||
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build();
|
||||
// noinspection ConstantConditions
|
||||
return execute(operations -> operations.multiGet(query, entityClass, getIndexCoordinates()));
|
||||
return execute(operations1 -> operations1.multiGet(query, entityClass, getIndexCoordinates())).stream()
|
||||
.filter(Objects::nonNull).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -214,7 +215,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
if (count == 0) {
|
||||
return new PageImpl<>(Collections.emptyList());
|
||||
}
|
||||
searchQuery.setPageable(PageRequest.of(0, (int)count));
|
||||
searchQuery.setPageable(PageRequest.of(0, (int) count));
|
||||
SearchHits<T> searchHits = execute(
|
||||
operations -> operations.search(searchQuery, entityClass, getIndexCoordinates()));
|
||||
AggregatedPage<SearchHit<T>> page = SearchHitSupport.page(searchHits, searchQuery.getPageable());
|
||||
|
@ -25,12 +25,14 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.Long;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -56,6 +58,7 @@ import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTes
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
|
||||
import org.springframework.data.elasticsearch.utils.IndexInitializer;
|
||||
import org.springframework.data.util.StreamUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
@ -361,6 +364,14 @@ public class SimpleElasticsearchRepositoryTests {
|
||||
@Test
|
||||
public void shouldDeleteAll() {
|
||||
|
||||
// given
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("hello world.");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
repository.save(sampleEntity);
|
||||
|
||||
// when
|
||||
repository.deleteAll();
|
||||
|
||||
@ -677,6 +688,32 @@ public class SimpleElasticsearchRepositoryTests {
|
||||
assertThat(savedEntities).hasSize(0);
|
||||
}
|
||||
|
||||
@Test // DATAES-832
|
||||
void shouldNotReturnNullValuesInFindAllById() throws IOException {
|
||||
|
||||
// given
|
||||
String documentId1 = "id-one";
|
||||
SampleEntity sampleEntity1 = new SampleEntity();
|
||||
sampleEntity1.setId(documentId1);
|
||||
repository.save(sampleEntity1);
|
||||
String documentId2 = "id-two";
|
||||
SampleEntity sampleEntity2 = new SampleEntity();
|
||||
sampleEntity2.setId(documentId2);
|
||||
repository.save(sampleEntity2);
|
||||
String documentId3 = "id-three";
|
||||
SampleEntity sampleEntity3 = new SampleEntity();
|
||||
sampleEntity3.setId(documentId3);
|
||||
repository.save(sampleEntity3);
|
||||
|
||||
Iterable<SampleEntity> allById = repository
|
||||
.findAllById(Arrays.asList("id-one", "does-not-exist", "id-two", "where-am-i", "id-three"));
|
||||
List<SampleEntity> results = StreamUtils.createStreamFromIterator(allById.iterator()).collect(Collectors.toList());
|
||||
|
||||
assertThat(results).hasSize(3);
|
||||
assertThat(results.stream().map(SampleEntity::getId).collect(Collectors.toList()))
|
||||
.containsExactlyInAnyOrder("id-one", "id-two", "id-three");
|
||||
}
|
||||
|
||||
private static List<SampleEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities) {
|
||||
|
||||
List<SampleEntity> sampleEntities = new ArrayList<>();
|
||||
|
Loading…
x
Reference in New Issue
Block a user