mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-10-13 05:48:55 +00:00
DATAES-791 - DocumentOperations.multiGet() implementations must return null values for not found entities.
Original PR: #428
This commit is contained in:
parent
41fffc0fa5
commit
39fb25cbb9
@ -16,7 +16,6 @@
|
|||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||||
import org.springframework.data.elasticsearch.core.query.BulkOptions;
|
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 query the query defining the ids of the objects to get
|
||||||
* @param clazz the type of the object to be returned
|
* @param clazz the type of the object to be returned
|
||||||
* @param index the index(es) from which the objects are read.
|
* @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);
|
<T> List<T> multiGet(Query query, Class<T> clazz, IndexCoordinates index);
|
||||||
|
|
||||||
|
@ -118,16 +118,16 @@ public class DocumentAdapters {
|
|||||||
* Creates a List of {@link Document}s from {@link MultiGetResponse}.
|
* Creates a List of {@link Document}s from {@link MultiGetResponse}.
|
||||||
*
|
*
|
||||||
* @param source the source {@link MultiGetResponse}, not {@literal null}.
|
* @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) {
|
public static List<Document> from(MultiGetResponse source) {
|
||||||
|
|
||||||
Assert.notNull(source, "MultiGetResponse must not be null");
|
Assert.notNull(source, "MultiGetResponse must not be null");
|
||||||
|
|
||||||
//noinspection ReturnOfNull
|
// noinspection ReturnOfNull
|
||||||
return Arrays.stream(source.getResponses()) //
|
return Arrays.stream(source.getResponses()) //
|
||||||
.map(itemResponse -> itemResponse.isFailed() ? null : DocumentAdapters.from(itemResponse.getResponse())) //
|
.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) {
|
public Object get(Object key) {
|
||||||
return documentFields.stream() //
|
return documentFields.stream() //
|
||||||
.filter(documentField -> documentField.getName().equals(key)) //
|
.filter(documentField -> documentField.getName().equals(key)) //
|
||||||
.map(DocumentField::getValue)
|
.map(DocumentField::getValue).findFirst() //
|
||||||
.findFirst() //
|
|
||||||
.orElse(null); //
|
.orElse(null); //
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -240,6 +240,39 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
assertThat(sampleEntities.get(1)).isEqualTo(sampleEntity2);
|
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
|
@Test
|
||||||
public void shouldReturnObjectsForGivenIdsUsingMultiGetWithFields() {
|
public void shouldReturnObjectsForGivenIdsUsingMultiGetWithFields() {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user