|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +package org.opensearch.dataprepper.plugins.sink.opensearch; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.JsonNode; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
| 16 | +import org.opensearch.client.opensearch._types.VersionType; |
| 17 | +import org.opensearch.client.opensearch.core.bulk.BulkOperation; |
| 18 | +import org.opensearch.client.opensearch.core.bulk.CreateOperation; |
| 19 | +import org.opensearch.client.opensearch.core.bulk.DeleteOperation; |
| 20 | +import org.opensearch.client.opensearch.core.bulk.IndexOperation; |
| 21 | +import org.opensearch.client.opensearch.core.bulk.UpdateOperation; |
| 22 | +import org.opensearch.dataprepper.model.opensearch.OpenSearchBulkActions; |
| 23 | +import org.opensearch.dataprepper.model.sink.SinkContext; |
| 24 | +import org.opensearch.dataprepper.plugins.sink.opensearch.bulk.SerializedJson; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Optional; |
| 28 | + |
| 29 | +public class BulkOperationFactory { |
| 30 | + |
| 31 | + private final ObjectMapper objectMapper; |
| 32 | + private final ScriptManager scriptManager; |
| 33 | + private final VersionType versionType; |
| 34 | + private final String documentRootKey; |
| 35 | + private final SinkContext sinkContext; |
| 36 | + |
| 37 | + public BulkOperationFactory(final ScriptManager scriptManager, |
| 38 | + final VersionType versionType, |
| 39 | + final String documentRootKey, |
| 40 | + final SinkContext sinkContext) { |
| 41 | + this.objectMapper = new ObjectMapper(); |
| 42 | + this.scriptManager = scriptManager; |
| 43 | + this.versionType = versionType; |
| 44 | + this.documentRootKey = documentRootKey; |
| 45 | + this.sinkContext = sinkContext; |
| 46 | + } |
| 47 | + |
| 48 | + public BulkOperation create(final String action, |
| 49 | + final SerializedJson document, |
| 50 | + final Long version, |
| 51 | + final String indexName, |
| 52 | + final JsonNode jsonNode) { |
| 53 | + final Optional<String> docId = document.getDocumentId(); |
| 54 | + final Optional<String> routing = document.getRoutingField(); |
| 55 | + final Optional<String> pipeline = document.getPipelineField(); |
| 56 | + |
| 57 | + if (StringUtils.equals(action, OpenSearchBulkActions.CREATE.toString())) { |
| 58 | + return createCreateOperation(document, indexName, docId, routing, pipeline); |
| 59 | + } |
| 60 | + if (StringUtils.equals(action, OpenSearchBulkActions.UPDATE.toString()) || |
| 61 | + StringUtils.equals(action, OpenSearchBulkActions.UPSERT.toString())) { |
| 62 | + return createUpdateOperation(action, document, version, indexName, jsonNode, docId, routing); |
| 63 | + } |
| 64 | + if (StringUtils.equals(action, OpenSearchBulkActions.DELETE.toString())) { |
| 65 | + return createDeleteOperation(version, indexName, docId, routing); |
| 66 | + } |
| 67 | + return createIndexOperation(document, version, indexName, docId, routing, pipeline); |
| 68 | + } |
| 69 | + |
| 70 | + private BulkOperation createCreateOperation(final SerializedJson document, |
| 71 | + final String indexName, |
| 72 | + final Optional<String> docId, |
| 73 | + final Optional<String> routing, |
| 74 | + final Optional<String> pipeline) { |
| 75 | + final CreateOperation.Builder<Object> builder = new CreateOperation.Builder<>() |
| 76 | + .index(indexName) |
| 77 | + .document(document); |
| 78 | + docId.ifPresent(builder::id); |
| 79 | + routing.ifPresent(builder::routing); |
| 80 | + pipeline.ifPresent(builder::pipeline); |
| 81 | + return new BulkOperation.Builder().create(builder.build()).build(); |
| 82 | + } |
| 83 | + |
| 84 | + private BulkOperation createUpdateOperation(final String action, |
| 85 | + final SerializedJson document, |
| 86 | + final Long version, |
| 87 | + final String indexName, |
| 88 | + final JsonNode jsonNode, |
| 89 | + final Optional<String> docId, |
| 90 | + final Optional<String> routing) { |
| 91 | + JsonNode filteredJsonNode = jsonNode; |
| 92 | + try { |
| 93 | + if (isUsingDocumentFilters()) { |
| 94 | + filteredJsonNode = objectMapper.reader().readTree(document.getSerializedJson()); |
| 95 | + } |
| 96 | + } catch (final IOException e) { |
| 97 | + throw new RuntimeException( |
| 98 | + String.format("An exception occurred while deserializing a document for the %s action: %s", action, e.getMessage())); |
| 99 | + } |
| 100 | + |
| 101 | + final boolean isUpsert = StringUtils.equals(action.toLowerCase(), OpenSearchBulkActions.UPSERT.toString()); |
| 102 | + final UpdateOperation.Builder<Object> builder = new UpdateOperation.Builder<>() |
| 103 | + .index(indexName) |
| 104 | + .versionType(versionType) |
| 105 | + .version(version); |
| 106 | + |
| 107 | + if (scriptManager.isScriptEnabled()) { |
| 108 | + builder.script(scriptManager.buildScript(jsonNode, document.getResolvedScriptParameters().orElse(null))); |
| 109 | + builder.upsert(filteredJsonNode); |
| 110 | + builder.scriptedUpsert(true); |
| 111 | + } else if (isUpsert) { |
| 112 | + builder.document(filteredJsonNode).upsert(filteredJsonNode); |
| 113 | + } else { |
| 114 | + builder.document(filteredJsonNode); |
| 115 | + } |
| 116 | + |
| 117 | + docId.ifPresent(builder::id); |
| 118 | + routing.ifPresent(builder::routing); |
| 119 | + return new BulkOperation.Builder().update(builder.build()).build(); |
| 120 | + } |
| 121 | + |
| 122 | + private BulkOperation createDeleteOperation(final Long version, |
| 123 | + final String indexName, |
| 124 | + final Optional<String> docId, |
| 125 | + final Optional<String> routing) { |
| 126 | + final DeleteOperation.Builder builder = new DeleteOperation.Builder() |
| 127 | + .index(indexName) |
| 128 | + .versionType(versionType) |
| 129 | + .version(version); |
| 130 | + docId.ifPresent(builder::id); |
| 131 | + routing.ifPresent(builder::routing); |
| 132 | + return new BulkOperation.Builder().delete(builder.build()).build(); |
| 133 | + } |
| 134 | + |
| 135 | + private BulkOperation createIndexOperation(final SerializedJson document, |
| 136 | + final Long version, |
| 137 | + final String indexName, |
| 138 | + final Optional<String> docId, |
| 139 | + final Optional<String> routing, |
| 140 | + final Optional<String> pipeline) { |
| 141 | + final IndexOperation.Builder<Object> builder = new IndexOperation.Builder<>() |
| 142 | + .index(indexName) |
| 143 | + .document(document) |
| 144 | + .version(version) |
| 145 | + .versionType(versionType); |
| 146 | + docId.ifPresent(builder::id); |
| 147 | + routing.ifPresent(builder::routing); |
| 148 | + pipeline.ifPresent(builder::pipeline); |
| 149 | + return new BulkOperation.Builder().index(builder.build()).build(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * This function is used for update and upsert bulk actions to determine whether the original JsonNode needs to be filtered down |
| 154 | + * based on the user's sink configuration. If a new parameter manipulates the document before sending to OpenSearch, it needs to be added to |
| 155 | + * this list to get applied for update and upsert actions |
| 156 | + * @return whether the doc needs filtering |
| 157 | + */ |
| 158 | + private boolean isUsingDocumentFilters() { |
| 159 | + return documentRootKey != null || |
| 160 | + (sinkContext.getIncludeKeys() != null && !sinkContext.getIncludeKeys().isEmpty()) || |
| 161 | + (sinkContext.getExcludeKeys() != null && !sinkContext.getExcludeKeys().isEmpty()) || |
| 162 | + sinkContext.getTagsTargetKey() != null; |
| 163 | + } |
| 164 | +} |
0 commit comments