|
14 | 14 | import org.opensearch.action.admin.indices.create.CreateIndexRequest;
|
15 | 15 | import org.opensearch.action.support.PlainActionFuture;
|
16 | 16 | import org.opensearch.client.Client;
|
17 |
| -import org.opensearch.common.xcontent.XContentType; |
| 17 | +import org.opensearch.common.xcontent.LoggingDeprecationHandler; |
| 18 | +import org.opensearch.common.xcontent.XContentHelper; |
18 | 19 | import org.opensearch.core.action.ActionListener;
|
19 | 20 | import org.opensearch.core.common.bytes.BytesArray;
|
20 | 21 | import org.opensearch.core.common.bytes.BytesReference;
|
| 22 | +import org.opensearch.core.rest.RestStatus; |
| 23 | +import org.opensearch.core.xcontent.MediaTypeRegistry; |
21 | 24 | import org.opensearch.flowframework.exception.FlowFrameworkException;
|
| 25 | +import org.opensearch.flowframework.exception.WorkflowStepException; |
22 | 26 | import org.opensearch.flowframework.indices.FlowFrameworkIndicesHandler;
|
23 | 27 | import org.opensearch.flowframework.util.ParseUtils;
|
| 28 | +import org.opensearch.index.mapper.MapperService; |
24 | 29 |
|
25 | 30 | import java.io.IOException;
|
26 | 31 | import java.nio.charset.StandardCharsets;
|
27 | 32 | import java.util.Collections;
|
| 33 | +import java.util.HashMap; |
28 | 34 | import java.util.Map;
|
29 | 35 | import java.util.Set;
|
30 | 36 |
|
| 37 | +import static java.util.Collections.singletonMap; |
31 | 38 | import static org.opensearch.flowframework.common.CommonValue.CONFIGURATIONS;
|
32 | 39 | import static org.opensearch.flowframework.common.WorkflowResources.INDEX_NAME;
|
33 | 40 | import static org.opensearch.flowframework.common.WorkflowResources.getResourceByWorkflowStep;
|
@@ -85,8 +92,13 @@ public PlainActionFuture<WorkflowData> execute(
|
85 | 92 |
|
86 | 93 | byte[] byteArr = configurations.getBytes(StandardCharsets.UTF_8);
|
87 | 94 | BytesReference configurationsBytes = new BytesArray(byteArr);
|
| 95 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); |
| 96 | + if (!configurations.isEmpty()) { |
| 97 | + Map<String, Object> sourceAsMap = XContentHelper.convertToMap(configurationsBytes, false, MediaTypeRegistry.JSON).v2(); |
| 98 | + sourceAsMap = prepareMappings(sourceAsMap); |
| 99 | + createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE); |
| 100 | + } |
88 | 101 |
|
89 |
| - CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).source(configurationsBytes, XContentType.JSON); |
90 | 102 | client.admin().indices().create(createIndexRequest, ActionListener.wrap(acknowledgedResponse -> {
|
91 | 103 | String resourceName = getResourceByWorkflowStep(getName());
|
92 | 104 | logger.info("Created index: {}", indexName);
|
@@ -129,6 +141,26 @@ public PlainActionFuture<WorkflowData> execute(
|
129 | 141 | return createIndexFuture;
|
130 | 142 | }
|
131 | 143 |
|
| 144 | + // This method to check if the mapping contains a type `_doc` and if yes we fail the request |
| 145 | + // is to duplicate the behavior we have today through create index rest API, we want users |
| 146 | + // to encounter the same behavior and not suddenly have to add `_doc` while using our create_index step |
| 147 | + private static Map<String, Object> prepareMappings(Map<String, Object> source) { |
| 148 | + if (source.containsKey("mappings") == false || (source.get("mappings") instanceof Map) == false) { |
| 149 | + return source; |
| 150 | + } |
| 151 | + |
| 152 | + Map<String, Object> newSource = new HashMap<>(source); |
| 153 | + |
| 154 | + @SuppressWarnings("unchecked") |
| 155 | + Map<String, Object> mappings = (Map<String, Object>) source.get("mappings"); |
| 156 | + if (MapperService.isMappingSourceTyped(MapperService.SINGLE_MAPPING_NAME, mappings)) { |
| 157 | + throw new WorkflowStepException("The mapping definition cannot be nested under a type", RestStatus.BAD_REQUEST); |
| 158 | + } |
| 159 | + |
| 160 | + newSource.put("mappings", singletonMap(MapperService.SINGLE_MAPPING_NAME, mappings)); |
| 161 | + return newSource; |
| 162 | + } |
| 163 | + |
132 | 164 | @Override
|
133 | 165 | public String getName() {
|
134 | 166 | return NAME;
|
|
0 commit comments