Skip to content

Add function to list all indexes #1693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@
*/
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;
Expand Down Expand Up @@ -86,6 +74,7 @@
import org.elasticsearch.client.Request;
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;
Expand Down Expand Up @@ -131,6 +120,18 @@
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.
*
Expand All @@ -144,6 +145,7 @@
* @author Thomas Geese
* @author Brian Clozel
* @author Farid Faoudi
* @author George Popides
* @since 3.2
* @see ClientConfiguration
* @see ReactiveRestClients
Expand Down Expand Up @@ -757,6 +759,12 @@ public Mono<Boolean> deleteTemplate(HttpHeaders headers, DeleteIndexTemplateRequ
.map(AcknowledgedResponse::isAcknowledged).next();
}

@Override
public Mono<GetIndexResponse> getIndex(HttpHeaders headers, org.elasticsearch.client.indices.GetIndexRequest getIndexRequest) {
return sendRequest(getIndexRequest, requestCreator.getIndex(), GetIndexResponse.class, headers)
.next();
}

// endregion

// region helper functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.client.reactive;

import org.elasticsearch.client.indices.GetIndexResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -1456,5 +1457,7 @@ default Mono<Boolean> deleteTemplate(DeleteIndexTemplateRequest deleteIndexTempl
* @since 4.1
*/
Mono<Boolean> deleteTemplate(HttpHeaders headers, DeleteIndexTemplateRequest deleteIndexTemplateRequest);

Mono<GetIndexResponse> getIndex(HttpHeaders headers, org.elasticsearch.client.indices.GetIndexRequest getIndexRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
/**
* @author Roman Puchkovskiy
* @author Farid Faoudi
* @author George Popides
* @since 4.0
*/
public interface RequestCreator {
Expand Down Expand Up @@ -149,6 +150,7 @@ default Function<CountRequest, Request> count() {
return RequestConverters::count;
}

default Function<org.elasticsearch.client.indices.GetIndexRequest, Request> getIndex() { return RequestConverters::getIndex; }
/**
* @since 4.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,22 @@ public static Request getIndex(GetIndexRequest getIndexRequest) {
return request;
}

public static Request getIndex(org.elasticsearch.client.indices.GetIndexRequest getIndexRequest) {
String[] indices = getIndexRequest.indices() == null ? Strings.EMPTY_ARRAY : getIndexRequest.indices();

String endpoint = endpoint(indices);
Request request = new Request(HttpMethod.GET.name(), endpoint);

Params params = new Params(request);
params.withIndicesOptions(getIndexRequest.indicesOptions());
params.withLocal(getIndexRequest.local());
params.withIncludeDefaults(getIndexRequest.includeDefaults());
params.withHuman(getIndexRequest.humanReadable());
params.withMasterTimeout(getIndexRequest.masterNodeTimeout());

return request;
}

public static Request indexDelete(DeleteIndexRequest deleteIndexRequest) {
String endpoint = RequestConverters.endpoint(deleteIndexRequest.indices());
Request request = new Request(HttpMethod.DELETE.name(), endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ 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");
Expand All @@ -66,6 +68,7 @@ public AbstractDefaultIndexOperations(ElasticsearchConverter elasticsearchConver
requestFactory = new RequestFactory(elasticsearchConverter);
this.boundClass = boundClass;
this.boundIndex = null;
this.responseConverter = new ResponseConverter();
}

public AbstractDefaultIndexOperations(ElasticsearchConverter elasticsearchConverter, IndexCoordinates boundIndex) {
Expand All @@ -76,6 +79,7 @@ public AbstractDefaultIndexOperations(ElasticsearchConverter elasticsearchConver
requestFactory = new RequestFactory(elasticsearchConverter);
this.boundClass = null;
this.boundIndex = boundIndex;
this.responseConverter = new ResponseConverter();
}

protected Class<?> checkForBoundClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
import org.elasticsearch.client.indices.GetMappingsRequest;
Expand All @@ -51,6 +52,7 @@
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;
Expand All @@ -60,6 +62,7 @@
*
* @author Peter-Josef Meisch
* @author Sascha Woo
* @author George Popides
* @since 4.0
*/
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
Expand Down Expand Up @@ -256,5 +259,18 @@ public boolean deleteTemplate(DeleteTemplateRequest deleteTemplateRequest) {
client -> client.indices().deleteTemplate(deleteIndexTemplateRequest, RequestOptions.DEFAULT).isAcknowledged());
}

@Override
public List<IndexInformation> getInformation() {
IndexCoordinates indexCoordinates = getIndexCoordinates();
GetIndexRequest request = requestFactory.getIndexRequest(indexCoordinates);

return restTemplate.execute(
client -> {
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the mapping should be done from Elasticsearch types to our data

return responseConverter.indexInformationCollection(getIndexResponse);
});
}

// endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import static org.elasticsearch.client.Requests.*;
import static org.springframework.util.StringUtils.*;

import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.Set;

Expand All @@ -38,7 +36,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.elasticsearch.NoSuchIndexException;
import org.springframework.data.elasticsearch.annotations.Mapping;
Expand All @@ -55,11 +52,17 @@
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
* @since 4.1
*/
class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
Expand All @@ -71,6 +74,7 @@ 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) {

Expand All @@ -80,6 +84,7 @@ public DefaultReactiveIndexOperations(ReactiveElasticsearchOperations operations
this.operations = operations;
this.converter = operations.getElasticsearchConverter();
this.requestFactory = new RequestFactory(operations.getElasticsearchConverter());
this.responseConverter = new ResponseConverter();
this.boundClass = null;
this.boundIndex = index;
}
Expand All @@ -92,6 +97,7 @@ public DefaultReactiveIndexOperations(ReactiveElasticsearchOperations operations
this.operations = operations;
this.converter = operations.getElasticsearchConverter();
this.requestFactory = new RequestFactory(operations.getElasticsearchConverter());
this.responseConverter = new ResponseConverter();
this.boundClass = clazz;
this.boundIndex = getIndexCoordinatesFor(clazz);
}
Expand Down Expand Up @@ -159,11 +165,11 @@ public Mono<Document> createMapping() {
@Override
public Mono<Document> createMapping(Class<?> clazz) {

Mapping mappingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Mapping.class);
Mapping mappingAnnotation = AnnotatedElementUtils.findMergedAnnotation(clazz, Mapping.class);

if (mappingAnnotation != null) {
return loadDocument(mappingAnnotation.mappingPath(), "@Mapping");
}
if (mappingAnnotation != null) {
return loadDocument(mappingAnnotation.mappingPath(), "@Mapping");
}

String mapping = new MappingBuilder(converter).buildPropertyMapping(clazz);
return Mono.just(Document.parse(mapping));
Expand Down Expand Up @@ -201,11 +207,11 @@ public Mono<Document> createSettings() {
@Override
public Mono<Document> createSettings(Class<?> clazz) {

Setting setting = AnnotatedElementUtils.findMergedAnnotation(clazz, Setting.class);
Setting setting = AnnotatedElementUtils.findMergedAnnotation(clazz, Setting.class);

if (setting != null) {
return loadDocument(setting.settingPath(), "@Setting");
}
if (setting != null) {
return loadDocument(setting.settingPath(), "@Setting");
}

return Mono.just(getRequiredPersistentEntity(clazz).getDefaultSettings());
}
Expand Down Expand Up @@ -304,6 +310,15 @@ public IndexCoordinates getIndexCoordinates() {
return (boundClass != null) ? getIndexCoordinatesFor(boundClass) : boundIndex;
}

@Override
public Flux<IndexInformation> getInformation() {
org.elasticsearch.client.indices.GetIndexRequest getIndexRequest = requestFactory.getIndexRequest(getIndexCoordinates());

return Mono.from(operations.executeWithIndicesClient(client ->
client.getIndex(HttpHeaders.EMPTY, getIndexRequest).map(responseConverter::indexInformationCollection)))
.flatMapMany(Flux::fromIterable);
}

private IndexCoordinates getIndexCoordinatesFor(Class<?> clazz) {
return operations.getElasticsearchConverter().getMappingContext().getRequiredPersistentEntity(clazz)
.getIndexCoordinates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
Expand Down Expand Up @@ -57,6 +59,7 @@
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;
Expand All @@ -66,6 +69,7 @@
*
* @author Peter-Josef Meisch
* @author Sascha Woo
* @author George Popides
* @since 4.0
*/
class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
Expand Down Expand Up @@ -296,4 +300,16 @@ public boolean deleteTemplate(DeleteTemplateRequest deleteTemplateRequest) {
deleteTemplateRequest);
return client.admin().indices().deleteTemplate(deleteIndexTemplateRequest).actionGet().isAcknowledged();
}

@Override
public List<IndexInformation> getInformation() {
GetIndexRequest getIndexRequest = new GetIndexRequest();
IndexCoordinates index = getIndexCoordinates();

getIndexRequest.indices(index.getIndexNames());

GetIndexResponse getIndexResponse = client.admin().indices().getIndex(getIndexRequest).actionGet();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the mapping should be done from Elasticsearch types to our data

return responseConverter.indexInformationCollection(getIndexResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;

Expand All @@ -41,6 +42,7 @@
*
* @author Peter-Josef Meisch
* @author Sascha Woo
* @author George Popides
* @since 4.0
*/
public interface IndexOperations {
Expand Down Expand Up @@ -317,5 +319,13 @@ default boolean deleteTemplate(String templateName) {
*/
IndexCoordinates getIndexCoordinates();


/**
*
* @return a list of {@link IndexInformation}
* @since 4.2
*/
List<IndexInformation> getInformation();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add Javadoc with a @since 4.2 tag


// endregion
}
Loading