diff --git a/flint-core/src/main/scala/org/opensearch/flint/core/FlintClient.java b/flint-core/src/main/scala/org/opensearch/flint/core/FlintClient.java index 8ee4903d1..b4271360c 100644 --- a/flint-core/src/main/scala/org/opensearch/flint/core/FlintClient.java +++ b/flint-core/src/main/scala/org/opensearch/flint/core/FlintClient.java @@ -5,12 +5,11 @@ package org.opensearch.flint.core; +import java.util.List; import org.opensearch.flint.core.metadata.FlintMetadata; import org.opensearch.flint.core.storage.FlintReader; import org.opensearch.flint.core.storage.FlintWriter; -import java.io.Writer; - /** * Flint index client that provides API for metadata and data operations * on a Flint index regardless of concrete storage. @@ -33,6 +32,14 @@ public interface FlintClient { */ boolean exists(String indexName); + /** + * Retrieve all metadata for Flint index whose name matches the given pattern. + * + * @param indexNamePattern index name pattern + * @return all matched index metadata + */ + List getAllIndexMetadata(String indexNamePattern); + /** * Retrieve metadata in a Flint index. * diff --git a/flint-core/src/main/scala/org/opensearch/flint/core/storage/FlintOpenSearchClient.java b/flint-core/src/main/scala/org/opensearch/flint/core/storage/FlintOpenSearchClient.java index bc58a7c5e..4e3c5a76c 100644 --- a/flint-core/src/main/scala/org/opensearch/flint/core/storage/FlintOpenSearchClient.java +++ b/flint-core/src/main/scala/org/opensearch/flint/core/storage/FlintOpenSearchClient.java @@ -5,9 +5,17 @@ package org.opensearch.flint.core.storage; +import static org.opensearch.common.xcontent.DeprecationHandler.IGNORE_DEPRECATIONS; + import com.amazonaws.auth.AWS4Signer; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; import org.apache.http.HttpHost; import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; import org.opensearch.client.RequestOptions; @@ -34,13 +42,6 @@ import org.opensearch.search.SearchModule; import org.opensearch.search.builder.SearchSourceBuilder; -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicReference; - -import static org.opensearch.common.xcontent.DeprecationHandler.IGNORE_DEPRECATIONS; - /** * Flint client implementation for OpenSearch storage. */ @@ -79,6 +80,19 @@ public FlintOpenSearchClient(FlintOptions options) { } } + @Override public List getAllIndexMetadata(String indexNamePattern) { + try (RestHighLevelClient client = createClient()) { + GetMappingsRequest request = new GetMappingsRequest().indices(indexNamePattern); + GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT); + + return response.mappings().values().stream() + .map(mapping -> new FlintMetadata(mapping.source().string())) + .collect(Collectors.toList()); + } catch (Exception e) { + throw new IllegalStateException("Failed to get Flint index metadata for " + indexNamePattern, e); + } + } + @Override public FlintMetadata getIndexMetadata(String indexName) { try (RestHighLevelClient client = createClient()) { GetMappingsRequest request = new GetMappingsRequest().indices(indexName); diff --git a/integ-test/src/test/scala/org/opensearch/flint/core/FlintOpenSearchClientSuite.scala b/integ-test/src/test/scala/org/opensearch/flint/core/FlintOpenSearchClientSuite.scala index 91ec25c24..72fa1e116 100644 --- a/integ-test/src/test/scala/org/opensearch/flint/core/FlintOpenSearchClientSuite.scala +++ b/integ-test/src/test/scala/org/opensearch/flint/core/FlintOpenSearchClientSuite.scala @@ -43,6 +43,13 @@ class FlintOpenSearchClientSuite extends AnyFlatSpec with OpenSearchSuite with M flintClient.getIndexMetadata(indexName).getContent should matchJson(content) } + it should "get all index metadata with the given index name pattern" in { + flintClient.createIndex("flint_test_1_index", new FlintMetadata("{}")) + flintClient.createIndex("flint_test_2_index", new FlintMetadata("{}")) + + flintClient.getAllIndexMetadata("flint_*_index") should have size 2 + } + it should "return false if index not exist" in { flintClient.exists("non-exist-index") shouldBe false }