DATAES-791 - DocumentOperations.multiGet() implementations must return null values for not found entities.

Original PR: #428
This commit is contained in:
Peter-Josef Meisch 2020-04-14 22:12:19 +02:00 committed by GitHub
parent 41fffc0fa5
commit 39fb25cbb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 7 deletions

View File

@ -16,7 +16,6 @@
package org.springframework.data.elasticsearch.core;
import java.util.List;
import java.util.Optional;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
@ -121,7 +120,7 @@ public interface DocumentOperations {
* @param query the query defining the ids of the objects to get
* @param clazz the type of the object to be returned
* @param index the index(es) from which the objects are read.
* @return list of objects
* @return list of objects, contains null values for ids that are not found
*/
<T> List<T> multiGet(Query query, Class<T> clazz, IndexCoordinates index);

View File

@ -118,16 +118,16 @@ public class DocumentAdapters {
* Creates a List of {@link Document}s from {@link MultiGetResponse}.
*
* @param source the source {@link MultiGetResponse}, not {@literal null}.
* @return a possibly empty list of the Documents.
* @return a list of Documents, contains null values for not found Documents.
*/
public static List<Document> from(MultiGetResponse source) {
Assert.notNull(source, "MultiGetResponse must not be null");
//noinspection ReturnOfNull
// noinspection ReturnOfNull
return Arrays.stream(source.getResponses()) //
.map(itemResponse -> itemResponse.isFailed() ? null : DocumentAdapters.from(itemResponse.getResponse())) //
.filter(Objects::nonNull).collect(Collectors.toList());
.collect(Collectors.toList());
}
/**
@ -299,8 +299,7 @@ public class DocumentAdapters {
public Object get(Object key) {
return documentFields.stream() //
.filter(documentField -> documentField.getName().equals(key)) //
.map(DocumentField::getValue)
.findFirst() //
.map(DocumentField::getValue).findFirst() //
.orElse(null); //
}

View File

@ -240,6 +240,39 @@ public abstract class ElasticsearchTemplateTests {
assertThat(sampleEntities.get(1)).isEqualTo(sampleEntity2);
}
@Test // DATAES-791
public void shouldReturnNullObjectForNotExistingIdUsingMultiGet() {
// given
// first document
String documentId = randomNumeric(5);
SampleEntity sampleEntity1 = SampleEntity.builder().id(documentId).message("some message")
.version(System.currentTimeMillis()).build();
// second document
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = SampleEntity.builder().id(documentId2).message("some message")
.version(System.currentTimeMillis()).build();
List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(sampleEntity1, sampleEntity2));
operations.bulkIndex(indexQueries, index);
indexOperations.refresh();
// when
List<String> idsToSearch = Arrays.asList(documentId, randomNumeric(5), documentId2);
assertThat(idsToSearch).hasSize(3);
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(idsToSearch).build();
List<SampleEntity> sampleEntities = operations.multiGet(query, SampleEntity.class, index);
// then
assertThat(sampleEntities).hasSize(3);
assertThat(sampleEntities.get(0)).isEqualTo(sampleEntity1);
assertThat(sampleEntities.get(1)).isNull();
assertThat(sampleEntities.get(2)).isEqualTo(sampleEntity2);
}
@Test
public void shouldReturnObjectsForGivenIdsUsingMultiGetWithFields() {