From 15e5fafb5664d69aa80a006ab9781b5bc631f0c2 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 18 Sep 2023 16:51:50 -0700 Subject: [PATCH] Addressed PR comments Signed-off-by: Owais Kazi --- .../workflow/CreateIndex/CreateIndexStep.java | 43 ++++++++++++++----- .../{knn-index-mapping.json => knn.json} | 0 2 files changed, 32 insertions(+), 11 deletions(-) rename src/main/resources/mappings/{knn-index-mapping.json => knn.json} (100%) diff --git a/src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java b/src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java index 242d80bae..0cb36cabf 100644 --- a/src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java +++ b/src/main/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStep.java @@ -16,6 +16,7 @@ import org.opensearch.action.admin.indices.create.CreateIndexRequest; import org.opensearch.action.admin.indices.create.CreateIndexResponse; import org.opensearch.client.Client; +import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.XContentType; import org.opensearch.flowframework.workflow.WorkflowData; import org.opensearch.flowframework.workflow.WorkflowStep; @@ -26,12 +27,19 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; +/** + * Step to create an index + */ public class CreateIndexStep implements WorkflowStep { private static final Logger logger = LogManager.getLogger(CreateIndexStep.class); private Client client; - private final String CREATE_INDEX_STEP = "create_index_step"; + private final String NAME = "create_index_step"; + /** + * Instantiate this class + * @param client Client to create an index + */ public CreateIndexStep(Client client) { this.client = client; } @@ -43,7 +51,7 @@ public CompletableFuture execute(List data) { @Override public void onResponse(CreateIndexResponse createIndexResponse) { - logger.info("created index:{}"); + logger.info("created index:{}", createIndexResponse.index()); future.complete(new WorkflowData() { @Override public Map getContent() { @@ -54,41 +62,54 @@ public Map getContent() { @Override public void onFailure(Exception e) { - logger.error("Index creation failed", e); + logger.error("Failed to create an index", e); future.completeExceptionally(e); } }; String index = null; + String type = null; + Settings settings = null; for (WorkflowData workflowData : data) { // Fetch index from content i.e. request body of execute API Map content = workflowData.getContent(); index = (String) content.get("index"); + type = (String) content.get("type"); + settings = (Settings) content.get("settings"); + if (index != null && type != null) { + break; + } } // TODO: - // 1. Map index type -> fileName - // 2. Create settings based on the index settings received from content - CreateIndexRequest request = new CreateIndexRequest(index).mapping(getIndexMappings(fileName), XContentType.JSON) - .settings(settings); - client.admin().indices().create(request, actionListener); + // 1. Create settings based on the index settings received from content + + try { + CreateIndexRequest request = new CreateIndexRequest(index).mapping(getIndexMappings(type), XContentType.JSON) + .settings(settings); + client.admin().indices().create(request, actionListener); + } catch (Exception e) { + logger.error("Failed to find the right mapping for the index", e); + } + return future; } @Override public String getName() { - return CREATE_INDEX_STEP; + return NAME; } /** * Get index mapping json content. * + * @param mapping type of the index to fetch the specific mapping file * @return index mapping * @throws IOException IOException if mapping file can't be read correctly */ - public static String getIndexMappings(String mappingFileName) throws IOException { - URL url = CreateIndexStep.class.getClassLoader().getResource(mappingFileName); + public static String getIndexMappings(String mapping) throws IOException { + URL url = CreateIndexStep.class.getClassLoader().getResource(mapping); return Resources.toString(url, Charsets.UTF_8); } } diff --git a/src/main/resources/mappings/knn-index-mapping.json b/src/main/resources/mappings/knn.json similarity index 100% rename from src/main/resources/mappings/knn-index-mapping.json rename to src/main/resources/mappings/knn.json