简体   繁体   中英

Elasticsearch template not creating new index in elasticsearch 1.7

I have created a index template and inserted it in my local elasticsearch store. Using the following command:

 curl -XPUT localhost:9200/_template/media_template -d ' { "template" : "media_*", "mappings" : { "properties": { "media-type": { "type": "string" } } } }

I have installed Elasticsearch-head and can go into the Info>Templates and see that my template was indeed created. I am under the assumption that once I have a template then I can insert into any index with a name that fits within the regex of media_* even if that index does not yet exist. I want to be able to auto create indexes using the index template.

When I attempt to insert a record into an index that is not yet created, but is a valid regex of media_* , I get an error. Below is the insert statement that I call and after that is the error.

 $ curl -XPOST 'http://localhost:9200/media_2016_03_25/1' -d ' { "media-type" : "audio" } '

Error:

 { "error": "MapperParsingException[mapping [properties]]; nested: MapperParsingException[Root type mapping not empty after parsing! Remaining fields: [media-type : {type=string}]]; ", "status": 400 }

What am I doing wrong? Am I misunderstanding index templates? Should they be able to auto create the index if it does not exist and is compliant with the template specification?

I am running elasticsearch 1.7

you need to specify which type you are applying your mapping to and what's the type of your document when you are creating it.

Try this :

curl -XPUT localhost:9200/_template/media_template -d '
{
    "template" : "media_*",
    "mappings" : {
        "my-document-type" : {
            "properties": {
               "media-type": {
                    "type": "string"
                }
            }
        }
    }
}

Then this to create your document :

$ curl -XPOST 'http://localhost:9200/media_2016_03_25/my-document-type/1' -d '
{
    "media-type" : "audio"
}
'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM