2
votes

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
any luck with defining dynamic templates with spring-data-elasticsearch ? - Rajind Ruparathna

2 Answers

3
votes

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");
  }

`

0
votes

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