Search in sources :

Example 11 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testAliasInvalidFilterValidJson.

public void testAliasInvalidFilterValidJson() throws Exception {
    //invalid filter but valid json: put index template works fine, fails during index creation
    client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).addAlias(new Alias("invalid_alias").filter("{ \"invalid\": {} }")).get();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates("template_1").get();
    assertThat(response.getIndexTemplates().size(), equalTo(1));
    assertThat(response.getIndexTemplates().get(0).getAliases().size(), equalTo(1));
    assertThat(response.getIndexTemplates().get(0).getAliases().get("invalid_alias").filter().string(), equalTo("{\"invalid\":{}}"));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> createIndex("test"));
    assertThat(e.getMessage(), equalTo("failed to parse filter for alias [invalid_alias]"));
    assertThat(e.getCause(), instanceOf(ParsingException.class));
    assertThat(e.getCause().getMessage(), equalTo("no [query] registered for [invalid]"));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) ParsingException(org.elasticsearch.common.ParsingException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException)

Example 12 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.

the class RestGetIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] names = Strings.splitStringByCommaToArray(request.param("name"));
    final GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names);
    getIndexTemplatesRequest.local(request.paramAsBoolean("local", getIndexTemplatesRequest.local()));
    getIndexTemplatesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getIndexTemplatesRequest.masterNodeTimeout()));
    final boolean implicitAll = getIndexTemplatesRequest.names().length == 0;
    return channel -> client.admin().indices().getTemplates(getIndexTemplatesRequest, new RestToXContentListener<GetIndexTemplatesResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetIndexTemplatesResponse response) {
            final boolean templateExists = response.getIndexTemplates().isEmpty() == false;
            return (templateExists || implicitAll) ? OK : NOT_FOUND;
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) Set(java.util.Set) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestStatus(org.elasticsearch.rest.RestStatus) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest)

Example 13 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.

the class SimpleClusterStateIT method testIndexTemplates.

public void testIndexTemplates() throws Exception {
    client().admin().indices().preparePutTemplate("foo_template").setPatterns(Collections.singletonList("te*")).setOrder(0).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("store", true).endObject().startObject("field2").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject()).get();
    client().admin().indices().preparePutTemplate("fuu_template").setPatterns(Collections.singletonList("test*")).setOrder(1).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field2").field("type", "text").field("store", false).endObject().endObject().endObject().endObject()).get();
    ClusterStateResponse clusterStateResponseUnfiltered = client().admin().cluster().prepareState().get();
    assertThat(clusterStateResponseUnfiltered.getState().metaData().templates().size(), is(greaterThanOrEqualTo(2)));
    GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates("foo_template").get();
    assertIndexTemplateExists(getIndexTemplatesResponse, "foo_template");
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)

Example 14 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project graylog2-server by Graylog2.

the class IndicesTest method testIndexTemplateCanBeOverridden.

@Test
public void testIndexTemplateCanBeOverridden() throws Exception {
    final String customTemplateName = "custom-template";
    final IndicesAdminClient client = this.client.admin().indices();
    // Create custom index template
    final Map<String, Object> customMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
    final PutIndexTemplateResponse putIndexTemplateResponse = client.preparePutTemplate(customTemplateName).setTemplate(indexSet.getIndexWildcard()).setOrder(1).addMapping(IndexMapping.TYPE_MESSAGE, customMapping).get();
    assertThat(putIndexTemplateResponse.isAcknowledged()).isTrue();
    // Validate existing index templates
    final GetIndexTemplatesResponse getTemplatesResponse = client.prepareGetTemplates().get();
    final List<IndexTemplateMetaData> indexTemplates = getTemplatesResponse.getIndexTemplates();
    assertThat(indexTemplates).extracting(IndexTemplateMetaData::getName).containsExactly(customTemplateName);
    // Create index with custom template
    final String testIndexName = "graylog_override_template";
    indices.create(testIndexName, indexSet);
    // Check index mapping
    final GetMappingsResponse indexMappingResponse = client.prepareGetMappings(testIndexName).get();
    final String mapping = indexMappingResponse.getMappings().get(testIndexName).get(IndexMapping.TYPE_MESSAGE).source().string();
    final ReadContext ctx = JsonPath.parse(mapping);
    final boolean sourceEnabled = ctx.read("$.message._source.enabled");
    assertThat(sourceEnabled).isFalse();
    final String messageField = ctx.read("$.message.properties.message.index");
    assertThat(messageField).isEqualTo("not_analyzed");
    // Clean up
    final DeleteIndexTemplateResponse deleteResponse = client.prepareDeleteTemplate(customTemplateName).get();
    assertThat(deleteResponse.isAcknowledged()).isTrue();
    indices.delete(testIndexName);
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) ReadContext(com.jayway.jsonpath.ReadContext) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) Test(org.junit.Test)

Example 15 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project graylog2-server by Graylog2.

the class IndicesTest method testCreateEnsuresIndexTemplateExists.

@Test
public void testCreateEnsuresIndexTemplateExists() throws Exception {
    final String templateName = indexSetConfig.indexTemplateName();
    final IndicesAdminClient client = this.client.admin().indices();
    final GetIndexTemplatesRequest request = client.prepareGetTemplates(templateName).request();
    final GetIndexTemplatesResponse responseBefore = client.getTemplates(request).actionGet();
    assertThat(responseBefore.getIndexTemplates()).isEmpty();
    indices.create("index_template_test", indexSet);
    final GetIndexTemplatesResponse responseAfter = client.getTemplates(request).actionGet();
    assertThat(responseAfter.getIndexTemplates()).hasSize(1);
    final IndexTemplateMetaData templateMetaData = responseAfter.getIndexTemplates().get(0);
    assertThat(templateMetaData.getName()).isEqualTo(templateName);
    assertThat(templateMetaData.getMappings().keysIt()).containsExactly(IndexMapping.TYPE_MESSAGE);
    final DeleteIndexTemplateRequest deleteRequest = client.prepareDeleteTemplate(templateName).request();
    final DeleteIndexTemplateResponse deleteResponse = client.deleteTemplate(deleteRequest).actionGet();
    assertThat(deleteResponse.isAcknowledged()).isTrue();
    indices.delete("index_template_test");
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) Test(org.junit.Test)

Aggregations

GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)35 Test (org.junit.Test)18 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)11 PartitionName (io.crate.metadata.PartitionName)10 BytesRef (org.apache.lucene.util.BytesRef)10 GetSettingsResponse (org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 Map (java.util.Map)5 GetIndexTemplatesRequest (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest)5 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)5 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)5 HashMap (java.util.HashMap)4 Alias (org.elasticsearch.action.admin.indices.alias.Alias)4 XContentType (org.elasticsearch.common.xcontent.XContentType)4 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)3 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)3 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)3 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)2 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)2 DeleteIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2