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 1 commit
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 @@ -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,16 @@ public boolean deleteTemplate(DeleteTemplateRequest deleteTemplateRequest) {
client -> client.indices().deleteTemplate(deleteIndexTemplateRequest, RequestOptions.DEFAULT).isAcknowledged());
}

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

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 IndexInformation.createList(getIndexResponse);
});
}

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

import reactor.core.publisher.Mono;

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

Expand All @@ -38,7 +37,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 +53,16 @@
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.Mono;

/**
* @author Peter-Josef Meisch
* @author George Popides
* @since 4.1
*/
class DefaultReactiveIndexOperations implements ReactiveIndexOperations {
Expand Down Expand Up @@ -304,6 +307,14 @@ public IndexCoordinates getIndexCoordinates() {
return (boundClass != null) ? getIndexCoordinatesFor(boundClass) : boundIndex;
}

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

return Mono.from(operations.executeWithIndicesClient(client -> client.getIndex(HttpHeaders.EMPTY, getIndexRequest)
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

.map(IndexInformation::createList)));
}

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 response = 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 IndexInformation.createList(response);
}
}
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 Down Expand Up @@ -317,5 +318,7 @@ default boolean deleteTemplate(String templateName) {
*/
IndexCoordinates getIndexCoordinates();

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.springframework.data.elasticsearch.core;

import org.springframework.data.elasticsearch.core.mapping.IndexInformation;
import reactor.core.publisher.Mono;

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

Expand Down Expand Up @@ -284,5 +286,6 @@ default Mono<Boolean> deleteTemplate(String templateName) {
*/
IndexCoordinates getIndexCoordinates();

Mono<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.

we should rather return a Flux<IndexInformation> than a Mono of a list here.
Please add Javadoc and @since 4.2

// endregion
}
Loading