Skip to content

Commit 51a703d

Browse files
authored
Add enrich transport client support (#46002)
This commit adds an enrich client, as well as a smoke test to validate the client works.
1 parent 1157224 commit 51a703d

File tree

8 files changed

+170
-8
lines changed

8 files changed

+170
-8
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.xpack.core.action.XPackInfoAction;
1818
import org.elasticsearch.xpack.core.action.XPackInfoRequestBuilder;
1919
import org.elasticsearch.xpack.core.ccr.client.CcrClient;
20+
import org.elasticsearch.xpack.core.enrich.client.EnrichClient;
2021
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;
2122
import org.elasticsearch.xpack.core.ilm.client.ILMClient;
2223
import org.elasticsearch.xpack.core.ml.client.MachineLearningClient;
@@ -43,6 +44,7 @@ public class XPackClient {
4344
private final WatcherClient watcherClient;
4445
private final MachineLearningClient machineLearning;
4546
private final ILMClient ilmClient;
47+
private final EnrichClient enrichClient;
4648

4749
public XPackClient(Client client) {
4850
this.client = Objects.requireNonNull(client, "client");
@@ -53,6 +55,7 @@ public XPackClient(Client client) {
5355
this.watcherClient = new WatcherClient(client);
5456
this.machineLearning = new MachineLearningClient(client);
5557
this.ilmClient = new ILMClient(client);
58+
this.enrichClient = new EnrichClient(client);
5659
}
5760

5861
public Client es() {
@@ -87,6 +90,10 @@ public ILMClient ilmClient() {
8790
return ilmClient;
8891
}
8992

93+
public EnrichClient enrichClient() {
94+
return enrichClient;
95+
}
96+
9097
public XPackClient withHeaders(Map<String, String> headers) {
9198
return new XPackClient(client.filterWithHeader(headers));
9299
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
package org.elasticsearch.xpack.core;
77

88
import org.elasticsearch.Version;
9-
import org.elasticsearch.action.ActionType;
109
import org.elasticsearch.action.ActionResponse;
10+
import org.elasticsearch.action.ActionType;
1111
import org.elasticsearch.cluster.ClusterState;
1212
import org.elasticsearch.cluster.NamedDiff;
1313
import org.elasticsearch.cluster.metadata.MetaData;
@@ -57,6 +57,10 @@
5757
import org.elasticsearch.xpack.core.dataframe.transforms.TimeSyncConfig;
5858
import org.elasticsearch.xpack.core.datascience.DataScienceFeatureSetUsage;
5959
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
60+
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
61+
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
62+
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
63+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
6064
import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage;
6165
import org.elasticsearch.xpack.core.frozen.FrozenIndicesFeatureSetUsage;
6266
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;
@@ -427,8 +431,13 @@ public List<ActionType<? extends ActionResponse>> getClientActions() {
427431
DeleteDataFrameTransformAction.INSTANCE,
428432
GetDataFrameTransformsAction.INSTANCE,
429433
GetDataFrameTransformsStatsAction.INSTANCE,
430-
PreviewDataFrameTransformAction.INSTANCE
431-
);
434+
PreviewDataFrameTransformAction.INSTANCE,
435+
// enrich
436+
DeleteEnrichPolicyAction.INSTANCE,
437+
ExecuteEnrichPolicyAction.INSTANCE,
438+
GetEnrichPolicyAction.INSTANCE,
439+
PutEnrichPolicyAction.INSTANCE
440+
);
432441
}
433442

434443
@Override
@@ -589,7 +598,7 @@ public List<NamedXContentRegistry.Entry> getNamedXContent() {
589598
DataFrameTransformState::fromXContent),
590599
new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(DataFrameField.TASK_NAME),
591600
DataFrameTransformState::fromXContent)
592-
);
601+
);
593602
}
594603

