-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 2 commits
3fbc0c7
3337cd8
84f76dd
5967c77
b4d7bd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -60,6 +62,7 @@ | |
* | ||
* @author Peter-Josef Meisch | ||
* @author Sascha Woo | ||
* @author George Popides | ||
* @since 4.0 | ||
*/ | ||
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations { | ||
|
@@ -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()); | ||
|
||
return restTemplate.execute( | ||
client -> { | ||
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this whole loop be moved as well into the List<IndexInformation> indexInformationList = requestFactory.fromGetIndexResponse(getIndexResponse); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return indexInformationList; | ||
}); | ||
} | ||
|
||
// endregion | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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 { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -66,6 +70,7 @@ | |
* | ||
* @author Peter-Josef Meisch | ||
* @author Sascha Woo | ||
* @author George Popides | ||
* @since 4.0 | ||
*/ | ||
class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations { | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no separate call for aliases, check my other comments |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -41,6 +42,7 @@ | |
* | ||
* @author Peter-Josef Meisch | ||
* @author Sascha Woo | ||
* @author George Popides | ||
* @since 4.0 | ||
*/ | ||
public interface IndexOperations { | ||
|
@@ -317,5 +319,13 @@ default boolean deleteTemplate(String templateName) { | |
*/ | ||
IndexCoordinates getIndexCoordinates(); | ||
|
||
|
||
/** | ||
* | ||
* @return a list of {@link IndexInformation} | ||
* @since 4.2 | ||
*/ | ||
List<IndexInformation> getInformation(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add Javadoc with a |
||
|
||
// endregion | ||
} |
There was a problem hiding this comment.
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