Skip to content

Commit

Permalink
Update IndexTemplates
Browse files Browse the repository at this point in the history
Signed-off-by: Russ Cam <[email protected]>
  • Loading branch information
russcam committed Oct 25, 2023
1 parent dd87d7d commit 98a4fda
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

package org.opensearch.client.samples;

import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch.cluster.PutComponentTemplateRequest;
import org.opensearch.client.opensearch.indices.*;

import java.util.List;

/**
* Run with: <c>./gradlew :samples:run -Dsamples.mainClass=IndexTemplates</c>
*/
Expand All @@ -25,21 +24,19 @@ public class IndexTemplates {
public static void main(String[] args) {
try {
var client = SampleClient.create();

var version = client.info().version();
LOGGER.info("Server: {}@{}", version.distribution(), version.number());

final var indexTemplateName = "my-index-template";

if (!client.indices().existsIndexTemplate(t -> t.name(indexTemplateName)).value()) {
if (client.indices().existsIndexTemplate(t -> t.name(indexTemplateName)).value()) {
DeleteIndexTemplateRequest deleteIndexTemplateRequest = DeleteIndexTemplateRequest.of(i -> i.name(indexTemplateName));

LOGGER.info("Deleting index template {}", indexTemplateName);
client.indices().deleteIndexTemplate(deleteIndexTemplateRequest);
}

// Create an index template composed of two component templates, one for index settings, and one for mappings
String indexSettingsComponentTemplate = "index-settings";
final var indexSettingsComponentTemplate = "index-settings";
PutComponentTemplateRequest putComponentTemplateRequest = PutComponentTemplateRequest.of(
c -> c.name(indexSettingsComponentTemplate)
.settings(
Expand All @@ -60,7 +57,7 @@ public static void main(String[] args) {
LOGGER.info("Creating component template {}", indexSettingsComponentTemplate);
client.cluster().putComponentTemplate(putComponentTemplateRequest);

String indexMappingsComponentTemplate = "index-mappings";
final var indexMappingsComponentTemplate = "index-mappings";
putComponentTemplateRequest = PutComponentTemplateRequest.of(
c -> c.name(indexMappingsComponentTemplate).mappings(m -> m.properties("age", p -> p.integer(i -> i)))
);
Expand All @@ -76,7 +73,7 @@ public static void main(String[] args) {
LOGGER.info("Creating index template {}", indexTemplateName);
client.indices().putIndexTemplate(putIndexTemplateRequest);

String indexName = "my-index-1";
final var indexName = "my-index-1";
if (client.indices().exists(r -> r.index(indexName)).value()) {
LOGGER.info("Deleting index {}", indexName);
client.indices().delete(DeleteIndexRequest.of(d -> d.index(indexName)));
Expand Down

0 comments on commit 98a4fda

Please sign in to comment.