Skip to content

Add _reload_search_analyzers endpoint to HLRC #43733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 3, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersResponse;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
import org.elasticsearch.client.indices.rollover.RolloverRequest;
import org.elasticsearch.client.indices.rollover.RolloverResponse;
Expand Down Expand Up @@ -1328,4 +1330,28 @@ public void deleteTemplateAsync(DeleteIndexTemplateRequest request, RequestOptio
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteTemplate,
options, AcknowledgedResponse::fromXContent, listener, emptySet());
}

/**
* Synchronously calls the _reload_search_analyzers API
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public ReloadAnalyzersResponse reloadAnalyzers(ReloadAnalyzersRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options,
ReloadAnalyzersResponse::fromXContent, emptySet());
}

/**
* Asynchronously calls the _reload_search_analyzers API
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void reloadAnalyzersAsync(ReloadAnalyzersRequest request, RequestOptions options,
ActionListener<ReloadAnalyzersResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options,
ReloadAnalyzersResponse::fromXContent, listener, emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
import org.elasticsearch.client.indices.rollover.RolloverRequest;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -644,4 +645,13 @@ static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequ
request.addParameters(params.asMap());
return request;
}

static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) {
String endpoint = RequestConverters.endpoint(reloadAnalyzersRequest.getIndices(), "_reload_search_analyzers");
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withIndicesOptions(reloadAnalyzersRequest.indicesOptions());
request.addParameters(parameters.asMap());
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Shards shards() {
return shards;
}

BroadcastResponse(final Shards shards) {
protected BroadcastResponse(final Shards shards) {
this.shards = Objects.requireNonNull(shards);
}

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

static {
PARSER.declareObject(ConstructingObjectParser.constructorArg(), Shards.SHARDS_PARSER, SHARDS_FIELD);
declareShardsField(PARSER);
}

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

protected static <T extends BroadcastResponse> void declareShardsField(ConstructingObjectParser<T, Void> PARSER) {
PARSER.declareObject(ConstructingObjectParser.constructorArg(), Shards.SHARDS_PARSER, SHARDS_FIELD);
}

/**
* Represents the results of a collection of shards on which a request was executed against.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.indices;

import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Validatable;

import java.util.Objects;

/**
* Request for the _reload_search_analyzers API
*/
public final class ReloadAnalyzersRequest implements Validatable {

private final String[] indices;
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();

/**
* Creates a new reload analyzers request
* @param indices the index for which to reload analyzers
*/
public ReloadAnalyzersRequest(String... indices) {
this.indices = Objects.requireNonNull(indices);
}

/**
* Returns the indices
*/
public String[] getIndices() {
return indices;
}

/**
* Specifies what type of requested indices to ignore and how to deal with wildcard expressions.
* For example indices that don't exist.
*
* @return the current behaviour when it comes to index names and wildcard indices expressions
*/
public IndicesOptions indicesOptions() {
return indicesOptions;
}

/**
* Specifies what type of requested indices to ignore and how to deal with wildcard expressions.
* For example indices that don't exist.
*
* @param indicesOptions the desired behaviour regarding indices to ignore and wildcard indices expressions
*/
public void setIndicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.indices;

import org.elasticsearch.client.core.BroadcastResponse;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

/**
* The response object that will be returned when reloading analyzers
*/
public class ReloadAnalyzersResponse extends BroadcastResponse {

private final Map<String, ReloadDetails> reloadDetails;

ReloadAnalyzersResponse(final Shards shards, Map<String, ReloadDetails> reloadDetails) {
super(shards);
this.reloadDetails = reloadDetails;
}

@SuppressWarnings({ "unchecked" })
private static final ConstructingObjectParser<ReloadAnalyzersResponse, Void> PARSER = new ConstructingObjectParser<>("reload_analyzer",
true, arg -> {
Shards shards = (Shards) arg[0];
List<Tuple<String, ReloadDetails>> results = (List<Tuple<String, ReloadDetails>>) arg[1];
Map<String, ReloadDetails> reloadDetails = new HashMap<>();
for (Tuple<String, ReloadDetails> result : results) {
reloadDetails.put(result.v1(), result.v2());
}
return new ReloadAnalyzersResponse(shards, reloadDetails);
});

@SuppressWarnings({ "unchecked" })
private static final ConstructingObjectParser<Tuple<String, ReloadDetails>, Void> ENTRY_PARSER = new ConstructingObjectParser<>(
"reload_analyzer.entry", true, arg -> {
String index = (String) arg[0];
Set<String> nodeIds = new HashSet<>((List<String>) arg[1]);
Set<String> analyzers = new HashSet<>((List<String>) arg[2]);
return new Tuple<>(index, new ReloadDetails(index, nodeIds, analyzers));
});

static {
declareShardsField(PARSER);
PARSER.declareObjectArray(constructorArg(), ENTRY_PARSER, new ParseField("reload_details"));
ENTRY_PARSER.declareString(constructorArg(), new ParseField("index"));
ENTRY_PARSER.declareStringArray(constructorArg(), new ParseField("reloaded_node_ids"));
ENTRY_PARSER.declareStringArray(constructorArg(), new ParseField("reloaded_analyzers"));
}

public static ReloadAnalyzersResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

public Map<String, ReloadDetails> getReloadedDetails() {
return reloadDetails;
}

public static class ReloadDetails {

private final String indexName;
private final Set<String> reloadedIndicesNodes;
private final Set<String> reloadedAnalyzers;

public ReloadDetails(String name, Set<String> reloadedIndicesNodes, Set<String> reloadedAnalyzers) {
this.indexName = name;
this.reloadedIndicesNodes = reloadedIndicesNodes;
this.reloadedAnalyzers = reloadedAnalyzers;
}

public String getIndexName() {
return indexName;
}

public Set<String> getReloadedIndicesNodes() {
return reloadedIndicesNodes;
}

public Set<String> getReloadedAnalyzers() {
return reloadedAnalyzers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
import org.elasticsearch.client.indices.ReloadAnalyzersResponse;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
import org.elasticsearch.client.indices.rollover.RolloverRequest;
import org.elasticsearch.client.indices.rollover.RolloverResponse;
Expand Down Expand Up @@ -1877,4 +1879,14 @@ public void testFreezeAndUnfreeze() throws IOException {
assertTrue(unfreeze.isShardsAcknowledged());
assertTrue(unfreeze.isAcknowledged());
}

public void testReloadAnalyzer() throws IOException {
createIndex("test", Settings.EMPTY);
RestHighLevelClient client = highLevelClient();

ReloadAnalyzersResponse reloadResponse = execute(new ReloadAnalyzersRequest("test"), client.indices()::reloadAnalyzers,
client.indices()::reloadAnalyzersAsync);
assertNotNull(reloadResponse.shards());
assertTrue(reloadResponse.getReloadedDetails().containsKey("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.RandomCreateIndexGenerator;
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
import org.elasticsearch.client.indices.rollover.RolloverRequest;
import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -1215,4 +1216,21 @@ public void testDeleteTemplateRequest() {
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
Assert.assertThat(request.getEntity(), nullValue());
}

public void testReloadAnalyzers() {
String[] indices = RequestConvertersTests.randomIndicesNames(1, 5);
StringJoiner endpoint = new StringJoiner("/", "/", "");
if (indices != null && indices.length > 0) {
endpoint.add(String.join(",", indices));
}
ReloadAnalyzersRequest reloadRequest = new ReloadAnalyzersRequest(indices);
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomIndicesOptions(reloadRequest::setIndicesOptions, reloadRequest::indicesOptions,
expectedParams);
Request request = IndicesRequestConverters.reloadAnalyzers(reloadRequest);
Assert.assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
Assert.assertThat(request.getEndpoint(), equalTo(endpoint + "/_reload_search_analyzers"));
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
Assert.assertThat(request.getEntity(), nullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ public void testApiNamingConventions() throws Exception {
apiName.startsWith("ccr.") == false &&
apiName.startsWith("data_frame") == false &&
apiName.endsWith("freeze") == false &&
apiName.endsWith("reload_analyzers") == false &&
// IndicesClientIT.getIndexTemplate should be renamed "getTemplate" in version 8.0 when we
// can get rid of 7.0's deprecated "getTemplate"
apiName.equals("indices.get_index_template") == false) {
Expand Down
Loading