Search in sources :

Example 1 with PutTemplateRequest

use of org.springframework.data.elasticsearch.core.index.PutTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveIndexOperationsTest method shouldCheckExists.

// DATAES-612
@Test
void shouldCheckExists() {
    ReactiveIndexOperations indexOps = operations.indexOps(Entity.class);
    String templateName = "template" + UUID.randomUUID().toString();
    ExistsTemplateRequest existsTemplateRequest = new ExistsTemplateRequest(templateName);
    boolean exists = indexOps.existsTemplate(existsTemplateRequest).block();
    assertThat(exists).isFalse();
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder(templateName, "log-*").withOrder(// 
    11).withVersion(// 
    42).build();
    boolean acknowledged = indexOps.putTemplate(putTemplateRequest).block();
    assertThat(acknowledged).isTrue();
    exists = indexOps.existsTemplate(existsTemplateRequest).block();
    assertThat(exists).isTrue();
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) ExistsTemplateRequest(org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 2 with PutTemplateRequest

use of org.springframework.data.elasticsearch.core.index.PutTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveIndexOperationsTest method shouldGetTemplate.

// DATAES-612
@Test
void shouldGetTemplate() throws JSONException {
    ReactiveIndexOperations indexOps = operations.indexOps(Entity.class);
    org.springframework.data.elasticsearch.core.document.Document mapping = indexOps.createMapping(TemplateClass.class).block();
    Settings settings = indexOps.createSettings(TemplateClass.class).block();
    AliasActions aliasActions = new AliasActions(new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias1", "alias2").build()));
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder("test-template", "log-*").withSettings(// 
    settings).withMappings(// 
    mapping).withAliasActions(// 
    aliasActions).withOrder(// 
    11).withVersion(// 
    42).build();
    Boolean acknowledged = indexOps.putTemplate(putTemplateRequest).block();
    assertThat(acknowledged).isTrue();
    GetTemplateRequest getTemplateRequest = new GetTemplateRequest(putTemplateRequest.getName());
    TemplateData templateData = indexOps.getTemplate(getTemplateRequest).block();
    assertThat(templateData).isNotNull();
    assertThat(templateData.getIndexPatterns()).containsExactlyInAnyOrder(putTemplateRequest.getIndexPatterns());
    assertEquals(settings.toJson(), templateData.getSettings().toJson(), false);
    assertEquals(mapping.toJson(), templateData.getMapping().toJson(), false);
    Map<String, AliasData> aliases = templateData.getAliases();
    assertThat(aliases).hasSize(2);
    AliasData alias1 = aliases.get("alias1");
    assertThat(alias1.getAlias()).isEqualTo("alias1");
    AliasData alias2 = aliases.get("alias2");
    assertThat(alias2.getAlias()).isEqualTo("alias2");
    assertThat(templateData.getOrder()).isEqualTo(putTemplateRequest.getOrder());
    assertThat(templateData.getVersion()).isEqualTo(putTemplateRequest.getVersion());
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) TemplateData(org.springframework.data.elasticsearch.core.index.TemplateData) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) GetTemplateRequest(org.springframework.data.elasticsearch.core.index.GetTemplateRequest) Settings(org.springframework.data.elasticsearch.core.index.Settings) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 3 with PutTemplateRequest

use of org.springframework.data.elasticsearch.core.index.PutTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveIndexOperationsTest method shouldDeleteTemplate.

// DATAES-612
@Test
void shouldDeleteTemplate() {
    ReactiveIndexOperations indexOps = operations.indexOps(Entity.class);
    String templateName = "template" + UUID.randomUUID().toString();
    ExistsTemplateRequest existsTemplateRequest = new ExistsTemplateRequest(templateName);
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder(templateName, "log-*").withOrder(// 
    11).withVersion(// 
    42).build();
    boolean acknowledged = indexOps.putTemplate(putTemplateRequest).block();
    assertThat(acknowledged).isTrue();
    boolean exists = indexOps.existsTemplate(existsTemplateRequest).block();
    assertThat(exists).isTrue();
    acknowledged = indexOps.deleteTemplate(new DeleteTemplateRequest(templateName)).block();
    assertThat(acknowledged).isTrue();
    exists = indexOps.existsTemplate(existsTemplateRequest).block();
    assertThat(exists).isFalse();
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) ExistsTemplateRequest(org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest) DeleteTemplateRequest(org.springframework.data.elasticsearch.core.index.DeleteTemplateRequest) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 4 with PutTemplateRequest

use of org.springframework.data.elasticsearch.core.index.PutTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class RequestFactoryTests method shouldCreatePutIndexTemplateRequest.

@Test
// DATAES-612
void shouldCreatePutIndexTemplateRequest() throws JSONException, IOException {
    String expected = // 
    "{\n" + // 
    "  \"index_patterns\": [\n" + // 
    "    \"test-*\"\n" + // 
    "  ],\n" + // 
    "  \"order\": 42,\n" + // 
    "  \"version\": 7,\n" + // 
    "  \"settings\": {\n" + // 
    "    \"index\": {\n" + // 
    "      \"number_of_replicas\": \"2\",\n" + // 
    "      \"number_of_shards\": \"3\",\n" + // 
    "      \"refresh_interval\": \"7s\",\n" + // 
    "      \"store\": {\n" + // 
    "        \"type\": \"oops\"\n" + // 
    "      }\n" + // 
    "    }\n" + // 
    "  },\n" + // 
    "  \"mappings\": {\n" + // 
    "    \"properties\": {\n" + // 
    "      \"price\": {\n" + // 
    "        \"type\": \"double\"\n" + // 
    "      }\n" + // 
    "    }\n" + // 
    "  },\n" + // 
    "  \"aliases\":{\n" + // 
    "    \"alias1\": {},\n" + // 
    "    \"alias2\": {},\n" + // 
    "    \"alias3\": {\n" + // 
    "      \"routing\": \"11\"\n" + // 
    "    }\n" + // 
    "  }\n" + // 
    "}\n";
    org.springframework.data.elasticsearch.core.document.Document settings = org.springframework.data.elasticsearch.core.document.Document.create();
    settings.put("index.number_of_replicas", 2);
    settings.put("index.number_of_shards", 3);
    settings.put("index.refresh_interval", "7s");
    settings.put("index.store.type", "oops");
    org.springframework.data.elasticsearch.core.document.Document mappings = org.springframework.data.elasticsearch.core.document.Document.parse("{\"properties\":{\"price\":{\"type\":\"double\"}}}");
    AliasActions aliasActions = new AliasActions(new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias1", "alias2").build()), new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias3").withRouting("11").build()));
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder("test-template", "test-*").withSettings(// 
    settings).withMappings(// 
    mappings).withAliasActions(// 
    aliasActions).withOrder(// 
    42).withVersion(// 
    7).build();
    PutIndexTemplateRequest putIndexTemplateRequest = requestFactory.putIndexTemplateRequest(putTemplateRequest);
    String json = requestToString(putIndexTemplateRequest);
    assertEquals(expected, json, false);
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Test(org.junit.jupiter.api.Test)