595604
@Override
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.core.enrich.client;
7+
8+
import org.elasticsearch.action.ActionFuture;
9+
import org.elasticsearch.action.ActionListener;
10+
import org.elasticsearch.action.support.PlainActionFuture;
11+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
12+
import org.elasticsearch.client.ElasticsearchClient;
13+
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
14+
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
15+
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
16+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
17+
18+
import java.util.Objects;
19+
20+
public class EnrichClient {
21+
22+
private final ElasticsearchClient client;
23+
24+
public EnrichClient(ElasticsearchClient client) {
25+
this.client = Objects.requireNonNull(client, "client");
26+
}
27+
28+
public void deleteEnrichPolicy(
29+
final DeleteEnrichPolicyAction.Request request,
30+
final ActionListener<AcknowledgedResponse> listener) {
31+
client.execute(DeleteEnrichPolicyAction.INSTANCE, request, listener);
32+
}
33+
34+
public ActionFuture<AcknowledgedResponse> deleteEnrichPolicy(final DeleteEnrichPolicyAction.Request request) {
35+
final PlainActionFuture<AcknowledgedResponse> listener = PlainActionFuture.newFuture();
36+
client.execute(DeleteEnrichPolicyAction.INSTANCE, request, listener);
37+
return listener;
38+
}
39+
40+
public void executeEnrichPolicy(
41+
final ExecuteEnrichPolicyAction.Request request,
42+
final ActionListener<AcknowledgedResponse> listener) {
43+
client.execute(ExecuteEnrichPolicyAction.INSTANCE, request, listener);
44+
}
45+
46+
public ActionFuture<AcknowledgedResponse> executeEnrichPolicy(final ExecuteEnrichPolicyAction.Request request) {
47+
final PlainActionFuture<AcknowledgedResponse> listener = PlainActionFuture.newFuture();
48+
client.execute(ExecuteEnrichPolicyAction.INSTANCE, request, listener);
49+
return listener;
50+
}
51+
52+
public void getEnrichPolicy(
53+
final GetEnrichPolicyAction.Request request,
54+
final ActionListener<GetEnrichPolicyAction.Response> listener) {
55+
client.execute(GetEnrichPolicyAction.INSTANCE, request, listener);
56+
}
57+
58+
public ActionFuture<GetEnrichPolicyAction.Response> getEnrichPolicy(final GetEnrichPolicyAction.Request request) {
59+
final PlainActionFuture<GetEnrichPolicyAction.Response> listener = PlainActionFuture.newFuture();
60+
client.execute(GetEnrichPolicyAction.INSTANCE, request, listener);
61+
return listener;
62+
}
63+
64+
public void putEnrichPolicy(
65+
final PutEnrichPolicyAction.Request request,
66+
final ActionListener<AcknowledgedResponse> listener) {
67+
client.execute(PutEnrichPolicyAction.INSTANCE, request, listener);
68+
}
69+
70+
public ActionFuture<AcknowledgedResponse> putEnrichPolicy(final PutEnrichPolicyAction.Request request) {
71+
final PlainActionFuture<AcknowledgedResponse> listener = PlainActionFuture.newFuture();
72+
client.execute(PutEnrichPolicyAction.INSTANCE, request, listener);
73+
return listener;
74+
}
75+
}

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.concurrent.TimeUnit;
5959
import java.util.function.Supplier;
6060

61+
import static java.util.Collections.emptyList;
6162
import static org.elasticsearch.xpack.core.XPackSettings.ENRICH_ENABLED_SETTING;
6263

