2

For example I want to create time based index. In elastic search we can achieve this by creating a pattern.

    curl -XPUT 'localhost:9200/_template/indextemplate' -d '{
      "template": "dynamicIndex-*",
      "order": 0,
      "settings": {
        "index": {
          "number_of_shards": 2,
          "number_of_replicas": 2
        }
      }
    }

In the above example the settings and mappings will apply on "dynamicIndex-". So now I can create weekly indexes like dynamicIndex-1, dyanmicIndex-2. How can I achieve this using spring data(How create/set the index template in using spring data).

2

2 Answers 2

3

You can use the PutIndexTemplateRequest to create a template in elasticsearch, using a java client. This creates a generic template which will be applied to all index as specified in the template source file.

`

  source = readFile(templatePath, StandardCharsets.UTF_8);
  PutIndexTemplateRequest request = new PutIndexTemplateRequest("template-name");
  request.source(source.getBytes());
  PutIndexTemplateResponse response = client.admin().indices().execute(PutIndexTemplateAction.INSTANCE, request)
      .get();

  if (!response.isAcknowledged()) {
    LOGGER.error("Error While Updating Template");
  } else {
    LOGGER.debug("Template Updated Successfully on the elasticsearch");
  }

`

Sign up to request clarification or add additional context in comments.

Comments

0

If you don't have a strict requirement to use spring-data-elasticsearch, then you can simply use native elasticsearch java client to start up a node and use it just as an standalone elasticsearch node.

And you can do these dynamic template configurations using that.

https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/node-client.html#node-client

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.