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 2 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 @@ -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,29 @@ 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);

Map<String, Set<AliasData>> aliases = getAliasesForIndex(indexCoordinates.getIndexNames());
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is executing a separate call to Elasticsearch for the aliases. But alias information is return in the GetIndex request, no need for a separate call here


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

List<IndexInformation> indexInformationList = new ArrayList<>();

for (String indexName : getIndexResponse.getIndices()) {
Document settings = requestFactory.settingsFromGetIndexResponse(getIndexResponse, indexName);
Document mappings = requestFactory.mappingsFromGetIndexResponse(getIndexResponse, indexName);
Set<AliasData> indexAliases = aliases.get(indexName);

indexInformationList.add(IndexInformation.create(indexName, settings, mappings, indexAliases));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't this whole loop be moved as well into the RequestFactory? Something like

List<IndexInformation> indexInformationList = requestFactory.fromGetIndexResponse(getIndexResponse);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also didn't really like writing the same thing 3 times. But shouldn't request factory create only Request objects?
Is that a good place to add a function which returns a list of IndexInformation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

fair point. The problem is that the reactive and non-reactive templates have no common base. Now that there are more than just one method for response data it might make sense to add a ResponseConverter class next to the RequestFactory that contains the methods for processing response data and that is used like the Requestfactoryis.


return indexInformationList;
});
}

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

import reactor.core.publisher.Mono;

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

Expand All @@ -38,7 +38,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 +54,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 Down Expand Up @@ -304,6 +309,27 @@ 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)
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

.flatMap(getIndexResponse -> getAliasesForIndex(getIndexCoordinates().getIndexNames()).map(aliases -> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

no separate call to get the alias information. This is returned in the GetIndex call

List<IndexInformation> indexInformationList = new ArrayList<>();

for (String indexName : getIndexResponse.getIndices()) {
Document settings = requestFactory.settingsFromGetIndexResponse(getIndexResponse, indexName);
Document mappings = requestFactory.mappingsFromGetIndexResponse(getIndexResponse, indexName);
Set<AliasData> indexAliases = aliases.get(indexName);

indexInformationList.add(IndexInformation.create(indexName, settings, mappings, indexAliases));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment as in the non-reactive code

return indexInformationList;
})
))
).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 @@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -29,6 +30,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 +60,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 +70,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 +301,27 @@ 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());

Map<String, Set<AliasData>> aliases = getAliases(index.getIndexNames());
Copy link
Collaborator

Choose a reason for hiding this comment

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

no separate call for aliases, check my other comments


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

GetIndexResponse getIndexResponse = client.admin().indices().getIndex(getIndexRequest).actionGet();
List<IndexInformation> indexInformationList = new ArrayList<>();

for (String indexName : getIndexResponse.getIndices()) {
Document settings = requestFactory.settingsFromGetIndexResponse(getIndexResponse, indexName);
Document mappings = requestFactory.mappingsFromGetIndexResponse(getIndexResponse, indexName);
Set<AliasData> indexAliases = aliases.get(indexName);

indexInformationList.add(IndexInformation.create(indexName, settings, mappings, indexAliases));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

see other comments


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

import reactor.core.publisher.Mono;

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

Expand All @@ -29,11 +27,16 @@
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.
*
* @author Peter-Josef Meisch
* @author George Popides
* @since 4.1
*/
public interface ReactiveIndexOperations {
Expand Down Expand Up @@ -284,5 +287,11 @@ default Mono<Boolean> deleteTemplate(String templateName) {
*/
IndexCoordinates getIndexCoordinates();

/**
*
* @return a flux of {@link IndexInformation}
* @since 4.2
*/
Flux<IndexInformation> getInformation();
// endregion
}
Loading