Skip to content

Commit 662f517

Browse files
author
Christoph Büscher
committed
Add _reload_search_analyzers endpoint to HLRC (#43733)
This change adds the new endpoint that allows reloading of search analyzers to the high-level java rest client. Relates to #43313
1 parent 96b0b27 commit 662f517

File tree

14 files changed

+493
-3
lines changed

14 files changed

+493
-3
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java

+26
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
6262
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
6363
import org.elasticsearch.client.indices.PutMappingRequest;
64+
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
65+
import org.elasticsearch.client.indices.ReloadAnalyzersResponse;
6466
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
6567
import org.elasticsearch.client.indices.rollover.RolloverRequest;
6668
import org.elasticsearch.client.indices.rollover.RolloverResponse;
@@ -1328,4 +1330,28 @@ public void deleteTemplateAsync(DeleteIndexTemplateRequest request, RequestOptio
13281330
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
13291331
options, AcknowledgedResponse::fromXContent, listener, emptySet());
13301332
}
1333+
1334+
/**
1335+
* Synchronously calls the _reload_search_analyzers API
1336+
*
1337+
* @param request the request
1338+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1339+
*/
1340+
public ReloadAnalyzersResponse reloadAnalyzers(ReloadAnalyzersRequest request, RequestOptions options) throws IOException {
1341+
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options,
1342+
ReloadAnalyzersResponse::fromXContent, emptySet());
1343+
}
1344+
1345+
/**
1346+
* Asynchronously calls the _reload_search_analyzers API
1347+
*
1348+
* @param request the request
1349+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1350+
* @param listener the listener to be notified upon request completion
1351+
*/
1352+
public void reloadAnalyzersAsync(ReloadAnalyzersRequest request, RequestOptions options,
1353+
ActionListener<ReloadAnalyzersResponse> listener) {
1354+
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options,
1355+
ReloadAnalyzersResponse::fromXContent, listener, emptySet());
1356+
}
13311357
}

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
5151
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
5252
import org.elasticsearch.client.indices.PutMappingRequest;
53+
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
5354
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
5455
import org.elasticsearch.client.indices.rollover.RolloverRequest;
5556
import org.elasticsearch.common.Strings;
@@ -646,4 +647,13 @@ static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequ
646647
request.addParameters(params.asMap());
647648
return request;
648649
}
650+
651+
static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) {
652+
String endpoint = RequestConverters.endpoint(reloadAnalyzersRequest.getIndices(), "_reload_search_analyzers");
653+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
654+
RequestConverters.Params parameters = new RequestConverters.Params();
655+
parameters.withIndicesOptions(reloadAnalyzersRequest.indicesOptions());
656+
request.addParameters(parameters.asMap());
657+
return request;
658+
}
649659
}

client/rest-high-level/src/main/java/org/elasticsearch/client/core/BroadcastResponse.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Shards shards() {
4545
return shards;
4646
}
4747