Example 5 with PutTemplateRequest

use of org.springframework.data.elasticsearch.core.index.PutTemplateRequest in project spring-data-elasticsearch by spring-projects.

the class ReactiveIndexTemplate method putTemplate.

// endregion
// region templates
@Override
public Mono<Boolean> putTemplate(PutTemplateRequest putTemplateRequest) {
    Assert.notNull(putTemplateRequest, "putTemplateRequest must not be null");
    PutIndexTemplateRequest putIndexTemplateRequest = requestFactory.putIndexTemplateRequest(putTemplateRequest);
    return Mono.from(operations.executeWithIndicesClient(client -> client.putTemplate(putIndexTemplateRequest)));
}
Also used : AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) GetMappingsRequest(org.elasticsearch.client.indices.GetMappingsRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Settings(org.springframework.data.elasticsearch.core.index.Settings) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ElasticsearchPersistentEntity(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity) Mapping(org.springframework.data.elasticsearch.annotations.Mapping) ElasticsearchConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) Map(java.util.Map) Nullable(org.springframework.lang.Nullable) Requests(org.elasticsearch.client.Requests) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) ReactiveMappingBuilder(org.springframework.data.elasticsearch.core.index.ReactiveMappingBuilder) TemplateData(org.springframework.data.elasticsearch.core.index.TemplateData) IndexTemplatesExistRequest(org.elasticsearch.client.indices.IndexTemplatesExistRequest) DeleteTemplateRequest(org.springframework.data.elasticsearch.core.index.DeleteTemplateRequest) ExistsTemplateRequest(org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) GetIndexTemplatesRequest(org.elasticsearch.client.indices.GetIndexTemplatesRequest) Set(java.util.Set) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) Mono(reactor.core.publisher.Mono) PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) Flux(reactor.core.publisher.Flux) GetTemplateRequest(org.springframework.data.elasticsearch.core.index.GetTemplateRequest) NoSuchIndexException(org.springframework.data.elasticsearch.NoSuchIndexException) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) Document(org.springframework.data.elasticsearch.core.document.Document) StringUtils(org.springframework.util.StringUtils) Assert(org.springframework.util.Assert) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest)

Aggregations

PutTemplateRequest (org.springframework.data.elasticsearch.core.index.PutTemplateRequest)6 Test (org.junit.jupiter.api.Test)5 AliasActions (org.springframework.data.elasticsearch.core.index.AliasActions)4 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)4 AliasAction (org.springframework.data.elasticsearch.core.index.AliasAction)3 ExistsTemplateRequest (org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest)3 Settings (org.springframework.data.elasticsearch.core.index.Settings)3 PutIndexTemplateRequest (org.elasticsearch.client.indices.PutIndexTemplateRequest)2 AliasData (org.springframework.data.elasticsearch.core.index.AliasData)2 DeleteTemplateRequest (org.springframework.data.elasticsearch.core.index.DeleteTemplateRequest)2 GetTemplateRequest (org.springframework.data.elasticsearch.core.index.GetTemplateRequest)2 TemplateData (org.springframework.data.elasticsearch.core.index.TemplateData)2 Map (java.util.Map)1 Set (java.util.Set)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)1 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)1 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)1 GetSettingsRequest (org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest)1