use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testSimpleIndexTemplateTests.
public void testSimpleIndexTemplateTests() throws Exception {
// clean all templates setup by the framework.
client().admin().indices().prepareDeleteTemplate("*").get();
// check get all templates on an empty index.
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), empty());
client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).setSettings(indexSettings()).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("template_2").setPatterns(Collections.singletonList("test*")).setSettings(indexSettings()).setOrder(1).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field2").field("type", "text").field("store", false).endObject().endObject().endObject().endObject()).get();
// test create param
assertThrows(client().admin().indices().preparePutTemplate("template_2").setPatterns(Collections.singletonList("test*")).setSettings(indexSettings()).setCreate(true).setOrder(1).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field2").field("type", "text").field("store", false).endObject().endObject().endObject().endObject()), IllegalArgumentException.class);
response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), hasSize(2));
// index something into test_index, will match on both templates
client().prepareIndex("test_index", "type1", "1").setSource("field1", "value1", "field2", "value 2").setRefreshPolicy(IMMEDIATE).get();
ensureGreen();
SearchResponse searchResponse = client().prepareSearch("test_index").setQuery(termQuery("field1", "value1")).addStoredField("field1").addStoredField("field2").execute().actionGet();
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).field("field1").getValue().toString(), equalTo("value1"));
// field2 is not stored.
assertThat(searchResponse.getHits().getAt(0).field("field2"), nullValue());
client().prepareIndex("text_index", "type1", "1").setSource("field1", "value1", "field2", "value 2").setRefreshPolicy(IMMEDIATE).get();
ensureGreen();
// now only match on one template (template_1)
searchResponse = client().prepareSearch("text_index").setQuery(termQuery("field1", "value1")).addStoredField("field1").addStoredField("field2").execute().actionGet();
if (searchResponse.getFailedShards() > 0) {
logger.warn("failed search {}", Arrays.toString(searchResponse.getShardFailures()));
}
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).field("field1").getValue().toString(), equalTo("value1"));
assertThat(searchResponse.getHits().getAt(0).field("field2").getValue().toString(), equalTo("value 2"));
}
use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testCombineTemplates.
public void testCombineTemplates() throws Exception {
// clean all templates setup by the framework.
client().admin().indices().prepareDeleteTemplate("*").get();
// check get all templates on an empty index.
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), empty());
//Now, a complete mapping with two separated templates is error
// base template
client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("*")).setSettings(" {\n" + " \"index\" : {\n" + " \"analysis\" : {\n" + " \"analyzer\" : {\n" + " \"custom_1\" : {\n" + " \"tokenizer\" : \"whitespace\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n", XContentType.JSON).get();
// put template using custom_1 analyzer
MapperParsingException e = expectThrows(MapperParsingException.class, () -> client().admin().indices().preparePutTemplate("template_2").setPatterns(Collections.singletonList("test*")).setCreate(true).setOrder(1).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field2").field("type", "text").field("analyzer", "custom_1").endObject().endObject().endObject().endObject()).get());
assertThat(e.getMessage(), containsString("analyzer [custom_1] not found for field [field2]"));
response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), hasSize(1));
}
use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testThatGetIndexTemplatesWithSimpleRegexWorks.
public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception {
logger.info("--> put template_1");
client().admin().indices().preparePutTemplate("template_1").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()).execute().actionGet();
logger.info("--> put template_2");
client().admin().indices().preparePutTemplate("template_2").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()).execute().actionGet();
logger.info("--> put template3");
client().admin().indices().preparePutTemplate("template3").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()).execute().actionGet();
logger.info("--> get template template_*");
GetIndexTemplatesResponse getTemplate1Response = client().admin().indices().prepareGetTemplates("template_*").execute().actionGet();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(2));
List<String> templateNames = new ArrayList<>();
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
assertThat(templateNames, containsInAnyOrder("template_1", "template_2"));
logger.info("--> get all templates");
getTemplate1Response = client().admin().indices().prepareGetTemplates("template*").execute().actionGet();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(3));
templateNames = new ArrayList<>();
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
templateNames.add(getTemplate1Response.getIndexTemplates().get(2).name());
assertThat(templateNames, containsInAnyOrder("template_1", "template_2", "template3"));
logger.info("--> get templates template_1 and template_2");
getTemplate1Response = client().admin().indices().prepareGetTemplates("template_1", "template_2").execute().actionGet();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(2));
templateNames = new ArrayList<>();
templateNames.add(getTemplate1Response.getIndexTemplates().get(0).name());
templateNames.add(getTemplate1Response.getIndexTemplates().get(1).name());
assertThat(templateNames, containsInAnyOrder("template_1", "template_2"));
}
use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testInvalidSettings.
public void testInvalidSettings() throws Exception {
// clean all templates setup by the framework.
client().admin().indices().prepareDeleteTemplate("*").get();
// check get all templates on an empty index.
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), empty());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).setSettings(Settings.builder().put("does_not_exist", "test")).get());
assertEquals("unknown setting [index.does_not_exist] please check that any required plugins are" + " installed, or check the breaking changes documentation for removed settings", e.getMessage());
response = client().admin().indices().prepareGetTemplates().get();
assertEquals(0, response.getIndexTemplates().size());
createIndex("test");
GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings("test").get();
assertNull(getSettingsResponse.getIndexToSettings().get("test").getAsMap().get("index.does_not_exist"));
}
use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project elasticsearch by elastic.
the class SharedClusterSnapshotRestoreIT method testRestoreTemplates.
public void testRestoreTemplates() throws Exception {
Client client = client();
logger.info("--> creating repository");
assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())));
logger.info("--> creating test template");
assertThat(client.admin().indices().preparePutTemplate("test-template").setPatterns(Collections.singletonList("te*")).addMapping("test-mapping", XContentFactory.jsonBuilder().startObject().startObject("test-mapping").startObject("properties").startObject("field1").field("type", "text").field("store", true).endObject().startObject("field2").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject()).get().isAcknowledged(), equalTo(true));
logger.info("--> snapshot");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setIndices().setWaitForCompletion(true).get();
assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(0));
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(0));
assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
logger.info("--> delete test template");
assertThat(client.admin().indices().prepareDeleteTemplate("test-template").get().isAcknowledged(), equalTo(true));
GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
logger.info("--> restore cluster state");
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
// We don't restore any indices here
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
logger.info("--> check that template is restored");
getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
}
Aggregations