6364
public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
@@ -89,10 +90,12 @@ public class EnrichPlugin extends Plugin implements ActionPlugin, IngestPlugin {
8990

9091
private final Settings settings;
9192
private final Boolean enabled;
93+
private final boolean transportClientMode;
9294

9395
public EnrichPlugin(final Settings settings) {
9496
this.settings = settings;
9597
this.enabled = ENRICH_ENABLED_SETTING.get(settings);
98+
this.transportClientMode = XPackPlugin.transportClientMode(settings);
9699
}
97100

98101
@Override
@@ -106,7 +109,7 @@ public Map<String, Processor.Factory> getProcessors(Processor.Parameters paramet
106109

107110
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
108111
if (enabled == false) {
109-
return Collections.emptyList();
112+
return emptyList();
110113
}
111114

112115
return Arrays.asList(
@@ -124,7 +127,7 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC
124127
IndexNameExpressionResolver indexNameExpressionResolver,
125128
Supplier<DiscoveryNodes> nodesInCluster) {
126129
if (enabled == false) {
127-
return Collections.emptyList();
130+
return emptyList();
128131
}
129132

130133
return Arrays.asList(
@@ -140,6 +143,9 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
140143
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
141144
NamedXContentRegistry xContentRegistry, Environment environment,
142145
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) {
146+
if (enabled == false || transportClientMode) {
147+
return emptyList();
148+
}
143149
EnrichPolicyLocks enrichPolicyLocks = new EnrichPolicyLocks();
144150
EnrichPolicyExecutor enrichPolicyExecutor = new EnrichPolicyExecutor(settings, clusterService, client, threadPool,
145151
new IndexNameExpressionResolver(), enrichPolicyLocks, System::currentTimeMillis);

x-pack/qa/transport-client-tests/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ testingConventions {
1616
naming.clear()
1717
naming {
1818
IT {
19-
baseClass 'org.elasticsearch.xpack.ml.client.ESXPackSmokeClientTestCase'
19+
baseClass 'org.elasticsearch.xpack.ESXPackSmokeClientTestCase'
2020
}
2121
}
2222
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
package org.elasticsearch.xpack.ml.client;
6+
package org.elasticsearch.xpack;
77

88
import org.apache.logging.log4j.Logger;
99
import org.apache.logging.log4j.LogManager;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.enrich.client;
8+
9+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
10+
import org.elasticsearch.client.Client;
11+
import org.elasticsearch.xpack.ESXPackSmokeClientTestCase;
12+
import org.elasticsearch.xpack.core.XPackClient;
13+
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
14+
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
15+
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
16+
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
17+
import org.elasticsearch.xpack.core.enrich.client.EnrichClient;
18+
19+
import java.io.IOException;
20+
import java.util.Collections;
21+
22+
import static org.hamcrest.Matchers.equalTo;
23+
import static org.hamcrest.Matchers.nullValue;
24+
25+
public class EnrichTransportClientIT extends ESXPackSmokeClientTestCase {
26+
27+
private static void assertEqualPolicies(EnrichPolicy expectedInstance, EnrichPolicy newInstance) {
28+
assertThat(newInstance.getType(), equalTo(expectedInstance.getType()));
29+
if (newInstance.getQuery() != null) {
30+
// testFromXContent, always shuffles the xcontent and then byte wise the query is different, so we check the parsed version:
31+
assertThat(newInstance.getQuery().getQueryAsMap(), equalTo(expectedInstance.getQuery().getQueryAsMap()));
32+
} else {
33+
assertThat(expectedInstance.getQuery(), nullValue());
34+
}
35+
assertThat(newInstance.getIndices(), equalTo(expectedInstance.getIndices()));
36+
assertThat(newInstance.getMatchField(), equalTo(expectedInstance.getMatchField()));
37+
assertThat(newInstance.getEnrichFields(), equalTo(expectedInstance.getEnrichFields()));
38+
}
39+
40+
public void testEnrichCrud() throws IOException {
41+
Client client = getClient();
42+
XPackClient xPackClient = new XPackClient(client);
43+
EnrichClient enrichClient = xPackClient.enrichClient();
44+
45+
EnrichPolicy policy = new EnrichPolicy("exact_match", null, Collections.emptyList(), "test", Collections.emptyList());
46+
String policyName = "my-policy";
47+
48+
AcknowledgedResponse acknowledgedResponse = enrichClient.putEnrichPolicy(
49+
new PutEnrichPolicyAction.Request(policyName,
50+
policy)).actionGet();
51+
52+
assertTrue(acknowledgedResponse.isAcknowledged());
53+
54+
GetEnrichPolicyAction.Response getResponse = enrichClient.getEnrichPolicy(
55+
new GetEnrichPolicyAction.Request(policyName)).actionGet();
56+
57+
assertThat(getResponse.getPolicies().size(), equalTo(1));
58+
assertThat(policyName, equalTo(getResponse.getPolicies().get(0).getName()));
59+
assertEqualPolicies(policy, getResponse.getPolicies().get(0).getPolicy());
60+
61+
acknowledgedResponse = enrichClient.deleteEnrichPolicy(new DeleteEnrichPolicyAction.Request(policyName)).actionGet();
62+
assertTrue(acknowledgedResponse.isAcknowledged());
63+
}
64+
}

x-pack/qa/transport-client-tests/src/test/java/org/elasticsearch/xpack/ml/client/MLTransportClientIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.common.bytes.BytesArray;
1111
import org.elasticsearch.common.unit.TimeValue;
1212
import org.elasticsearch.common.xcontent.XContentType;
13+
import org.elasticsearch.xpack.ESXPackSmokeClientTestCase;
1314
import org.elasticsearch.xpack.core.XPackClient;
1415
import org.elasticsearch.xpack.core.ml.action.CloseJobAction;
1516
import org.elasticsearch.xpack.core.ml.action.DeleteJobAction;

0 commit comments

Comments
 (0)