|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.enrich; |
| 9 | + |
| 10 | +import org.elasticsearch.action.get.GetRequest; |
| 11 | +import org.elasticsearch.action.index.IndexRequest; |
| 12 | +import org.elasticsearch.action.support.WriteRequest; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.core.Strings; |
| 15 | +import org.elasticsearch.ingest.common.IngestCommonPlugin; |
| 16 | +import org.elasticsearch.plugins.Plugin; |
| 17 | +import org.elasticsearch.reindex.ReindexPlugin; |
| 18 | +import org.elasticsearch.test.ESSingleNodeTestCase; |
| 19 | +import org.elasticsearch.xcontent.XContentType; |
| 20 | +import org.elasticsearch.xpack.core.XPackSettings; |
| 21 | +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; |
| 22 | +import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; |
| 23 | +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; |
| 24 | + |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.elasticsearch.xpack.enrich.AbstractEnrichTestCase.createSourceIndices; |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | + |
| 32 | +public class EnrichSourceDataChangeIT extends ESSingleNodeTestCase { |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Collection<Class<? extends Plugin>> getPlugins() { |
| 36 | + return List.of(LocalStateEnrich.class, ReindexPlugin.class, IngestCommonPlugin.class); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected Settings nodeSettings() { |
| 41 | + return Settings.builder() |
| 42 | + // TODO Change this to run with security enabled |
| 43 | + // https://github.com/elastic/elasticsearch/issues/75940 |
| 44 | + .put(XPackSettings.SECURITY_ENABLED.getKey(), false) |
| 45 | + .build(); |
| 46 | + } |
| 47 | + |
| 48 | + private final String policyName = "device-enrich-policy"; |
| 49 | + private final String sourceIndexName = "devices-idx"; |
| 50 | + |
| 51 | + public void testChangesToTheSourceIndexTakeEffectOnPolicyExecution() throws Exception { |
| 52 | + // create and store the enrich policy |
| 53 | + final var enrichPolicy = new EnrichPolicy( |
| 54 | + EnrichPolicy.MATCH_TYPE, |
| 55 | + null, |
| 56 | + List.of(sourceIndexName), |
| 57 | + "host.ip", |
| 58 | + List.of("device.name", "host.ip") |
| 59 | + ); |
| 60 | + |
| 61 | + // create the source index |
| 62 | + createSourceIndices(client(), enrichPolicy); |
| 63 | + |
| 64 | + final String initialDeviceName = "some.device." + randomAlphaOfLength(10); |
| 65 | + |
| 66 | + // add a single document to the enrich index |
| 67 | + setEnrichDeviceName(initialDeviceName); |
| 68 | + |
| 69 | + // store the enrich policy and execute it |
| 70 | + var putPolicyRequest = new PutEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policyName, enrichPolicy); |
| 71 | + client().execute(PutEnrichPolicyAction.INSTANCE, putPolicyRequest).actionGet(); |
| 72 | + executeEnrichPolicy(); |
| 73 | + |
| 74 | + // create an honest to goodness pipeline for repeated executions (we're not running any _simulate requests here) |
| 75 | + final String pipelineName = "my-pipeline"; |
| 76 | + putJsonPipeline(pipelineName, """ |
| 77 | + { |
| 78 | + "processors": [ |
| 79 | + { |
| 80 | + "enrich": { |
| 81 | + "policy_name": "device-enrich-policy", |
| 82 | + "field": "host.ip", |
| 83 | + "target_field": "_tmp.device" |
| 84 | + } |
| 85 | + }, |
| 86 | + { |
| 87 | + "rename" : { |
| 88 | + "field" : "_tmp.device.device.name", |
| 89 | + "target_field" : "device.name" |
| 90 | + } |
| 91 | + }, |
| 92 | + { |
| 93 | + "remove" : { |
| 94 | + "field" : "_tmp" |
| 95 | + } |
| 96 | + } |
| 97 | + ] |
| 98 | + }"""); |
| 99 | + |
| 100 | + { |
| 101 | + final var indexRequest = new IndexRequest(sourceIndexName); |
| 102 | + indexRequest.id("1"); |
| 103 | + indexRequest.setPipeline("my-pipeline"); |
| 104 | + indexRequest.source(""" |
| 105 | + { |
| 106 | + "host": { |
| 107 | + "ip": "10.151.80.8" |
| 108 | + } |
| 109 | + } |
| 110 | + """, XContentType.JSON); |
| 111 | + indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 112 | + client().index(indexRequest).actionGet(); |
| 113 | + |
| 114 | + final var response = client().get(new GetRequest(sourceIndexName).id("1")).actionGet(); |
| 115 | + assertThat(response.getSource().get("device"), equalTo(Map.of("name", initialDeviceName))); |
| 116 | + } |
| 117 | + |
| 118 | + // add different document to the enrich index |
| 119 | + final String changedDeviceName = "some.device." + randomAlphaOfLength(10); |
| 120 | + setEnrichDeviceName(changedDeviceName); |
| 121 | + |
| 122 | + // execute the policy to pick up the change |
| 123 | + executeEnrichPolicy(); |
| 124 | + |
| 125 | + // it can take a moment for the execution to take effect, so assertBusy |
| 126 | + assertBusy(() -> { |
| 127 | + final var indexRequest = new IndexRequest(sourceIndexName); |
| 128 | + indexRequest.id("2"); |
| 129 | + indexRequest.setPipeline("my-pipeline"); |
| 130 | + indexRequest.source(""" |
| 131 | + { |
| 132 | + "host": { |
| 133 | + "ip": "10.151.80.8" |
| 134 | + } |
| 135 | + } |
| 136 | + """, XContentType.JSON); |
| 137 | + indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 138 | + client().index(indexRequest).actionGet(); |
| 139 | + |
| 140 | + final var response = client().get(new GetRequest(sourceIndexName).id("2")).actionGet(); |
| 141 | + assertThat(response.getSource().get("device"), equalTo(Map.of("name", changedDeviceName))); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + private void setEnrichDeviceName(final String deviceName) { |
| 146 | + final var indexRequest = new IndexRequest(sourceIndexName); |
| 147 | + indexRequest.id("1"); // there's only one document, and we keep overwriting it |
| 148 | + indexRequest.source(Strings.format(""" |
| 149 | + { |
| 150 | + "host": { |
| 151 | + "ip": "10.151.80.8" |
| 152 | + }, |
| 153 | + "device": { |
| 154 | + "name": "%s" |
| 155 | + } |
| 156 | + } |
| 157 | + """, deviceName), XContentType.JSON); |
| 158 | + indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 159 | + client().index(indexRequest).actionGet(); |
| 160 | + } |
| 161 | + |
| 162 | + private void executeEnrichPolicy() { |
| 163 | + final var executePolicyRequest = new ExecuteEnrichPolicyAction.Request(TEST_REQUEST_TIMEOUT, policyName); |
| 164 | + client().execute(ExecuteEnrichPolicyAction.INSTANCE, executePolicyRequest).actionGet(); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments