如何在elasticsearch2.3中使用json文件创建映射?

时间:2016-06-30 09:36:00

标签: elasticsearch mapping

我正在使用ES2.3

使用以下命令工作创建新的索引映射。

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

现在我希望可以使用json文件来创建相同的索引。 tmap.json文件如下所示

    {
    "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" ]
  }

然后我用卷曲来创造它。

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

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

以上两个命令都不起作用,得到如下错误。

{"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}%

如何用curl和我的json文件创建索引?这让我很长时间困惑。

任何人都可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:1)

您定义别名的方式是错误的。它应该是地图而不是数组。

  {
    "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": {} }
  }

有关索引创建中别名的更多信息: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#create-index-aliases