mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-30 16:52:11 +00:00
Polishing.
This commit is contained in:
parent
929d97f255
commit
863ac2f3f5
@ -15,6 +15,18 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.reactive;
|
||||
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.handler.ssl.ApplicationProtocolConfig;
|
||||
import io.netty.handler.ssl.ClientAuth;
|
||||
import io.netty.handler.ssl.IdentityCipherSuiteFilter;
|
||||
import io.netty.handler.ssl.JdkSslContext;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.netty.transport.ProxyProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.ConnectException;
|
||||
@ -120,18 +132,6 @@ import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
|
||||
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.handler.ssl.ApplicationProtocolConfig;
|
||||
import io.netty.handler.ssl.ClientAuth;
|
||||
import io.netty.handler.ssl.IdentityCipherSuiteFilter;
|
||||
import io.netty.handler.ssl.JdkSslContext;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.netty.transport.ProxyProvider;
|
||||
|
||||
/**
|
||||
* A {@link WebClient} based {@link ReactiveElasticsearchClient} that connects to an Elasticsearch cluster using HTTP.
|
||||
*
|
||||
@ -760,9 +760,9 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GetIndexResponse> getIndex(HttpHeaders headers, org.elasticsearch.client.indices.GetIndexRequest getIndexRequest) {
|
||||
return sendRequest(getIndexRequest, requestCreator.getIndex(), GetIndexResponse.class, headers)
|
||||
.next();
|
||||
public Mono<GetIndexResponse> getIndex(HttpHeaders headers,
|
||||
org.elasticsearch.client.indices.GetIndexRequest getIndexRequest) {
|
||||
return sendRequest(getIndexRequest, requestCreator.getIndex(), GetIndexResponse.class, headers).next();
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.client.reactive;
|
||||
|
||||
import org.elasticsearch.client.indices.GetIndexResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@ -54,6 +53,7 @@ import org.elasticsearch.action.update.UpdateResponse;
|
||||
import org.elasticsearch.client.GetAliasesResponse;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
|
||||
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
|
||||
import org.elasticsearch.client.indices.GetIndexResponse;
|
||||
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
|
||||
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
|
||||
@ -1458,6 +1458,39 @@ public interface ReactiveElasticsearchClient {
|
||||
*/
|
||||
Mono<Boolean> deleteTemplate(HttpHeaders headers, DeleteIndexTemplateRequest deleteIndexTemplateRequest);
|
||||
|
||||
Mono<GetIndexResponse> getIndex(HttpHeaders headers, org.elasticsearch.client.indices.GetIndexRequest getIndexRequest);
|
||||
/**
|
||||
* Execute the given {@link GetIndexRequest} against the {@literal indices} API.
|
||||
*
|
||||
* @param consumer never {@literal null}.
|
||||
* @return the {@link Mono} emitting the response
|
||||
* @since 4.2
|
||||
*/
|
||||
default Mono<GetIndexResponse> getIndex(Consumer<org.elasticsearch.client.indices.GetIndexRequest> consumer) {
|
||||
org.elasticsearch.client.indices.GetIndexRequest getIndexRequest = new org.elasticsearch.client.indices.GetIndexRequest();
|
||||
consumer.accept(getIndexRequest);
|
||||
return getIndex(getIndexRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the given {@link GetIndexRequest} against the {@literal indices} API.
|
||||
*
|
||||
* @param getIndexRequest must not be {@literal null}
|
||||
* @return the {@link Mono} emitting the response
|
||||
* @since 4.2
|
||||
*/
|
||||
default Mono<GetIndexResponse> getIndex(org.elasticsearch.client.indices.GetIndexRequest getIndexRequest) {
|
||||
return getIndex(HttpHeaders.EMPTY, getIndexRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the given {@link GetIndexRequest} against the {@literal indices} API.
|
||||
*
|
||||
* @param headers Use {@link HttpHeaders} to provide eg. authentication data. Must not be {@literal null}.
|
||||
* @param getIndexRequest must not be {@literal null}
|
||||
* @return the {@link Mono} emitting the response
|
||||
* @since 4.2
|
||||
*/
|
||||
Mono<GetIndexResponse> getIndex(HttpHeaders headers,
|
||||
org.elasticsearch.client.indices.GetIndexRequest getIndexRequest);
|
||||
}
|
||||
}
|
||||
|
@ -150,7 +150,6 @@ public interface RequestCreator {
|
||||
return RequestConverters::count;
|
||||
}
|
||||
|
||||
default Function<org.elasticsearch.client.indices.GetIndexRequest, Request> getIndex() { return RequestConverters::getIndex; }
|
||||
/**
|
||||
* @since 4.1
|
||||
*/
|
||||
@ -213,4 +212,11 @@ public interface RequestCreator {
|
||||
default Function<GetFieldMappingsRequest, Request> getFieldMapping() {
|
||||
return RequestConverters::getFieldMapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.2
|
||||
*/
|
||||
default Function<org.elasticsearch.client.indices.GetIndexRequest, Request> getIndex() {
|
||||
return RequestConverters::getIndex;
|
||||
}
|
||||
}
|
||||
|
@ -54,12 +54,10 @@ abstract class AbstractDefaultIndexOperations implements IndexOperations {
|
||||
|
||||
protected final ElasticsearchConverter elasticsearchConverter;
|
||||
protected final RequestFactory requestFactory;
|
||||
protected final ResponseConverter responseConverter;
|
||||
|
||||
@Nullable protected final Class<?> boundClass;
|
||||
@Nullable private final IndexCoordinates boundIndex;
|
||||
|
||||
|
||||
public AbstractDefaultIndexOperations(ElasticsearchConverter elasticsearchConverter, Class<?> boundClass) {
|
||||
|
||||
Assert.notNull(boundClass, "boundClass may not be null");
|
||||
@ -68,7 +66,6 @@ abstract class AbstractDefaultIndexOperations implements IndexOperations {
|
||||
requestFactory = new RequestFactory(elasticsearchConverter);
|
||||
this.boundClass = boundClass;
|
||||
this.boundIndex = null;
|
||||
this.responseConverter = new ResponseConverter();
|
||||
}
|
||||
|
||||
public AbstractDefaultIndexOperations(ElasticsearchConverter elasticsearchConverter, IndexCoordinates boundIndex) {
|
||||
@ -79,7 +76,6 @@ abstract class AbstractDefaultIndexOperations implements IndexOperations {
|
||||
requestFactory = new RequestFactory(elasticsearchConverter);
|
||||
this.boundClass = null;
|
||||
this.boundIndex = boundIndex;
|
||||
this.responseConverter = new ResponseConverter();
|
||||
}
|
||||
|
||||
protected Class<?> checkForBoundClass() {
|
||||
|
@ -52,14 +52,13 @@ import org.springframework.data.elasticsearch.core.index.GetTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.core.query.AliasQuery;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link IndexOperations} implementation using the RestClient.
|
||||
*
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Sascha Woo
|
||||
* @author George Popides
|
||||
@ -178,8 +177,8 @@ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements I
|
||||
|
||||
GetAliasesRequest getAliasesRequest = requestFactory.getAliasesRequest(aliasNames, indexNames);
|
||||
|
||||
return restTemplate.execute(client -> responseConverter
|
||||
.convertAliasesResponse(client.indices().getAlias(getAliasesRequest, RequestOptions.DEFAULT).getAliases()));
|
||||
return restTemplate.execute(client -> ResponseConverter
|
||||
.aliasDatas(client.indices().getAlias(getAliasesRequest, RequestOptions.DEFAULT).getAliases()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -199,7 +198,7 @@ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements I
|
||||
GetSettingsResponse response = restTemplate.execute(client -> client.indices() //
|
||||
.getSettings(getSettingsRequest, RequestOptions.DEFAULT));
|
||||
|
||||
return requestFactory.fromSettingsResponse(response, getSettingsRequest.indices()[0]);
|
||||
return ResponseConverter.fromSettingsResponse(response, getSettingsRequest.indices()[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -234,7 +233,7 @@ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements I
|
||||
GetIndexTemplatesRequest getIndexTemplatesRequest = requestFactory.getIndexTemplatesRequest(getTemplateRequest);
|
||||
GetIndexTemplatesResponse getIndexTemplatesResponse = restTemplate
|
||||
.execute(client -> client.indices().getIndexTemplate(getIndexTemplatesRequest, RequestOptions.DEFAULT));
|
||||
return requestFactory.getTemplateData(getIndexTemplatesResponse, getTemplateRequest.getTemplateName());
|
||||
return ResponseConverter.getTemplateData(getIndexTemplatesResponse, getTemplateRequest.getTemplateName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -260,17 +259,15 @@ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements I
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IndexInformation> getInformation() {
|
||||
IndexCoordinates indexCoordinates = getIndexCoordinates();
|
||||
GetIndexRequest request = requestFactory.getIndexRequest(indexCoordinates);
|
||||
public List<IndexInformation> getInformation(IndexCoordinates index) {
|
||||
|
||||
return restTemplate.execute(
|
||||
client -> {
|
||||
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
|
||||
return responseConverter.indexInformationCollection(getIndexResponse);
|
||||
});
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
GetIndexRequest request = requestFactory.getIndexRequest(index);
|
||||
return restTemplate.execute(client -> {
|
||||
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
|
||||
return ResponseConverter.getIndexInformations(getIndexResponse);
|
||||
});
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,9 @@ package org.springframework.data.elasticsearch.core;
|
||||
import static org.elasticsearch.client.Requests.*;
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -52,14 +55,9 @@ import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author Peter-Josef Meisch
|
||||
* @author George Popides
|
||||
@ -74,7 +72,6 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
private final RequestFactory requestFactory;
|
||||
private final ReactiveElasticsearchOperations operations;
|
||||
private final ElasticsearchConverter converter;
|
||||
private final ResponseConverter responseConverter;
|
||||
|
||||
public DefaultReactiveIndexOperations(ReactiveElasticsearchOperations operations, IndexCoordinates index) {
|
||||
|
||||
@ -84,7 +81,6 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
this.operations = operations;
|
||||
this.converter = operations.getElasticsearchConverter();
|
||||
this.requestFactory = new RequestFactory(operations.getElasticsearchConverter());
|
||||
this.responseConverter = new ResponseConverter();
|
||||
this.boundClass = null;
|
||||
this.boundIndex = index;
|
||||
}
|
||||
@ -97,7 +93,6 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
this.operations = operations;
|
||||
this.converter = operations.getElasticsearchConverter();
|
||||
this.requestFactory = new RequestFactory(operations.getElasticsearchConverter());
|
||||
this.responseConverter = new ResponseConverter();
|
||||
this.boundClass = clazz;
|
||||
this.boundIndex = getIndexCoordinatesFor(clazz);
|
||||
}
|
||||
@ -223,7 +218,7 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
GetSettingsRequest request = requestFactory.getSettingsRequest(indexName, includeDefaults);
|
||||
|
||||
return Mono.from(operations.executeWithIndicesClient(client -> client.getSettings(request)))
|
||||
.map(getSettingsResponse -> requestFactory.fromSettingsResponse(getSettingsResponse, indexName));
|
||||
.map(getSettingsResponse -> ResponseConverter.fromSettingsResponse(getSettingsResponse, indexName));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -250,7 +245,7 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
|
||||
GetAliasesRequest getAliasesRequest = requestFactory.getAliasesRequest(aliasNames, indexNames);
|
||||
return Mono.from(operations.executeWithIndicesClient(client -> client.getAliases(getAliasesRequest)))
|
||||
.map(GetAliasesResponse::getAliases).map(responseConverter::convertAliasesResponse);
|
||||
.map(GetAliasesResponse::getAliases).map(ResponseConverter::aliasDatas);
|
||||
}
|
||||
// endregion
|
||||
|
||||
@ -273,7 +268,8 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
return Mono.from(operations.executeWithIndicesClient(client -> client.getTemplate(getIndexTemplatesRequest)))
|
||||
.flatMap(response -> {
|
||||
if (response != null) {
|
||||
TemplateData templateData = requestFactory.getTemplateData(response, getTemplateRequest.getTemplateName());
|
||||
TemplateData templateData = ResponseConverter.getTemplateData(response,
|
||||
getTemplateRequest.getTemplateName());
|
||||
if (templateData != null) {
|
||||
return Mono.just(templateData);
|
||||
}
|
||||
@ -311,12 +307,15 @@ class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<IndexInformation> getInformation() {
|
||||
org.elasticsearch.client.indices.GetIndexRequest getIndexRequest = requestFactory.getIndexRequest(getIndexCoordinates());
|
||||
public Flux<IndexInformation> getInformation(IndexCoordinates index) {
|
||||
|
||||
return Mono.from(operations.executeWithIndicesClient(client ->
|
||||
client.getIndex(HttpHeaders.EMPTY, getIndexRequest).map(responseConverter::indexInformationCollection)))
|
||||
.flatMapMany(Flux::fromIterable);
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
org.elasticsearch.client.indices.GetIndexRequest getIndexRequest = requestFactory.getIndexRequest(index);
|
||||
return Mono
|
||||
.from(operations.executeWithIndicesClient(
|
||||
client -> client.getIndex(getIndexRequest).map(ResponseConverter::getIndexInformations)))
|
||||
.flatMapMany(Flux::fromIterable);
|
||||
}
|
||||
|
||||
private IndexCoordinates getIndexCoordinatesFor(Class<?> clazz) {
|
||||
|
@ -59,7 +59,6 @@ import org.springframework.data.elasticsearch.core.index.GetTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.core.query.AliasQuery;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@ -181,7 +180,7 @@ class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations imp
|
||||
|
||||
Map<String, Set<AliasMetadata>> aliasesResponse = new LinkedHashMap<>();
|
||||
aliases.keysIt().forEachRemaining(index -> aliasesResponse.put(index, new HashSet<>(aliases.get(index))));
|
||||
return responseConverter.convertAliasesResponse(aliasesResponse);
|
||||
return ResponseConverter.aliasDatas(aliasesResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -203,7 +202,7 @@ class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations imp
|
||||
.getSettings(getSettingsRequest) //
|
||||
.actionGet();
|
||||
|
||||
return requestFactory.fromSettingsResponse(response, index.getIndexName());
|
||||
return ResponseConverter.fromSettingsResponse(response, index.getIndexName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -247,7 +246,7 @@ class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations imp
|
||||
Iterator<String> keysItAliases = aliasesResponse.keysIt();
|
||||
while (keysItAliases.hasNext()) {
|
||||
String key = keysItAliases.next();
|
||||
aliases.put(key, responseConverter.convertAliasMetadata(aliasesResponse.get(key)));
|
||||
aliases.put(key, ResponseConverter.toAliasData(aliasesResponse.get(key)));
|
||||
}
|
||||
|
||||
Map<String, String> mappingsDoc = new LinkedHashMap<>();
|
||||
@ -302,14 +301,13 @@ class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IndexInformation> getInformation() {
|
||||
public List<IndexInformation> getInformation(IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
GetIndexRequest getIndexRequest = new GetIndexRequest();
|
||||
IndexCoordinates index = getIndexCoordinates();
|
||||
|
||||
getIndexRequest.indices(index.getIndexNames());
|
||||
|
||||
GetIndexResponse getIndexResponse = client.admin().indices().getIndex(getIndexRequest).actionGet();
|
||||
|
||||
return responseConverter.indexInformationCollection(getIndexResponse);
|
||||
return ResponseConverter.getIndexInformations(getIndexResponse);
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.elasticsearch.core.mapping;
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -26,53 +26,44 @@ import org.springframework.lang.Nullable;
|
||||
* Immutable object that holds information(name, settings, mappings, aliases) about an Index
|
||||
*
|
||||
* @author George Popides
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.2
|
||||
*/
|
||||
public class IndexInformation {
|
||||
private final String name;
|
||||
@Nullable
|
||||
private final Document settings;
|
||||
@Nullable
|
||||
private final Document mappings;
|
||||
@Nullable
|
||||
private final List<AliasData> aliases;
|
||||
@Nullable private final Document settings;
|
||||
@Nullable private final Document mapping;
|
||||
@Nullable private final List<AliasData> aliases;
|
||||
|
||||
|
||||
public static IndexInformation create(
|
||||
String indexName,
|
||||
@Nullable Document settings,
|
||||
@Nullable Document mappings,
|
||||
@Nullable List<AliasData> aliases
|
||||
) {
|
||||
return new IndexInformation(indexName, settings, mappings, aliases);
|
||||
public static IndexInformation of(String name, @Nullable Document settings, @Nullable Document mapping,
|
||||
@Nullable List<AliasData> aliases) {
|
||||
return new IndexInformation(name, settings, mapping, aliases);
|
||||
}
|
||||
|
||||
private IndexInformation(
|
||||
String indexName,
|
||||
@Nullable Document settings,
|
||||
@Nullable Document mappings,
|
||||
@Nullable List<AliasData> aliases
|
||||
) {
|
||||
this.name = indexName;
|
||||
private IndexInformation(String name, @Nullable Document settings, @Nullable Document mapping,
|
||||
@Nullable List<AliasData> aliases) {
|
||||
this.name = name;
|
||||
this.settings = settings;
|
||||
this.mappings = mappings;
|
||||
this.mapping = mapping;
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
public Document getMappings() {
|
||||
return mappings;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Document getMapping() {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Document getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public List<AliasData> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
}
|
@ -29,7 +29,6 @@ import org.springframework.data.elasticsearch.core.index.GetTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.core.query.AliasQuery;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@ -39,7 +38,7 @@ import org.springframework.lang.Nullable;
|
||||
* <br/>
|
||||
* IndexOperations are bound to an entity class or an IndexCoordinate by
|
||||
* {@link ElasticsearchOperations#indexOps(IndexCoordinates)} or {@link ElasticsearchOperations#indexOps(Class)}
|
||||
*
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Sascha Woo
|
||||
* @author George Popides
|
||||
@ -101,7 +100,7 @@ public interface IndexOperations {
|
||||
|
||||
/**
|
||||
* Writes the mapping to the index for the class this IndexOperations is bound to.
|
||||
*
|
||||
*
|
||||
* @return {@literal true} if the mapping could be stored
|
||||
* @since 4.1
|
||||
*/
|
||||
@ -111,7 +110,7 @@ public interface IndexOperations {
|
||||
|
||||
/**
|
||||
* writes a mapping to the index
|
||||
*
|
||||
*
|
||||
* @param mapping the Document with the mapping definitions
|
||||
* @return {@literal true} if the mapping could be stored
|
||||
*/
|
||||
@ -119,7 +118,7 @@ public interface IndexOperations {
|
||||
|
||||
/**
|
||||
* Creates the index mapping for the given class and writes it to the index.
|
||||
*
|
||||
*
|
||||
* @param clazz the clazz to create a mapping for
|
||||
* @return {@literal true} if the mapping could be stored
|
||||
* @since 4.1
|
||||
@ -127,12 +126,20 @@ public interface IndexOperations {
|
||||
default boolean putMapping(Class<?> clazz) {
|
||||
return putMapping(createMapping(clazz));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mapping for an index defined by a class.
|
||||
*
|
||||
* @return the mapping
|
||||
*/
|
||||
Map<String, Object> getMapping();
|
||||
|
||||
// endregion
|
||||
|
||||
// region settings
|
||||
/**
|
||||
* Creates the index settings for the entity this IndexOperations is bound to.
|
||||
*
|
||||
*
|
||||
* @return a settings document.
|
||||
* @since 4.1
|
||||
*/
|
||||
@ -147,13 +154,6 @@ public interface IndexOperations {
|
||||
*/
|
||||
Document createSettings(Class<?> clazz);
|
||||
|
||||
/**
|
||||
* Get mapping for an index defined by a class.
|
||||
*
|
||||
* @return the mapping
|
||||
*/
|
||||
Map<String, Object> getMapping();
|
||||
|
||||
/**
|
||||
* Get the index settings.
|
||||
*
|
||||
@ -202,7 +202,7 @@ public interface IndexOperations {
|
||||
|
||||
/**
|
||||
* Executes the given {@link AliasActions}.
|
||||
*
|
||||
*
|
||||
* @param aliasActions the actions to execute
|
||||
* @return if the operation is acknowledged by Elasticsearch
|
||||
* @since 4.1
|
||||
@ -211,7 +211,7 @@ public interface IndexOperations {
|
||||
|
||||
/**
|
||||
* gets information about aliases
|
||||
*
|
||||
*
|
||||
* @param aliasNames alias names, must not be {@literal null}
|
||||
* @return a {@link Map} from index names to {@link AliasData} for that index
|
||||
* @since 4.1
|
||||
@ -220,7 +220,7 @@ public interface IndexOperations {
|
||||
|
||||
/**
|
||||
* gets information about aliases
|
||||
*
|
||||
*
|
||||
* @param indexNames index names, must not be {@literal null}
|
||||
* @return a {@link Map} from index names to {@link AliasData} for that index
|
||||
* @since 4.1
|
||||
@ -232,7 +232,7 @@ public interface IndexOperations {
|
||||
/**
|
||||
* Creates an index template using the legacy Elasticsearch interface (@see
|
||||
* https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates-v1.html).
|
||||
*
|
||||
*
|
||||
* @param putTemplateRequest template request parameters
|
||||
* @return true if successful
|
||||
* @since 4.1
|
||||
@ -309,6 +309,27 @@ public interface IndexOperations {
|
||||
|
||||
// endregion
|
||||
|
||||
//region index information
|
||||
/**
|
||||
* Gets the {@link IndexInformation} for the indices defined by {@link #getIndexCoordinates()}.
|
||||
*
|
||||
* @return a list of {@link IndexInformation}
|
||||
* @since 4.2
|
||||
*/
|
||||
default List<IndexInformation> getInformation() {
|
||||
return getInformation(getIndexCoordinates());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link IndexInformation} for the indices defined by #index.
|
||||
*
|
||||
* @param index defines the index names to get the information for
|
||||
* @return a list of {@link IndexInformation}
|
||||
* @since 4.2
|
||||
*/
|
||||
List<IndexInformation> getInformation(IndexCoordinates index);
|
||||
//endregion
|
||||
|
||||
// region helper functions
|
||||
/**
|
||||
* get the current {@link IndexCoordinates}. These may change over time when the entity class has a SpEL constructed
|
||||
@ -319,13 +340,5 @@ public interface IndexOperations {
|
||||
*/
|
||||
IndexCoordinates getIndexCoordinates();
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return a list of {@link IndexInformation}
|
||||
* @since 4.2
|
||||
*/
|
||||
List<IndexInformation> getInformation();
|
||||
|
||||
// endregion
|
||||
}
|
||||
|
@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -27,10 +30,6 @@ import org.springframework.data.elasticsearch.core.index.GetTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Interface defining operations on indexes for the reactive stack.
|
||||
@ -69,14 +68,14 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
/**
|
||||
* checks if an index exists
|
||||
*
|
||||
*
|
||||
* @return a {@link Mono} with the result of exist check
|
||||
*/
|
||||
Mono<Boolean> exists();
|
||||
|
||||
/**
|
||||
* Refresh the index(es) this IndexOperations is bound to
|
||||
*
|
||||
*
|
||||
* @return a {@link Mono} signalling operation completion.
|
||||
*/
|
||||
Mono<Void> refresh();
|
||||
@ -100,7 +99,7 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
/**
|
||||
* Writes the mapping to the index for the class this IndexOperations is bound to.
|
||||
*
|
||||
*
|
||||
* @return {@literal true} if the mapping could be stored
|
||||
*/
|
||||
default Mono<Boolean> putMapping() {
|
||||
@ -117,7 +116,7 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
/**
|
||||
* Creates the index mapping for the given class and writes it to the index.
|
||||
*
|
||||
*
|
||||
* @param clazz the clazz to create a mapping for
|
||||
* @return {@literal true} if the mapping could be stored
|
||||
*/
|
||||
@ -153,7 +152,7 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
/**
|
||||
* get the settings for the index
|
||||
*
|
||||
*
|
||||
* @return a {@link Mono} with a {@link Document} containing the index settings
|
||||
*/
|
||||
default Mono<Document> getSettings() {
|
||||
@ -181,7 +180,7 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
/**
|
||||
* gets information about aliases
|
||||
*
|
||||
*
|
||||
* @param aliasNames alias names, must not be {@literal null}
|
||||
* @return a {@link Mono} of {@link Map} from index names to {@link AliasData} for that index
|
||||
* @since 4.1
|
||||
@ -190,7 +189,7 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
/**
|
||||
* gets information about aliases
|
||||
*
|
||||
*
|
||||
* @param indexNames alias names, must not be {@literal null}
|
||||
* @return a {@link Mono} of {@link Map} from index names to {@link AliasData} for that index
|
||||
* @since 4.1
|
||||
@ -277,6 +276,27 @@ public interface ReactiveIndexOperations {
|
||||
|
||||
// endregion
|
||||
|
||||
// region index information
|
||||
/**
|
||||
* Gets the {@link IndexInformation} for the indices defined by {@link #getIndexCoordinates()}.
|
||||
*
|
||||
* @return a flux of {@link IndexInformation}
|
||||
* @since 4.2
|
||||
*/
|
||||
default Flux<IndexInformation> getInformation() {
|
||||
return getInformation(getIndexCoordinates());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link IndexInformation} for the indices defined by {@link #getIndexCoordinates()}.
|
||||
*
|
||||
* @return a flux of {@link IndexInformation}
|
||||
* @since 4.2
|
||||
*/
|
||||
Flux<IndexInformation> getInformation(IndexCoordinates index);
|
||||
|
||||
// endregion
|
||||
|
||||
// region helper functions
|
||||
/**
|
||||
* get the current {@link IndexCoordinates}. These may change over time when the entity class has a SpEL constructed
|
||||
@ -287,11 +307,5 @@ public interface ReactiveIndexOperations {
|
||||
*/
|
||||
IndexCoordinates getIndexCoordinates();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return a flux of {@link IndexInformation}
|
||||
* @since 4.2
|
||||
*/
|
||||
Flux<IndexInformation> getInformation();
|
||||
// endregion
|
||||
}
|
||||
|
@ -22,8 +22,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@ -62,14 +60,10 @@ import org.elasticsearch.client.Requests;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
|
||||
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
|
||||
import org.elasticsearch.client.indices.GetMappingsRequest;
|
||||
import org.elasticsearch.client.indices.IndexTemplateMetadata;
|
||||
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
|
||||
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
|
||||
import org.elasticsearch.client.indices.PutMappingRequest;
|
||||
import org.elasticsearch.cluster.metadata.AliasMetadata;
|
||||
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
||||
import org.elasticsearch.common.geo.GeoDistance;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.DistanceUnit;
|
||||
@ -104,12 +98,10 @@ import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.index.AliasAction;
|
||||
import org.springframework.data.elasticsearch.core.index.AliasActionParameters;
|
||||
import org.springframework.data.elasticsearch.core.index.AliasActions;
|
||||
import org.springframework.data.elasticsearch.core.index.AliasData;
|
||||
import org.springframework.data.elasticsearch.core.index.DeleteTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.GetTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
@ -499,7 +491,6 @@ class RequestFactory {
|
||||
return new org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest().indices(indexNames);
|
||||
}
|
||||
|
||||
|
||||
public PutIndexTemplateRequest putIndexTemplateRequest(PutTemplateRequest putTemplateRequest) {
|
||||
|
||||
PutIndexTemplateRequest request = new PutIndexTemplateRequest(putTemplateRequest.getName())
|
||||
@ -643,38 +634,6 @@ class RequestFactory {
|
||||
return new GetIndexTemplatesRequest(getTemplateRequest.getTemplateName());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public TemplateData getTemplateData(GetIndexTemplatesResponse getIndexTemplatesResponse, String templateName) {
|
||||
for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplatesResponse.getIndexTemplates()) {
|
||||
|
||||
if (indexTemplateMetadata.name().equals(templateName)) {
|
||||
|
||||
Document settings = Document.create();
|
||||
Settings templateSettings = indexTemplateMetadata.settings();
|
||||
templateSettings.keySet().forEach(key -> settings.put(key, templateSettings.get(key)));
|
||||
|
||||
Map<String, AliasData> aliases = new LinkedHashMap<>();
|
||||
|
||||
ImmutableOpenMap<String, AliasMetadata> aliasesResponse = indexTemplateMetadata.aliases();
|
||||
Iterator<String> keysIt = aliasesResponse.keysIt();
|
||||
while (keysIt.hasNext()) {
|
||||
String key = keysIt.next();
|
||||
aliases.put(key, ResponseConverter.convertAliasMetadata(aliasesResponse.get(key)));
|
||||
}
|
||||
TemplateData templateData = TemplateData.builder()
|
||||
.withIndexPatterns(indexTemplateMetadata.patterns().toArray(new String[0])) //
|
||||
.withSettings(settings) //
|
||||
.withMapping(Document.from(indexTemplateMetadata.mappings().getSourceAsMap())) //
|
||||
.withAliases(aliases) //
|
||||
.withOrder(indexTemplateMetadata.order()) //
|
||||
.withVersion(indexTemplateMetadata.version()).build();
|
||||
|
||||
return templateData;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest getIndexTemplatesRequest(
|
||||
Client client, GetTemplateRequest getTemplateRequest) {
|
||||
return new org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest(
|
||||
@ -1803,39 +1762,5 @@ class RequestFactory {
|
||||
return null;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region response stuff
|
||||
|
||||
/**
|
||||
* extract the index settings information for a given index
|
||||
*
|
||||
* @param response the Elasticsearch response
|
||||
* @param indexName the index name
|
||||
* @return settings as {@link Document}
|
||||
*/
|
||||
public Document fromSettingsResponse(GetSettingsResponse response, String indexName) {
|
||||
|
||||
Document settings = Document.create();
|
||||
|
||||
if (!response.getIndexToDefaultSettings().isEmpty()) {
|
||||
Settings defaultSettings = response.getIndexToDefaultSettings().get(indexName);
|
||||
for (String key : defaultSettings.keySet()) {
|
||||
settings.put(key, defaultSettings.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
if (!response.getIndexToSettings().isEmpty()) {
|
||||
Settings customSettings = response.getIndexToSettings().get(indexName);
|
||||
for (String key : customSettings.keySet()) {
|
||||
settings.put(key, customSettings.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// endregion
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@ -25,87 +26,87 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
|
||||
import org.elasticsearch.client.indices.GetIndexResponse;
|
||||
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
|
||||
import org.elasticsearch.client.indices.IndexTemplateMetadata;
|
||||
import org.elasticsearch.cluster.metadata.AliasMetadata;
|
||||
import org.elasticsearch.cluster.metadata.MappingMetadata;
|
||||
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
||||
import org.elasticsearch.common.compress.CompressedXContent;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.index.AliasData;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.core.index.TemplateData;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Factory class to elasticsearch responses to different type of data classes.
|
||||
* Factory class to convert elasticsearch responses to different type of data classes.
|
||||
*
|
||||
* @author George Popides
|
||||
* @author Peter-Josef Meisch
|
||||
* @since 4.2
|
||||
*/
|
||||
public class ResponseConverter {
|
||||
public ResponseConverter() {
|
||||
}
|
||||
private ResponseConverter() {}
|
||||
|
||||
// region alias
|
||||
|
||||
public static AliasData convertAliasMetadata(AliasMetadata aliasMetaData) {
|
||||
Document filter = null;
|
||||
CompressedXContent aliasMetaDataFilter = aliasMetaData.getFilter();
|
||||
if (aliasMetaDataFilter != null) {
|
||||
filter = Document.parse(aliasMetaDataFilter.string());
|
||||
}
|
||||
AliasData aliasData = AliasData.of(aliasMetaData.alias(), filter, aliasMetaData.indexRouting(),
|
||||
aliasMetaData.getSearchRouting(), aliasMetaData.writeIndex(), aliasMetaData.isHidden());
|
||||
return aliasData;
|
||||
}
|
||||
|
||||
public List<IndexInformation> indexInformationCollection(GetIndexResponse getIndexResponse) {
|
||||
List<IndexInformation> indexInformationList = new ArrayList<>();
|
||||
|
||||
for (String indexName : getIndexResponse.getIndices()) {
|
||||
Document settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
List<AliasData> aliases = mappingsFromIndexResponse(getIndexResponse, indexName);
|
||||
|
||||
|
||||
indexInformationList.add(IndexInformation.create(indexName, settings, mappings, aliases));
|
||||
}
|
||||
|
||||
return indexInformationList;
|
||||
}
|
||||
|
||||
public List<IndexInformation> indexInformationCollection(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse) {
|
||||
List<IndexInformation> indexInformationList = new ArrayList<>();
|
||||
|
||||
for (String indexName : getIndexResponse.getIndices()) {
|
||||
Document settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
List<AliasData> aliases = mappingsFromIndexResponse(getIndexResponse, indexName);
|
||||
|
||||
indexInformationList.add(IndexInformation.create(indexName, settings, mappings, aliases));
|
||||
}
|
||||
|
||||
return indexInformationList;
|
||||
}
|
||||
|
||||
public Map<String, Set<AliasData>> convertAliasesResponse(Map<String, Set<AliasMetadata>> aliasesResponse) {
|
||||
public static Map<String, Set<AliasData>> aliasDatas(Map<String, Set<AliasMetadata>> aliasesMetadatas) {
|
||||
Map<String, Set<AliasData>> converted = new LinkedHashMap<>();
|
||||
aliasesResponse.forEach((index, aliasMetaDataSet) -> {
|
||||
aliasesMetadatas.forEach((index, aliasMetaDataSet) -> {
|
||||
Set<AliasData> aliasDataSet = new LinkedHashSet<>();
|
||||
aliasMetaDataSet.forEach(aliasMetaData -> aliasDataSet.add(convertAliasMetadata(aliasMetaData)));
|
||||
aliasMetaDataSet.forEach(aliasMetaData -> aliasDataSet.add(toAliasData(aliasMetaData)));
|
||||
converted.put(index, aliasDataSet);
|
||||
});
|
||||
return converted;
|
||||
}
|
||||
|
||||
public static AliasData toAliasData(AliasMetadata aliasMetaData) {
|
||||
Document filter = null;
|
||||
CompressedXContent aliasMetaDataFilter = aliasMetaData.getFilter();
|
||||
|
||||
// end region
|
||||
if (aliasMetaDataFilter != null) {
|
||||
filter = Document.parse(aliasMetaDataFilter.string());
|
||||
}
|
||||
return AliasData.of(aliasMetaData.alias(), filter, aliasMetaData.indexRouting(), aliasMetaData.getSearchRouting(),
|
||||
aliasMetaData.writeIndex(), aliasMetaData.isHidden());
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region index informations
|
||||
/**
|
||||
* get the index informations from a {@link GetIndexResponse}
|
||||
*
|
||||
* @param getIndexResponse the index response, must not be {@literal null}
|
||||
* @return list of {@link IndexInformation}s for the different indices
|
||||
*/
|
||||
public static List<IndexInformation> getIndexInformations(GetIndexResponse getIndexResponse) {
|
||||
|
||||
Assert.notNull(getIndexResponse, "getIndexResponse must not be null");
|
||||
|
||||
List<IndexInformation> indexInformationList = new ArrayList<>();
|
||||
|
||||
for (String indexName : getIndexResponse.getIndices()) {
|
||||
Document settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
List<AliasData> aliases = aliasDataFromIndexResponse(getIndexResponse, indexName);
|
||||
|
||||
indexInformationList.add(IndexInformation.of(indexName, settings, mappings, aliases));
|
||||
}
|
||||
|
||||
return indexInformationList;
|
||||
}
|
||||
|
||||
/**
|
||||
* extract the index settings information from a given index
|
||||
*
|
||||
* @param getIndexResponse the elastic GetIndexResponse
|
||||
* @param indexName the index name
|
||||
* @return a document that represents {@link Settings}
|
||||
*/
|
||||
private Document settingsFromGetIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
|
||||
private static Document settingsFromGetIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
|
||||
Document document = Document.create();
|
||||
|
||||
Settings indexSettings = getIndexResponse.getSettings().get(indexName);
|
||||
@ -121,11 +122,12 @@ public class ResponseConverter {
|
||||
|
||||
/**
|
||||
* extract the mappings information from a given index
|
||||
*
|
||||
* @param getIndexResponse the elastic GetIndexResponse
|
||||
* @param indexName the index name
|
||||
* @return a document that represents {@link MappingMetadata}
|
||||
*/
|
||||
private Document mappingsFromGetIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
|
||||
private static Document mappingsFromGetIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
|
||||
Document document = Document.create();
|
||||
|
||||
if (getIndexResponse.getMappings().containsKey(indexName)) {
|
||||
@ -136,7 +138,40 @@ public class ResponseConverter {
|
||||
return document;
|
||||
}
|
||||
|
||||
private Document settingsFromGetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
|
||||
private static List<AliasData> aliasDataFromIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
|
||||
List<AliasData> aliases = Collections.emptyList();
|
||||
|
||||
if (getIndexResponse.getAliases().get(indexName) != null) {
|
||||
aliases = getIndexResponse.getAliases().get(indexName).stream().map(ResponseConverter::toAliasData)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return aliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the index informations from a {@link org.elasticsearch.action.admin.indices.get.GetIndexResponse} (transport
|
||||
* client)
|
||||
*
|
||||
* @param getIndexResponse the index response, must not be {@literal null}
|
||||
* @return list of {@link IndexInformation}s for the different indices
|
||||
*/
|
||||
public static List<IndexInformation> getIndexInformations(
|
||||
org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse) {
|
||||
List<IndexInformation> indexInformationList = new ArrayList<>();
|
||||
|
||||
for (String indexName : getIndexResponse.getIndices()) {
|
||||
Document settings = settingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
Document mappings = mappingsFromGetIndexResponse(getIndexResponse, indexName);
|
||||
List<AliasData> aliases = aliasDataFromIndexResponse(getIndexResponse, indexName);
|
||||
|
||||
indexInformationList.add(IndexInformation.of(indexName, settings, mappings, aliases));
|
||||
}
|
||||
|
||||
return indexInformationList;
|
||||
}
|
||||
|
||||
private static Document settingsFromGetIndexResponse(
|
||||
org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
|
||||
Document document = Document.create();
|
||||
|
||||
if (getIndexResponse.getSettings().containsKey(indexName)) {
|
||||
@ -150,11 +185,12 @@ public class ResponseConverter {
|
||||
return document;
|
||||
}
|
||||
|
||||
private Document mappingsFromGetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
|
||||
private static Document mappingsFromGetIndexResponse(
|
||||
org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
|
||||
Document document = Document.create();
|
||||
|
||||
boolean responseHasMappings = getIndexResponse.getMappings().containsKey(indexName) &&
|
||||
(getIndexResponse.getMappings().get(indexName).get("_doc") != null);
|
||||
boolean responseHasMappings = getIndexResponse.getMappings().containsKey(indexName)
|
||||
&& (getIndexResponse.getMappings().get(indexName).get("_doc") != null);
|
||||
|
||||
if (responseHasMappings) {
|
||||
MappingMetadata mappings = getIndexResponse.getMappings().get(indexName).get("_doc");
|
||||
@ -164,32 +200,80 @@ public class ResponseConverter {
|
||||
return document;
|
||||
}
|
||||
|
||||
private List<AliasData> mappingsFromIndexResponse(GetIndexResponse getIndexResponse, String indexName) {
|
||||
private static List<AliasData> aliasDataFromIndexResponse(
|
||||
org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
|
||||
List<AliasData> aliases = Collections.emptyList();
|
||||
|
||||
if (getIndexResponse.getAliases().get(indexName) != null) {
|
||||
aliases = getIndexResponse
|
||||
.getAliases()
|
||||
.get(indexName)
|
||||
.stream()
|
||||
.map(ResponseConverter::convertAliasMetadata)
|
||||
aliases = getIndexResponse.getAliases().get(indexName).stream().map(ResponseConverter::toAliasData)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return aliases;
|
||||
}
|
||||
|
||||
private List<AliasData> mappingsFromIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse getIndexResponse, String indexName) {
|
||||
List<AliasData> aliases = Collections.emptyList();
|
||||
// endregion
|
||||
|
||||
if (getIndexResponse.getAliases().get(indexName) != null) {
|
||||
aliases = getIndexResponse
|
||||
.getAliases()
|
||||
.get(indexName)
|
||||
.stream()
|
||||
.map(ResponseConverter::convertAliasMetadata)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return aliases;
|
||||
}
|
||||
//region templates
|
||||
@Nullable
|
||||
public static TemplateData getTemplateData(GetIndexTemplatesResponse getIndexTemplatesResponse, String templateName) {
|
||||
for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplatesResponse.getIndexTemplates()) {
|
||||
|
||||
if (indexTemplateMetadata.name().equals(templateName)) {
|
||||
|
||||
Document settings = Document.create();
|
||||
Settings templateSettings = indexTemplateMetadata.settings();
|
||||
templateSettings.keySet().forEach(key -> settings.put(key, templateSettings.get(key)));
|
||||
|
||||
Map<String, AliasData> aliases = new LinkedHashMap<>();
|
||||
|
||||
ImmutableOpenMap<String, AliasMetadata> aliasesResponse = indexTemplateMetadata.aliases();
|
||||
Iterator<String> keysIt = aliasesResponse.keysIt();
|
||||
while (keysIt.hasNext()) {
|
||||
String key = keysIt.next();
|
||||
aliases.put(key, ResponseConverter.toAliasData(aliasesResponse.get(key)));
|
||||
}
|
||||
|
||||
return TemplateData.builder()
|
||||
.withIndexPatterns(indexTemplateMetadata.patterns().toArray(new String[0])) //
|
||||
.withSettings(settings) //
|
||||
.withMapping(Document.from(indexTemplateMetadata.mappings().getSourceAsMap())) //
|
||||
.withAliases(aliases) //
|
||||
.withOrder(indexTemplateMetadata.order()) //
|
||||
.withVersion(indexTemplateMetadata.version()).build();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
//endregion
|
||||
|
||||
//region settings
|
||||
/**
|
||||
* extract the index settings information for a given index
|
||||
*
|
||||
* @param response the Elasticsearch response
|
||||
* @param indexName the index name
|
||||
* @return settings as {@link Document}
|
||||
*/
|
||||
public static Document fromSettingsResponse(GetSettingsResponse response, String indexName) {
|
||||
|
||||
Document settings = Document.create();
|
||||
|
||||
if (!response.getIndexToDefaultSettings().isEmpty()) {
|
||||
Settings defaultSettings = response.getIndexToDefaultSettings().get(indexName);
|
||||
for (String key : defaultSettings.keySet()) {
|
||||
settings.put(key, defaultSettings.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
if (!response.getIndexToSettings().isEmpty()) {
|
||||
Settings customSettings = response.getIndexToSettings().get(indexName);
|
||||
for (String key : customSettings.keySet()) {
|
||||
settings.put(key, customSettings.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
//endregion
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,14 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.lang.Boolean;
|
||||
import java.lang.Long;
|
||||
import java.lang.Object;
|
||||
@ -75,14 +83,6 @@ import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearc
|
||||
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ReactiveElasticsearchTemplate}.
|
||||
*
|
||||
@ -641,8 +641,7 @@ public class ReactiveElasticsearchTemplateIntegrationTests {
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith(byQueryResponse -> {
|
||||
assertThat(byQueryResponse.getDeleted()).isEqualTo(0L);
|
||||
})
|
||||
.verifyComplete();
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAES-547
|
||||
@ -1079,44 +1078,41 @@ public class ReactiveElasticsearchTemplateIntegrationTests {
|
||||
@DisplayName("should not return explanation when not requested")
|
||||
void shouldNotReturnExplanationWhenNotRequested() {
|
||||
|
||||
ElasticsearchTemplateTests.SampleEntity entity = ElasticsearchTemplateTests.SampleEntity.builder().id("42").message("a message with text").build();
|
||||
ElasticsearchTemplateTests.SampleEntity entity = ElasticsearchTemplateTests.SampleEntity.builder().id("42")
|
||||
.message("a message with text").build();
|
||||
template.save(entity).as(StepVerifier::create).expectNextCount(1).verifyComplete();
|
||||
|
||||
Criteria criteria = new Criteria("message").contains("with");
|
||||
CriteriaQuery query = new CriteriaQuery(criteria);
|
||||
|
||||
template.search(query, ElasticsearchTemplateTests.SampleEntity.class)
|
||||
.as(StepVerifier::create)
|
||||
template.search(query, ElasticsearchTemplateTests.SampleEntity.class).as(StepVerifier::create)
|
||||
.consumeNextWith(searchHit -> {
|
||||
Explanation explanation = searchHit.getExplanation();
|
||||
assertThat(explanation).isNull();
|
||||
})
|
||||
.verifyComplete();
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // #725
|
||||
@DisplayName("should return explanation when requested")
|
||||
void shouldReturnExplanationWhenRequested() {
|
||||
|
||||
ElasticsearchTemplateTests.SampleEntity entity = ElasticsearchTemplateTests.SampleEntity.builder().id("42").message("a message with text").build();
|
||||
ElasticsearchTemplateTests.SampleEntity entity = ElasticsearchTemplateTests.SampleEntity.builder().id("42")
|
||||
.message("a message with text").build();
|
||||
template.save(entity).as(StepVerifier::create).expectNextCount(1).verifyComplete();
|
||||
|
||||
Criteria criteria = new Criteria("message").contains("with");
|
||||
CriteriaQuery query = new CriteriaQuery(criteria);
|
||||
query.setExplain(true);
|
||||
|
||||
template.search(query, ElasticsearchTemplateTests.SampleEntity.class)
|
||||
.as(StepVerifier::create)
|
||||
template.search(query, ElasticsearchTemplateTests.SampleEntity.class).as(StepVerifier::create)
|
||||
.consumeNextWith(searchHit -> {
|
||||
Explanation explanation = searchHit.getExplanation();
|
||||
assertThat(explanation).isNotNull();
|
||||
})
|
||||
.verifyComplete();
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
|
||||
@Test // #1646
|
||||
@DisplayName("should return a list of info for specific index using reactive template")
|
||||
@DisplayName("should return a list of info for specific index")
|
||||
void shouldReturnInformationListOfAllIndices() {
|
||||
String indexName = "test-index-reactive-information-list";
|
||||
String aliasName = "testindexinformationindex";
|
||||
@ -1125,42 +1121,33 @@ public class ReactiveElasticsearchTemplateIntegrationTests {
|
||||
indexOps.create().block();
|
||||
indexOps.putMapping().block();
|
||||
|
||||
AliasActionParameters parameters = AliasActionParameters.builder()
|
||||
.withAliases(aliasName)
|
||||
.withIndices(indexName)
|
||||
.withIsHidden(false)
|
||||
.withIsWriteIndex(false)
|
||||
.withRouting("indexrouting")
|
||||
.withSearchRouting("searchrouting")
|
||||
AliasActionParameters parameters = AliasActionParameters.builder().withAliases(aliasName).withIndices(indexName)
|
||||
.withIsHidden(false).withIsWriteIndex(false).withRouting("indexrouting").withSearchRouting("searchrouting")
|
||||
.build();
|
||||
indexOps.alias(new AliasActions(new AliasAction.Add(parameters))).block();
|
||||
|
||||
indexOps
|
||||
.getInformation()
|
||||
.as(StepVerifier::create)
|
||||
.consumeNextWith(indexInformation -> {
|
||||
assertThat(indexInformation.getName()).isEqualTo(indexName);
|
||||
assertThat(indexInformation.getSettings().get("index.number_of_shards")).isEqualTo("1");
|
||||
assertThat(indexInformation.getSettings().get("index.number_of_replicas")).isEqualTo("0");
|
||||
assertThat(indexInformation.getSettings().get("index.analysis.analyzer.emailAnalyzer.type")).isEqualTo("custom");
|
||||
assertThat(indexInformation.getAliases()).hasSize(1);
|
||||
indexOps.getInformation().as(StepVerifier::create).consumeNextWith(indexInformation -> {
|
||||
assertThat(indexInformation.getName()).isEqualTo(indexName);
|
||||
assertThat(indexInformation.getSettings().get("index.number_of_shards")).isEqualTo("1");
|
||||
assertThat(indexInformation.getSettings().get("index.number_of_replicas")).isEqualTo("0");
|
||||
assertThat(indexInformation.getSettings().get("index.analysis.analyzer.emailAnalyzer.type")).isEqualTo("custom");
|
||||
assertThat(indexInformation.getAliases()).hasSize(1);
|
||||
|
||||
AliasData aliasData = indexInformation.getAliases().get(0);
|
||||
AliasData aliasData = indexInformation.getAliases().get(0);
|
||||
|
||||
assertThat(aliasData.getAlias()).isEqualTo(aliasName);
|
||||
assertThat(aliasData.isHidden()).isEqualTo(false);
|
||||
assertThat(aliasData.isWriteIndex()).isEqualTo(false);
|
||||
assertThat(aliasData.getIndexRouting()).isEqualTo("indexrouting");
|
||||
assertThat(aliasData.getSearchRouting()).isEqualTo("searchrouting");
|
||||
assertThat(aliasData.getAlias()).isEqualTo(aliasName);
|
||||
assertThat(aliasData.isHidden()).isEqualTo(false);
|
||||
assertThat(aliasData.isWriteIndex()).isEqualTo(false);
|
||||
assertThat(aliasData.getIndexRouting()).isEqualTo("indexrouting");
|
||||
assertThat(aliasData.getSearchRouting()).isEqualTo("searchrouting");
|
||||
|
||||
String expectedMappings = "{\"properties\":{\"email\":{\"type\":\"text\",\"analyzer\":\"emailAnalyzer\"}}}";
|
||||
try {
|
||||
JSONAssert.assertEquals(expectedMappings, indexInformation.getMappings().toJson(), false);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
})
|
||||
.verifyComplete();
|
||||
String expectedMappings = "{\"properties\":{\"email\":{\"type\":\"text\",\"analyzer\":\"emailAnalyzer\"}}}";
|
||||
try {
|
||||
JSONAssert.assertEquals(expectedMappings, indexInformation.getMapping().toJson(), false);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -1266,8 +1253,7 @@ public class ReactiveElasticsearchTemplateIntegrationTests {
|
||||
@Setting(settingPath = "settings/test-settings.json")
|
||||
@Mapping(mappingPath = "mappings/test-mappings.json")
|
||||
private static class EntityWithSettingsAndMappingsReactive {
|
||||
@Id
|
||||
String id;
|
||||
@Id String id;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
@ -17,6 +17,8 @@ package org.springframework.data.elasticsearch.core.index;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -30,22 +32,22 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Mapping;
|
||||
import org.springframework.data.elasticsearch.annotations.Setting;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
|
||||
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
|
||||
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author George Popides
|
||||
*/
|
||||
@SpringIntegrationTest
|
||||
@ContextConfiguration(classes = { ElasticsearchRestTemplateConfiguration.class })
|
||||
public class IndexOperationTests {
|
||||
@Autowired
|
||||
protected ElasticsearchOperations operations;
|
||||
public class IndexOperationIntegrationTests {
|
||||
|
||||
public static final String INDEX_NAME = "test-index-information-list";
|
||||
|
||||
@Autowired protected ElasticsearchOperations operations;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@ -58,18 +60,12 @@ public class IndexOperationTests {
|
||||
IndexOperations indexOps = operations.indexOps(EntityWithSettingsAndMappings.class);
|
||||
|
||||
String aliasName = "testindexinformationindex";
|
||||
String indexName = "test-index-information-list";
|
||||
|
||||
indexOps.create();
|
||||
indexOps.putMapping();
|
||||
|
||||
AliasActionParameters parameters = AliasActionParameters.builder()
|
||||
.withAliases(aliasName)
|
||||
.withIndices(indexName)
|
||||
.withIsHidden(false)
|
||||
.withIsWriteIndex(false)
|
||||
.withRouting("indexrouting")
|
||||
.withSearchRouting("searchrouting")
|
||||
AliasActionParameters parameters = AliasActionParameters.builder().withAliases(aliasName).withIndices(INDEX_NAME)
|
||||
.withIsHidden(false).withIsWriteIndex(false).withRouting("indexrouting").withSearchRouting("searchrouting")
|
||||
.build();
|
||||
indexOps.alias(new AliasActions(new AliasAction.Add(parameters)));
|
||||
|
||||
@ -78,7 +74,7 @@ public class IndexOperationTests {
|
||||
IndexInformation indexInformation = indexInformationList.get(0);
|
||||
|
||||
assertThat(indexInformationList.size()).isEqualTo(1);
|
||||
assertThat(indexInformation.getName()).isEqualTo(indexName);
|
||||
assertThat(indexInformation.getName()).isEqualTo(INDEX_NAME);
|
||||
assertThat(indexInformation.getSettings().get("index.number_of_shards")).isEqualTo("1");
|
||||
assertThat(indexInformation.getSettings().get("index.number_of_replicas")).isEqualTo("0");
|
||||
assertThat(indexInformation.getSettings().get("index.analysis.analyzer.emailAnalyzer.type")).isEqualTo("custom");
|
||||
@ -93,15 +89,14 @@ public class IndexOperationTests {
|
||||
assertThat(aliasData.getSearchRouting()).isEqualTo("searchrouting");
|
||||
|
||||
String expectedMappings = "{\"properties\":{\"email\":{\"type\":\"text\",\"analyzer\":\"emailAnalyzer\"}}}";
|
||||
JSONAssert.assertEquals(expectedMappings, indexInformation.getMappings().toJson(), false);
|
||||
JSONAssert.assertEquals(expectedMappings, indexInformation.getMapping().toJson(), false);
|
||||
}
|
||||
|
||||
@Data
|
||||
@Document(indexName = "test-index-information-list")
|
||||
@Document(indexName = INDEX_NAME)
|
||||
@Setting(settingPath = "settings/test-settings.json")
|
||||
@Mapping(mappingPath = "mappings/test-mappings.json")
|
||||
protected static class EntityWithSettingsAndMappings {
|
||||
@Id
|
||||
String id;
|
||||
@Id String id;
|
||||
}
|
||||
}
|
@ -6,7 +6,5 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
/**
|
||||
* @author George Popides
|
||||
*/
|
||||
|
||||
@ContextConfiguration(classes = { ElasticsearchTemplateConfiguration.class })
|
||||
public class IndexOperationTransportTests extends IndexOperationTests {
|
||||
}
|
||||
public class IndexOperationTransportIntegrationTests extends IndexOperationIntegrationTests {}
|
Loading…
x
Reference in New Issue
Block a user