mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-31 09:12:11 +00:00
DATAES-786 - Polishing.
This commit is contained in:
parent
ff08d06c45
commit
99bf2fc0cb
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
* Copyright 2019-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -123,7 +123,6 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
// endregion
|
||||
|
||||
// region DocumentOperations
|
||||
|
||||
@Override
|
||||
public <T> T save(T entity) {
|
||||
|
||||
@ -307,8 +306,8 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
for (Query query : queries) {
|
||||
Class entityClass = it1.next();
|
||||
|
||||
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(
|
||||
entityClass, index);
|
||||
SearchDocumentResponseCallback<SearchHits<?>> callback = new ReadSearchDocumentResponseCallback<>(entityClass,
|
||||
index);
|
||||
|
||||
SearchResponse response = items[c++].getResponse();
|
||||
res.add(callback.doWith(SearchDocumentResponse.from(response)));
|
||||
@ -464,7 +463,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
|
||||
// endregion
|
||||
|
||||
// region callbacks
|
||||
// region Entity callbacks
|
||||
protected <T> T maybeCallbackBeforeConvert(T entity) {
|
||||
|
||||
if (entityCallbacks != null) {
|
||||
@ -523,8 +522,8 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
|
||||
// endregion
|
||||
|
||||
// region Document callbacks
|
||||
protected interface DocumentCallback<T> {
|
||||
|
||||
@Nullable
|
||||
T doWith(@Nullable Document document);
|
||||
}
|
||||
@ -535,6 +534,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
private final IndexCoordinates index;
|
||||
|
||||
public ReadDocumentCallback(EntityReader<? super T, Document> reader, Class<T> type, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(reader, "reader is null");
|
||||
Assert.notNull(type, "type is null");
|
||||
|
||||
@ -545,6 +545,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
|
||||
@Nullable
|
||||
public T doWith(@Nullable Document document) {
|
||||
|
||||
if (document == null) {
|
||||
return null;
|
||||
}
|
||||
@ -554,7 +555,6 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
}
|
||||
|
||||
protected interface SearchDocumentResponseCallback<T> {
|
||||
|
||||
@NonNull
|
||||
T doWith(@NonNull SearchDocumentResponse response);
|
||||
}
|
||||
@ -564,6 +564,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
private final Class<T> type;
|
||||
|
||||
public ReadSearchDocumentResponseCallback(Class<T> type, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(type, "type is null");
|
||||
|
||||
this.delegate = new ReadDocumentCallback<>(elasticsearchConverter, type, index);
|
||||
@ -572,11 +573,8 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
|
||||
@Override
|
||||
public SearchHits<T> doWith(SearchDocumentResponse response) {
|
||||
List<T> entities = response.getSearchDocuments().stream()
|
||||
.map(delegate::doWith)
|
||||
.collect(Collectors.toList());
|
||||
return SearchHitMapping.mappingFor(type, elasticsearchConverter.getMappingContext())
|
||||
.mapHits(response, entities);
|
||||
List<T> entities = response.getSearchDocuments().stream().map(delegate::doWith).collect(Collectors.toList());
|
||||
return SearchHitMapping.mappingFor(type, elasticsearchConverter.getMappingContext()).mapHits(response, entities);
|
||||
}
|
||||
}
|
||||
|
||||
@ -586,6 +584,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
private final Class<T> type;
|
||||
|
||||
public ReadSearchScrollDocumentResponseCallback(Class<T> type, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(type, "type is null");
|
||||
|
||||
this.delegate = new ReadDocumentCallback<>(elasticsearchConverter, type, index);
|
||||
@ -594,11 +593,10 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
|
||||
@Override
|
||||
public SearchScrollHits<T> doWith(SearchDocumentResponse response) {
|
||||
List<T> entities = response.getSearchDocuments().stream()
|
||||
.map(delegate::doWith)
|
||||
.collect(Collectors.toList());
|
||||
return SearchHitMapping.mappingFor(type, elasticsearchConverter.getMappingContext())
|
||||
.mapScrollHits(response, entities);
|
||||
List<T> entities = response.getSearchDocuments().stream().map(delegate::doWith).collect(Collectors.toList());
|
||||
return SearchHitMapping.mappingFor(type, elasticsearchConverter.getMappingContext()).mapScrollHits(response,
|
||||
entities);
|
||||
}
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
@ -172,9 +172,7 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
|
||||
MultiGetResponse result = execute(client -> client.mget(request, RequestOptions.DEFAULT));
|
||||
|
||||
DocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
return DocumentAdapters.from(result).stream()
|
||||
.map(callback::doWith)
|
||||
.collect(Collectors.toList());
|
||||
return DocumentAdapters.from(result).stream().map(callback::doWith).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -281,8 +279,8 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
|
||||
|
||||
SearchResponse response = execute(client -> client.search(searchRequest, RequestOptions.DEFAULT));
|
||||
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(
|
||||
clazz, index);
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
|
||||
index);
|
||||
return callback.doWith(SearchDocumentResponse.from(response));
|
||||
}
|
||||
|
||||
@ -325,7 +323,7 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region clientcallback
|
||||
// region ClientCallback
|
||||
/**
|
||||
* Callback interface to be used with {@link #execute(ClientCallback)} for operating directly on
|
||||
* {@link RestHighLevelClient}.
|
||||
|
@ -178,9 +178,7 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
|
||||
DocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
|
||||
List<Document> documents = DocumentAdapters.from(builder.execute().actionGet());
|
||||
return documents.stream()
|
||||
.map(callback::doWith)
|
||||
.collect(Collectors.toList());
|
||||
return documents.stream().map(callback::doWith).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -291,8 +289,8 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
|
||||
SearchResponse response = getSearchResponseWithTimeout(action);
|
||||
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(
|
||||
clazz, index);
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
|
||||
index);
|
||||
return callback.doWith(SearchDocumentResponse.from(response));
|
||||
}
|
||||
|
||||
@ -307,8 +305,8 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
|
||||
SearchResponse response = getSearchResponseWithTimeout(action);
|
||||
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(
|
||||
clazz, index);
|
||||
SearchDocumentResponseCallback<SearchScrollHits<T>> callback = new ReadSearchScrollDocumentResponseCallback<>(clazz,
|
||||
index);
|
||||
return callback.doWith(SearchDocumentResponse.from(response));
|
||||
}
|
||||
|
||||
|
@ -945,8 +945,7 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera
|
||||
@Override
|
||||
public Mono<SearchHit<T>> doWith(SearchDocument response) {
|
||||
return delegate.doWith(response)
|
||||
.map(entity -> SearchHitMapping.mappingFor(type, converter.getMappingContext())
|
||||
.mapHit(response, entity));
|
||||
.map(entity -> SearchHitMapping.mappingFor(type, converter.getMappingContext()).mapHit(response, entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -99,8 +99,7 @@ class SearchHitMapping<T> {
|
||||
searchHits.add(hit);
|
||||
}
|
||||
Aggregations aggregations = searchDocumentResponse.getAggregations();
|
||||
TotalHitsRelation totalHitsRelation = TotalHitsRelation
|
||||
.valueOf(searchDocumentResponse.getTotalHitsRelation());
|
||||
TotalHitsRelation totalHitsRelation = TotalHitsRelation.valueOf(searchDocumentResponse.getTotalHitsRelation());
|
||||
|
||||
return new SearchHitsImpl<>(totalHits, totalHitsRelation, maxScore, scrollId, searchHits, aggregations);
|
||||
}
|
||||
@ -119,8 +118,7 @@ class SearchHitMapping<T> {
|
||||
}
|
||||
|
||||
return highlightFields.entrySet().stream().collect(Collectors.toMap(entry -> {
|
||||
ElasticsearchPersistentProperty property = persistentEntity.getPersistentPropertyWithFieldName
|
||||
(entry.getKey());
|
||||
ElasticsearchPersistentProperty property = persistentEntity.getPersistentPropertyWithFieldName(entry.getKey());
|
||||
return property != null ? property.getName() : entry.getKey();
|
||||
}, Map.Entry::getValue));
|
||||
}
|
||||
|
@ -15,8 +15,17 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.convert;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
@ -1148,8 +1148,8 @@ public abstract class ElasticsearchTemplateTests {
|
||||
while (scroll.hasSearchHits()) {
|
||||
sampleEntities.addAll(scroll.getSearchHits());
|
||||
scrollId = scroll.getScrollId();
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000,
|
||||
SampleEntity.class, index);
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000, SampleEntity.class,
|
||||
index);
|
||||
}
|
||||
((AbstractElasticsearchTemplate) operations).searchScrollClear(scrollId);
|
||||
assertThat(sampleEntities).hasSize(30);
|
||||
@ -1176,8 +1176,8 @@ public abstract class ElasticsearchTemplateTests {
|
||||
while (scroll.hasSearchHits()) {
|
||||
sampleEntities.addAll(scroll.getSearchHits());
|
||||
scrollId = scroll.getScrollId();
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000,
|
||||
SampleEntity.class, index);
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000, SampleEntity.class,
|
||||
index);
|
||||
}
|
||||
((AbstractElasticsearchTemplate) operations).searchScrollClear(scrollId);
|
||||
assertThat(sampleEntities).hasSize(30);
|
||||
@ -1204,8 +1204,8 @@ public abstract class ElasticsearchTemplateTests {
|
||||
while (scroll.hasSearchHits()) {
|
||||
sampleEntities.addAll(scroll.getSearchHits());
|
||||
scrollId = scroll.getScrollId();
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000,
|
||||
SampleEntity.class, index);
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000, SampleEntity.class,
|
||||
index);
|
||||
}
|
||||
((AbstractElasticsearchTemplate) operations).searchScrollClear(scrollId);
|
||||
assertThat(sampleEntities).hasSize(30);
|
||||
@ -1232,8 +1232,8 @@ public abstract class ElasticsearchTemplateTests {
|
||||
while (scroll.hasSearchHits()) {
|
||||
sampleEntities.addAll(scroll.getSearchHits());
|
||||
scrollId = scroll.getScrollId();
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000,
|
||||
SampleEntity.class, index);
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000, SampleEntity.class,
|
||||
index);
|
||||
}
|
||||
((AbstractElasticsearchTemplate) operations).searchScrollClear(scrollId);
|
||||
assertThat(sampleEntities).hasSize(30);
|
||||
@ -1260,8 +1260,8 @@ public abstract class ElasticsearchTemplateTests {
|
||||
while (scroll.hasSearchHits()) {
|
||||
sampleEntities.addAll(scroll.getSearchHits());
|
||||
scrollId = scroll.getScrollId();
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000,
|
||||
SampleEntity.class, index);
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000, SampleEntity.class,
|
||||
index);
|
||||
}
|
||||
((AbstractElasticsearchTemplate) operations).searchScrollClear(scrollId);
|
||||
assertThat(sampleEntities).hasSize(30);
|
||||
@ -1288,8 +1288,8 @@ public abstract class ElasticsearchTemplateTests {
|
||||
while (scroll.hasSearchHits()) {
|
||||
sampleEntities.addAll(scroll.getSearchHits());
|
||||
scrollId = scroll.getScrollId();
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000,
|
||||
SampleEntity.class, index);
|
||||
scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scrollId, 1000, SampleEntity.class,
|
||||
index);
|
||||
}
|
||||
((AbstractElasticsearchTemplate) operations).searchScrollClear(scrollId);
|
||||
assertThat(sampleEntities).hasSize(30);
|
||||
|
@ -194,17 +194,17 @@ public class MappingElasticsearchConverterUnitTests {
|
||||
shotGunAsMap = Document.create();
|
||||
shotGunAsMap.put("model", "Ithaca 37 Pump Shotgun");
|
||||
shotGunAsMap.put("_class", ShotGun.class.getName());
|
||||
|
||||
|
||||
notificationAsMap = Document.create();
|
||||
notificationAsMap.put("id",1L);
|
||||
notificationAsMap.put("fromEmail","from@email.com");
|
||||
notificationAsMap.put("toEmail","to@email.com");
|
||||
Map<String,Object> data = new HashMap<>();
|
||||
data.put("documentType","abc");
|
||||
data.put("content",null);
|
||||
notificationAsMap.put("params",data);
|
||||
notificationAsMap.put("id", 1L);
|
||||
notificationAsMap.put("fromEmail", "from@email.com");
|
||||
notificationAsMap.put("toEmail", "to@email.com");
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("documentType", "abc");
|
||||
data.put("content", null);
|
||||
notificationAsMap.put("params", data);
|
||||
notificationAsMap.put("_class",
|
||||
"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Notification");
|
||||
"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Notification");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -627,37 +627,37 @@ public class MappingElasticsearchConverterUnitTests {
|
||||
assertThat(person.getBirthDate()).isEqualTo(LocalDate.of(2000, 8, 22));
|
||||
assertThat(person.getGender()).isEqualTo(Gender.MAN);
|
||||
}
|
||||
|
||||
@Test //DATAES-763
|
||||
|
||||
@Test // DATAES-763
|
||||
void writeEntityWithMapDataType() {
|
||||
|
||||
Notification notification = new Notification();
|
||||
notification.fromEmail="from@email.com";
|
||||
notification.toEmail="to@email.com";
|
||||
Map<String,Object> data = new HashMap<>();
|
||||
data.put("documentType","abc");
|
||||
data.put("content",null);
|
||||
notification.params= data;
|
||||
notification.id= 1L;
|
||||
notification.fromEmail = "from@email.com";
|
||||
notification.toEmail = "to@email.com";
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("documentType", "abc");
|
||||
data.put("content", null);
|
||||
notification.params = data;
|
||||
notification.id = 1L;
|
||||
|
||||
Document document = Document.create();
|
||||
mappingElasticsearchConverter.write(notification,document);
|
||||
mappingElasticsearchConverter.write(notification, document);
|
||||
assertThat(document).isEqualTo(notificationAsMap);
|
||||
}
|
||||
|
||||
@Test //DATAES-763
|
||||
@Test // DATAES-763
|
||||
void readEntityWithMapDataType() {
|
||||
|
||||
Document document = Document.create();
|
||||
document.put("id",1L);
|
||||
document.put("fromEmail","from@email.com");
|
||||
document.put("toEmail","to@email.com");
|
||||
Map<String,Object> data = new HashMap<>();
|
||||
data.put("documentType","abc");
|
||||
data.put("content",null);
|
||||
document.put("params",data);
|
||||
document.put("id", 1L);
|
||||
document.put("fromEmail", "from@email.com");
|
||||
document.put("toEmail", "to@email.com");
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("documentType", "abc");
|
||||
data.put("content", null);
|
||||
document.put("params", data);
|
||||
|
||||
Notification notification = mappingElasticsearchConverter.read(Notification.class,document);
|
||||
Notification notification = mappingElasticsearchConverter.read(Notification.class, document);
|
||||
assertThat(notification.params.get("documentType")).isEqualTo("abc");
|
||||
assertThat(notification.params.get("content")).isNull();
|
||||
}
|
||||
@ -794,14 +794,14 @@ public class MappingElasticsearchConverterUnitTests {
|
||||
List<Object> objectList;
|
||||
Map<String, Object> objectMap;
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
static class Notification {
|
||||
|
||||
Long id;
|
||||
String fromEmail;
|
||||
String toEmail;
|
||||
Map<String,Object> params;
|
||||
Map<String, Object> params;
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
|
Loading…
x
Reference in New Issue
Block a user