48-
BroadcastResponse(final Shards shards) {
48+
protected BroadcastResponse(final Shards shards) {
4949
this.shards = Objects.requireNonNull(shards);
5050
}
5151

@@ -56,7 +56,7 @@ public Shards shards() {
5656
a -> new BroadcastResponse((Shards) a[0]));
5757

5858
static {
59-
PARSER.declareObject(ConstructingObjectParser.constructorArg(), Shards.SHARDS_PARSER, SHARDS_FIELD);
59+
declareShardsField(PARSER);
6060
}
6161

6262
/**
@@ -70,6 +70,10 @@ public static BroadcastResponse fromXContent(final XContentParser parser) throws
7070
return PARSER.parse(parser, null);
7171
}
7272

73+
protected static <T extends BroadcastResponse> void declareShardsField(ConstructingObjectParser<T, Void> PARSER) {
74+
PARSER.declareObject(ConstructingObjectParser.constructorArg(), Shards.SHARDS_PARSER, SHARDS_FIELD);
75+
}
76+
7377
/**
7478
* Represents the results of a collection of shards on which a request was executed against.
7579
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.indices;
20+
21+
import org.elasticsearch.action.support.IndicesOptions;
22+
import org.elasticsearch.client.Validatable;
23+
24+
import java.util.Objects;
25+
26+
/**
27+
* Request for the _reload_search_analyzers API
28+
*/
29+
public final class ReloadAnalyzersRequest implements Validatable {
30+
31+
private final String[] indices;
32+
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
33+
34+
/**
35+
* Creates a new reload analyzers request
36+
* @param indices the index for which to reload analyzers
37+
*/
38+
public ReloadAnalyzersRequest(String... indices) {
39+
this.indices = Objects.requireNonNull(indices);
40+
}
41+
42+
/**
43+
* Returns the indices
44+
*/
45+
public String[] getIndices() {
46+
return indices;
47+
}
48+
49+
/**
50+
* Specifies what type of requested indices to ignore and how to deal with wildcard expressions.
51+
* For example indices that don't exist.
52+
*
53+
* @return the current behaviour when it comes to index names and wildcard indices expressions
54+
*/
55+
public IndicesOptions indicesOptions() {
56+
return indicesOptions;
57+
}
58+
59+
/**
60+
* Specifies what type of requested indices to ignore and how to deal with wildcard expressions.
61+
* For example indices that don't exist.
62+
*
63+
* @param indicesOptions the desired behaviour regarding indices to ignore and wildcard indices expressions
64+
*/
65+
public void setIndicesOptions(IndicesOptions indicesOptions) {
66+
this.indicesOptions = indicesOptions;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.indices;
20+
21+
import org.elasticsearch.client.core.BroadcastResponse;
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.collect.Tuple;
24+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
27+
import java.util.HashMap;
28+
import java.util.HashSet;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Set;
32+
33+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
34+
35+
/**
36+
* The response object that will be returned when reloading analyzers
37+
*/
38+
public class ReloadAnalyzersResponse extends BroadcastResponse {
39+
40+
private final Map<String, ReloadDetails> reloadDetails;
41+
42+
ReloadAnalyzersResponse(final Shards shards, Map<String, ReloadDetails> reloadDetails) {
43+
super(shards);
44+
this.reloadDetails = reloadDetails;
45+
}
46+
47+
@SuppressWarnings({ "unchecked" })
48+
private static final ConstructingObjectParser<ReloadAnalyzersResponse, Void> PARSER = new ConstructingObjectParser<>("reload_analyzer",
49+
true, arg -> {
50+
Shards shards = (Shards) arg[0];
51+
List<Tuple<String, ReloadDetails>> results = (List<Tuple<String, ReloadDetails>>) arg[1];
52+
Map<String, ReloadDetails> reloadDetails = new HashMap<>();
53+
for (Tuple<String, ReloadDetails> result : results) {
54+
reloadDetails.put(result.v1(), result.v2());
55+
}
56+
return new ReloadAnalyzersResponse(shards, reloadDetails);
57+
});
58+
59+
@SuppressWarnings({ "unchecked" })
60+
private static final ConstructingObjectParser<Tuple<String, ReloadDetails>, Void> ENTRY_PARSER = new ConstructingObjectParser<>(
61+
"reload_analyzer.entry", true, arg -> {
62+
String index = (String) arg[0];
63+
Set<String> nodeIds = new HashSet<>((List<String>) arg[1]);
64+
Set<String> analyzers = new HashSet<>((List<String>) arg[2]);
65+
return new Tuple<>(index, new ReloadDetails(index, nodeIds, analyzers));
66+
});
67+
68+
static {
69+
declareShardsField(PARSER);
70+
PARSER.declareObjectArray(constructorArg(), ENTRY_PARSER, new ParseField("reload_details"));
71+
ENTRY_PARSER.declareString(constructorArg(), new ParseField("index"));
72+
ENTRY_PARSER.declareStringArray(constructorArg(), new ParseField("reloaded_node_ids"));
73+
ENTRY_PARSER.declareStringArray(constructorArg(), new ParseField("reloaded_analyzers"));
74+
}
75+
76+
public static ReloadAnalyzersResponse fromXContent(XContentParser parser) {
77+
return PARSER.apply(parser, null);
78+
}
79+
80+
public Map<String, ReloadDetails> getReloadedDetails() {
81+
return reloadDetails;
82+
}
83+
84+
public static class ReloadDetails {
85+
86+
private final String indexName;
87+
private final Set<String> reloadedIndicesNodes;
88+
private final Set<String> reloadedAnalyzers;
89+
90+
public ReloadDetails(String name, Set<String> reloadedIndicesNodes, Set<String> reloadedAnalyzers) {
91+
this.indexName = name;
92+
this.reloadedIndicesNodes = reloadedIndicesNodes;
93+
this.reloadedAnalyzers = reloadedAnalyzers;
94+
}
95+
96+
public String getIndexName() {
97+
return indexName;
98+
}
99+
100+
public Set<String> getReloadedIndicesNodes() {
101+
return reloadedIndicesNodes;
102+
}
103+
104+
public Set<String> getReloadedAnalyzers() {
105+
return reloadedAnalyzers;
106+
}
107+
}
108+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

+12
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
7474
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
7575
import org.elasticsearch.client.indices.PutMappingRequest;
76+
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
77+
import org.elasticsearch.client.indices.ReloadAnalyzersResponse;
7678
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
7779
import org.elasticsearch.client.indices.rollover.RolloverRequest;
7880
import org.elasticsearch.client.indices.rollover.RolloverResponse;
@@ -1877,4 +1879,14 @@ public void testFreezeAndUnfreeze() throws IOException {
18771879
assertTrue(unfreeze.isShardsAcknowledged());
18781880
assertTrue(unfreeze.isAcknowledged());
18791881
}
1882+
1883+
public void testReloadAnalyzer() throws IOException {
1884+
createIndex("test", Settings.EMPTY);
1885+
RestHighLevelClient client = highLevelClient();
1886+
1887+
ReloadAnalyzersResponse reloadResponse = execute(new ReloadAnalyzersRequest("test"), client.indices()::reloadAnalyzers,
1888+
client.indices()::reloadAnalyzersAsync);
1889+
assertNotNull(reloadResponse.shards());
1890+
assertTrue(reloadResponse.getReloadedDetails().containsKey("test"));
1891+
}
18801892
}

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
5555
import org.elasticsearch.client.indices.PutMappingRequest;
5656
import org.elasticsearch.client.indices.RandomCreateIndexGenerator;
57+
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
5758
import org.elasticsearch.client.indices.rollover.RolloverRequest;
5859
import org.elasticsearch.common.CheckedFunction;
5960
import org.elasticsearch.common.Strings;
@@ -1215,4 +1216,21 @@ public void testDeleteTemplateRequest() {
12151216
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
12161217
Assert.assertThat(request.getEntity(), nullValue());
12171218
}
1219+
1220+
public void testReloadAnalyzers() {
1221+
String[] indices = RequestConvertersTests.randomIndicesNames(1, 5);
1222+
StringJoiner endpoint = new StringJoiner("/", "/", "");
1223+
if (indices != null && indices.length > 0) {
1224+
endpoint.add(String.join(",", indices));
1225+
}
1226+
ReloadAnalyzersRequest reloadRequest = new ReloadAnalyzersRequest(indices);
1227+
Map<String, String> expectedParams = new HashMap<>();
1228+
RequestConvertersTests.setRandomIndicesOptions(reloadRequest::setIndicesOptions, reloadRequest::indicesOptions,
1229+
expectedParams);
1230+
Request request = IndicesRequestConverters.reloadAnalyzers(reloadRequest);
1231+
Assert.assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
1232+
Assert.assertThat(request.getEndpoint(), equalTo(endpoint + "/_reload_search_analyzers"));
1233+
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
1234+
Assert.assertThat(request.getEntity(), nullValue());
1235+
}
12181236
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ public void testApiNamingConventions() throws Exception {
824824
apiName.startsWith("ccr.") == false &&
825825
apiName.startsWith("data_frame") == false &&
826826
apiName.endsWith("freeze") == false &&
827+
apiName.endsWith("reload_analyzers") == false &&
827828
// IndicesClientIT.getIndexTemplate should be renamed "getTemplate" in version 8.0 when we
828829
// can get rid of 7.0's deprecated "getTemplate"
829830
apiName.equals("indices.get_index_template") == false) {

0 commit comments

Comments
 (0)