How is a template used while creating an index with Java API

Hi,

I'm trying to create an index which is going to have a common structure every time it'll be recreated.
I've created a template on ES and want to use it while creating and populating the index via Java program.
How can an index template be used while creating an index from Java API.

Index template

PUT _template/quick-search
{
  "index_patterns": ["quick-search*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {

    "item" : {
        "type" : "long"
      },
      "description" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "id" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}
`

Template is just a way for ES to populate "mappings" for that index at creation time.
When you issue the API to either create a new index (or populate the first entry to a new index), ES will add the mappings according to your template.

It's the same thing with a "create" API with mappings. If you use template, your code doesn't need to add the mappings.

It's a preference imo. With templates, you need to probe ES to see what's going on.
With code, you can easily see what is the mappings without accessing the servers.
The downside with code is if somebody accidentally insert a document to a new index, that index will have the default mappings. Your subsequent "create" will have return status "index already exist" and ended up with the wrong mappings.

But with template, an accidental document insert will cause ES to use the mappings of the template. Again, it's a preference. Template will hide bad code logic since you will not know there's such accidental/bad document insertion before "create" API is invoked.

Thanks,
Is there any Java API with elasticsearch using which I can create index after getting index template from ES cluster, I could not find it in the ES documentation.
For example : like it is done on SOLR while creating a collection in following snippet?

CollectionAdminRequest.createCollection(collection,template,numShards,numReplicas)

I don't understand your question.
Template is something you configured (either API or kibana) once.
There's a wildcard field that specifies which index pattern to apply this mappings to.

I have never used solr, so I can't draw the similarities. Sorry.

In your example, any index names start with "quick-search" will have this template applies to.
quick-search1, quick-searchabc, quick-search-alkfsdja, etc.

Thanks for your time.
I just wanted to know if the template created on Kibana can be used via Java API while creating a new index and if yes, how?
I don't see an option to pass existing template while creating an index with CreateIndexRequest class.

I think you misunderstood.
Creating template is a separate API/action. Independent from "index creation" or "document insert" APIs.

Think of it as a cluster configuration.
For this cluster, all indices with such pattern will have this template applied automatically. It's a configuration.

Regardless from kibana or API, this configuration (template) exists for this cluster forever. Until you change it of course.

Thanks for the explanation !

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.