1

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
          }
        }
      }
    }
  }
}

1 Answer 1

1

Since you've added an index template, if you now try to save a document on an index that does not exist and it has a name that will match the pattern that you provided in the template "index_patterns": ["quick-search*"], elasticsearch will automatically create the mapping as per the template instead of creating a mapping based on your input.

Moreover if you try to create a new index and try to set the mapping (again that matches the pattern), the template acts as the default. So for a new index, you can set the type as keyword for item. This will override the default of long of the template, but the other settings will be taken from the template and will be present as well.

In order to test go to kibana and set your 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
          }
        }
      }
    }
  }
}

Then try to create a new index, but item needs to be a keyword

PUT quick-search-item-keyword
{
  "mappings": {
    "properties": {
      "item": {
        "type": "keyword",
        "index": true
      }
    }
  }
}

GET quick-search-item-keyword/_mapping

And now just save a document in an index that doesn't exist

POST quick-search-test-id/_doc/
{
  "id": "test"
}

GET quick-search-test-id/_mapping

Try to save the same document in an index that doesn't exist and doesn't match the index pattern of your template

POST test/_doc/
{
  "id": "test"
}

GET test/_mapping

There is nothing different if you use the java client. The same will apply

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

2 Comments

I did but it doesn't register my vote for resolution. Shows following message :"Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score."
Thank you for trying to upvote. Upvoting is different than marking the answer. You tick the answer in order to mark resolution.

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.