0

I'm using ES2.3

create new index mapping with below command works.

curl -XPUT 'http://localhost:9200/megacorp' -d '
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    }, 
    "mappings": {
        "employee": {
            "properties": {
                "first_name": {
                    "type": "string"
                }, 
                "last_name": {
                    "type": "string"
                }, 
                "age": {
                    "type": "integer"
                }, 
                "about": {
                    "type": "string"
                }, 
                "interests": {
                    "type": "string"
                }, 
                "join_time": {
                    "type": "date", 
                    "format": "dateOptionalTime", 
                    "index": "not_analyzed"
                }
            }
        }
    }
}
'

now i hope can use a json file to create same index. tmap.json file like below

    {
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    }, 
    "mappings": {
        "employee": {
            "properties": {
                "first_name": {
                    "type": "string"
                }, 
                "last_name": {
                    "type": "string"
                }, 
                "age": {
                    "type": "integer"
                }, 
                "about": {
                    "type": "string"
                }, 
                "interests": {
                    "type": "string"
                }, 
                "join_time": {
                    "type": "date", 
                    "format": "dateOptionalTime", 
                    "index": "not_analyzed"
                }
            }
        }
    },
    "aliases": [ "source" ]
  }

then i usr curl to create it.

curl -s -XPOST 'localhost:9200/megacorp' --data-binary @tmap.json

and

curl -XPUT 'http://localhost:9200/megacorp' -d @tmap.json

both above commands not working, get error like below.

{"error":{"root_cause":[{"type":"class_cast_exception","reason":"java.util.ArrayList cannot be cast to java.util.Map"}],"type":"class_cast_exception","reason":"java.util.ArrayList cannot be cast to java.util.Map"},"status":500}%

how to create index with curl and my json file? this is really confused me for long time.

can any body help me? thanks.

0

1 Answer 1

1

The way you define alias is wrong. It should be a map instead of an array.

  {
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    }, 
    "mappings": {
        "employee": {
            "properties": {
                "first_name": {
                    "type": "string"
                }, 
                "last_name": {
                    "type": "string"
                }, 
                "age": {
                    "type": "integer"
                }, 
                "about": {
                    "type": "string"
                }, 
                "interests": {
                    "type": "string"
                }, 
                "join_time": {
                    "type": "date", 
                    "format": "dateOptionalTime", 
                    "index": "not_analyzed"
                }
            }
        }
    },
    "aliases": { "source": {} }
  }

More info about aliases in index creation: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#create-index-aliases

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

3 Comments

i got it, thanks. other question, how to storage template file in config/templates folder, do you know it?
I do not understand your question. What do you mean by template file? I don't think Elasticsearch has something called template file.
thanks for your help. I found it from ES version 2.0 or newer will not supported for template files storage in local file, you have to use curl -XPUT localhost:9200/_template/xxx -d '{}' for your template. other will would be possible you storage it on local folder then run command by handle , like curl -XPUT localhost:9200/_template/xxx -d @templatefile

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.