Added IndicesAdminClient.getIndexTemplates()

In addition to creating and removing a template, one can now receive index templates as well.
Simple regexes like template* are supported.

Closes #3439
This commit is contained in:
Alexander Reelsen 2013-08-12 12:57:15 +02:00
parent dbed36a13f
commit 45c0d1de04
11 changed files with 453 additions and 47 deletions

View File

@ -88,6 +88,8 @@ import org.elasticsearch.action.admin.indices.status.IndicesStatusAction;
import org.elasticsearch.action.admin.indices.status.TransportIndicesStatusAction; import org.elasticsearch.action.admin.indices.status.TransportIndicesStatusAction;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateAction; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.delete.TransportDeleteIndexTemplateAction; import org.elasticsearch.action.admin.indices.template.delete.TransportDeleteIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesAction;
import org.elasticsearch.action.admin.indices.template.get.TransportGetIndexTemplatesAction;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateAction; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.put.TransportPutIndexTemplateAction; import org.elasticsearch.action.admin.indices.template.put.TransportPutIndexTemplateAction;
import org.elasticsearch.action.admin.indices.validate.query.TransportValidateQueryAction; import org.elasticsearch.action.admin.indices.validate.query.TransportValidateQueryAction;
@ -206,6 +208,7 @@ public class ActionModule extends AbstractModule {
registerAction(UpdateSettingsAction.INSTANCE, TransportUpdateSettingsAction.class); registerAction(UpdateSettingsAction.INSTANCE, TransportUpdateSettingsAction.class);
registerAction(AnalyzeAction.INSTANCE, TransportAnalyzeAction.class); registerAction(AnalyzeAction.INSTANCE, TransportAnalyzeAction.class);
registerAction(PutIndexTemplateAction.INSTANCE, TransportPutIndexTemplateAction.class); registerAction(PutIndexTemplateAction.INSTANCE, TransportPutIndexTemplateAction.class);
registerAction(GetIndexTemplatesAction.INSTANCE, TransportGetIndexTemplatesAction.class);
registerAction(DeleteIndexTemplateAction.INSTANCE, TransportDeleteIndexTemplateAction.class); registerAction(DeleteIndexTemplateAction.INSTANCE, TransportDeleteIndexTemplateAction.class);
registerAction(ValidateQueryAction.INSTANCE, TransportValidateQueryAction.class); registerAction(ValidateQueryAction.INSTANCE, TransportValidateQueryAction.class);
registerAction(GatewaySnapshotAction.INSTANCE, TransportGatewaySnapshotAction.class); registerAction(GatewaySnapshotAction.INSTANCE, TransportGatewaySnapshotAction.class);

View File

@ -0,0 +1,45 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.template.get;
import org.elasticsearch.action.admin.indices.IndicesAction;
import org.elasticsearch.client.IndicesAdminClient;
/**
*
*/
public class GetIndexTemplatesAction extends IndicesAction<GetIndexTemplatesRequest, GetIndexTemplatesResponse, GetIndexTemplatesRequestBuilder> {
public static final GetIndexTemplatesAction INSTANCE = new GetIndexTemplatesAction();
public static final String NAME = "indices/template/get";
protected GetIndexTemplatesAction() {
super(NAME);
}
@Override
public GetIndexTemplatesResponse newResponse() {
return new GetIndexTemplatesResponse();
}
@Override
public GetIndexTemplatesRequestBuilder newRequestBuilder(IndicesAdminClient client) {
return new GetIndexTemplatesRequestBuilder(client);
}
}

View File

@ -0,0 +1,78 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.template.get;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.info.ClusterInfoRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
import static org.elasticsearch.action.ValidateActions.addValidationError;
/**
*
*/
public class GetIndexTemplatesRequest extends ClusterInfoRequest<GetIndexTemplatesRequest> {
private String name;
public GetIndexTemplatesRequest() {}
public GetIndexTemplatesRequest(String name) {
this.name = name;
}
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (name == null) {
validationException = addValidationError("name is missing", validationException);
}
return validationException;
}
/**
* Sets the name of the index template.
*/
public GetIndexTemplatesRequest name(String name) {
this.name = name;
return this;
}
/**
* The name of the index template.
*/
public String name() {
return this.name;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
name = in.readString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(name);
}
}

View File

@ -0,0 +1,43 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.template.get;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.internal.InternalIndicesAdminClient;
/**
*
*/
public class GetIndexTemplatesRequestBuilder extends MasterNodeOperationRequestBuilder<GetIndexTemplatesRequest, GetIndexTemplatesResponse, GetIndexTemplatesRequestBuilder> {
public GetIndexTemplatesRequestBuilder(IndicesAdminClient indicesClient) {
super((InternalIndicesAdminClient) indicesClient, new GetIndexTemplatesRequest());
}
public GetIndexTemplatesRequestBuilder(IndicesAdminClient indicesClient, String name) {
super((InternalIndicesAdminClient) indicesClient, new GetIndexTemplatesRequest(name));
}
@Override
protected void doExecute(ActionListener<GetIndexTemplatesResponse> listener) {
((IndicesAdminClient) client).getTemplates(request, listener);
}
}

View File

@ -0,0 +1,66 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.template.get;
import com.google.common.collect.Lists;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
import java.util.List;
/**
*
*/
public class GetIndexTemplatesResponse extends ActionResponse {
private List<IndexTemplateMetaData> indexTemplates;
GetIndexTemplatesResponse() {
}
GetIndexTemplatesResponse(List<IndexTemplateMetaData> indexTemplates) {
this.indexTemplates = indexTemplates;
}
public List<IndexTemplateMetaData> getIndexTemplates() {
return indexTemplates;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
int size = in.readVInt();
indexTemplates = Lists.newArrayListWithExpectedSize(size);
for (int i = 0 ; i < size ; i++) {
indexTemplates.add(0, IndexTemplateMetaData.Builder.readFrom(in));
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(indexTemplates.size());
for (IndexTemplateMetaData indexTemplate : indexTemplates) {
IndexTemplateMetaData.Builder.writeTo(indexTemplate, out);
}
}
}

View File

@ -0,0 +1,85 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.action.admin.indices.template.get;
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.info.TransportClusterInfoAction;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import java.util.List;
import java.util.Map;
/**
*
*/
public class TransportGetIndexTemplatesAction extends TransportClusterInfoAction<GetIndexTemplatesRequest, GetIndexTemplatesResponse> {
@Inject
public TransportGetIndexTemplatesAction(Settings settings, TransportService transportService, ClusterService clusterService, ThreadPool threadPool) {
super(settings, transportService, clusterService, threadPool);
}
@Override
protected String transportAction() {
return GetIndexTemplatesAction.NAME;
}
@Override
protected String executor() {
return ThreadPool.Names.SAME;
}
@Override
protected GetIndexTemplatesRequest newRequest() {
return new GetIndexTemplatesRequest();
}
@Override
protected GetIndexTemplatesResponse newResponse() {
return new GetIndexTemplatesResponse();
}
@Override
protected void doMasterOperation(final GetIndexTemplatesRequest request, final ClusterState state, final ActionListener<GetIndexTemplatesResponse> listener) throws ElasticSearchException {
List<IndexTemplateMetaData> results = Lists.newArrayList();
if (Regex.isSimpleMatchPattern(request.name())) {
for (Map.Entry<String, IndexTemplateMetaData> entry : state.metaData().templates().entrySet()) {
if (Regex.simpleMatch(request.name(), entry.getKey())) {
results.add(entry.getValue());
}
}
} else if (state.metaData().templates().containsKey(request.name())) {
results.add(state.metaData().templates().get(request.name()));
}
listener.onResponse(new GetIndexTemplatesResponse(results));
}
}

View File

@ -89,6 +89,9 @@ import org.elasticsearch.action.admin.indices.status.IndicesStatusResponse;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequestBuilder; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequestBuilder;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequestBuilder;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
@ -636,6 +639,23 @@ public interface IndicesAdminClient {
*/ */
DeleteIndexTemplateRequestBuilder prepareDeleteTemplate(String name); DeleteIndexTemplateRequestBuilder prepareDeleteTemplate(String name);
/**
* Gets index template.
*/
ActionFuture<GetIndexTemplatesResponse> getTemplates(GetIndexTemplatesRequest request);
/**
* Gets an index template.
*/
void getTemplates(GetIndexTemplatesRequest request, ActionListener<GetIndexTemplatesResponse> listener);
/**
* Gets an index template.
*
* @param name The name of the template.
*/
GetIndexTemplatesRequestBuilder prepareGetTemplates(String name);
/** /**
* Validate a query for correctness. * Validate a query for correctness.
* *

View File

@ -112,6 +112,10 @@ import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplat
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequestBuilder; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequestBuilder;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesAction;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequestBuilder;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateAction; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
@ -495,6 +499,21 @@ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdmin
return new PutIndexTemplateRequestBuilder(this, name); return new PutIndexTemplateRequestBuilder(this, name);
} }
@Override
public ActionFuture<GetIndexTemplatesResponse> getTemplates(final GetIndexTemplatesRequest request) {
return execute(GetIndexTemplatesAction.INSTANCE, request);
}
@Override
public void getTemplates(final GetIndexTemplatesRequest request, final ActionListener<GetIndexTemplatesResponse> listener) {
execute(GetIndexTemplatesAction.INSTANCE, request, listener);
}
@Override
public GetIndexTemplatesRequestBuilder prepareGetTemplates(String name) {
return new GetIndexTemplatesRequestBuilder(this, name);
}
@Override @Override
public ActionFuture<DeleteIndexTemplateResponse> deleteTemplate(final DeleteIndexTemplateRequest request) { public ActionFuture<DeleteIndexTemplateResponse> deleteTemplate(final DeleteIndexTemplateRequest request) {
return execute(DeleteIndexTemplateAction.INSTANCE, request); return execute(DeleteIndexTemplateAction.INSTANCE, request);

View File

@ -20,22 +20,19 @@
package org.elasticsearch.rest.action.admin.indices.template.get; package org.elasticsearch.rest.action.admin.indices.template.get;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -47,47 +44,36 @@ import static org.elasticsearch.rest.RestStatus.OK;
*/ */
public class RestGetIndexTemplateAction extends BaseRestHandler { public class RestGetIndexTemplateAction extends BaseRestHandler {
private final SettingsFilter settingsFilter;
@Inject @Inject
public RestGetIndexTemplateAction(Settings settings, Client client, RestController controller, public RestGetIndexTemplateAction(Settings settings, Client client, RestController controller) {
SettingsFilter settingsFilter) {
super(settings, client); super(settings, client);
this.settingsFilter = settingsFilter;
controller.registerHandler(GET, "/_template/{name}", this); controller.registerHandler(GET, "/_template/{name}", this);
} }
@Override @Override
public void handleRequest(final RestRequest request, final RestChannel channel) { public void handleRequest(final RestRequest request, final RestChannel channel) {
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest() GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(request.param("name"));
.filterRoutingTable(true) getIndexTemplatesRequest.listenerThreaded(false);
.filterNodes(true)
.filteredIndexTemplates(request.param("name"))
.filterOutIndices();
clusterStateRequest.listenerThreaded(false); client.admin().indices().getTemplates(getIndexTemplatesRequest, new ActionListener<GetIndexTemplatesResponse>() {
client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
@Override @Override
public void onResponse(ClusterStateResponse response) { public void onResponse(GetIndexTemplatesResponse getIndexTemplatesResponse) {
try {
boolean templateExists = getIndexTemplatesResponse.getIndexTemplates().size() > 0;
if (!templateExists) {
channel.sendResponse(new StringRestResponse(NOT_FOUND));
} else {
Map<String, String> paramsMap = Maps.newHashMap(); Map<String, String> paramsMap = Maps.newHashMap();
paramsMap.put("reduce_mappings", "true"); paramsMap.put("reduce_mappings", "true");
ToXContent.Params params = new ToXContent.DelegatingMapParams(paramsMap, request); ToXContent.Params params = new ToXContent.DelegatingMapParams(paramsMap, request);
try {
MetaData metaData = response.getState().metaData();
if (metaData.templates().values().size() == 0) {
channel.sendResponse(new StringRestResponse(NOT_FOUND));
} else {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject(); builder.startObject();
for (IndexTemplateMetaData indexTemplateMetaData : getIndexTemplatesResponse.getIndexTemplates()) {
for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) { IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
} }
builder.endObject(); builder.endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder)); channel.sendResponse(new XContentRestResponse(request, OK, builder));
@ -100,8 +86,8 @@ public class RestGetIndexTemplateAction extends BaseRestHandler {
@Override @Override
public void onFailure(Throwable e) { public void onFailure(Throwable e) {
try { try {
channel.sendResponse(new XContentThrowableRestResponse(request, e)); channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
} catch (IOException e1) { } catch (Exception e1) {
logger.error("Failed to send failure response", e1); logger.error("Failed to send failure response", e1);
} }
} }

View File

@ -20,10 +20,9 @@ package org.elasticsearch.rest.action.admin.indices.template.head;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
@ -46,19 +45,12 @@ public class RestHeadIndexTemplateAction extends BaseRestHandler {
@Override @Override
public void handleRequest(final RestRequest request, final RestChannel channel) { public void handleRequest(final RestRequest request, final RestChannel channel) {
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest() GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(request.param("name"));
.filterRoutingTable(true) client.admin().indices().getTemplates(getIndexTemplatesRequest, new ActionListener<GetIndexTemplatesResponse>() {
.filterNodes(true)
.filteredIndexTemplates(request.param("name"))
.filterOutIndices();
clusterStateRequest.listenerThreaded(false);
client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
@Override @Override
public void onResponse(ClusterStateResponse response) { public void onResponse(GetIndexTemplatesResponse getIndexTemplatesResponse) {
try { try {
boolean templateExists = response.getState().metaData().templates().size() > 0; boolean templateExists = getIndexTemplatesResponse.getIndexTemplates().size() > 0;
if (templateExists) { if (templateExists) {
channel.sendResponse(new StringRestResponse(OK)); channel.sendResponse(new StringRestResponse(OK));

View File

@ -19,6 +19,8 @@
package org.elasticsearch.test.integration.indices.template; package org.elasticsearch.test.integration.indices.template;
import com.google.common.collect.Lists;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.Priority; import org.elasticsearch.common.Priority;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -27,10 +29,11 @@ import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.*;
/** /**
* *
@ -150,4 +153,70 @@ public class SimpleIndexTemplateTests extends AbstractSharedClusterTest {
admin().indices().prepareDeleteTemplate("template*").execute().actionGet(); admin().indices().prepareDeleteTemplate("template*").execute().actionGet();
assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().size(), equalTo(0)); assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().size(), equalTo(0));
} }
@Test
public void testThatGetIndexTemplatesWorks() throws Exception {
logger.info("--> put template_1");
client().admin().indices().preparePutTemplate("template_1")
.setTemplate("te*")
.setOrder(0)
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field1").field("type", "string").field("store", "yes").endObject()
.startObject("field2").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
logger.info("--> get template template_1");
GetIndexTemplatesResponse getTemplate1Response = client().admin().indices().prepareGetTemplates("template_1").execute().actionGet();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(1));
assertThat(getTemplate1Response.getIndexTemplates().get(0), is(notNullValue()));
assertThat(getTemplate1Response.getIndexTemplates().get(0).getTemplate(), is("te*"));
assertThat(getTemplate1Response.getIndexTemplates().get(0).getOrder(), is(0));
logger.info("--> get non-existing-template");
GetIndexTemplatesResponse getTemplate2Response = client().admin().indices().prepareGetTemplates("non-existing-template").execute().actionGet();
assertThat(getTemplate2Response.getIndexTemplates(), hasSize(0));
}
@Test
public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception {
logger.info("--> put template_1");
client().admin().indices().preparePutTemplate("template_1")
.setTemplate("te*")
.setOrder(0)
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field1").field("type", "string").field("store", "yes").endObject()
.startObject("field2").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
logger.info("--> put template_2");
client().admin().indices().preparePutTemplate("template_2")
.setTemplate("te*")
.setOrder(0)
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field1").field("type", "string").field("store", "yes").endObject()
.startObject("field2").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
logger.info("--> put template3");
client().admin().indices().preparePutTemplate("template3")
.setTemplate("te*")
.setOrder(0)
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field1").field("type", "string").field("store", "yes").endObject()
.startObject("field2").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
logger.info("--> get template template_*");
GetIndexTemplatesResponse getTemplate1Response = client().admin().indices().prepareGetTemplates("template_*").execute().actionGet();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(2));
List<String> templateNames = Lists.newArrayList();
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
assertThat(templateNames, containsInAnyOrder("template_1", "template_2"));
}
} }