如何在Elasticsearch中创建多型索引?

时间:2019-04-18 09:16:42

标签: elasticsearch elasticsearch-6

Elasticsearch文档中的几页中提到了如何查询多类型索引。 但我未能首先创建一个。

这是我的最小示例(在Elasticsearch 6.x服务器上):

PUT /myindex
{
  "settings" : {
    "number_of_shards" : 1
  }
}

PUT /myindex/people/123
{
  "first name": "John",
  "last name": "Doe"
}

PUT /myindex/dog/456
{
  "name": "Rex"
}

索引创建和拳头插入效果很好,但是尝试狗类型插入:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [myindex] as the final mapping would have more than 1 type: [people, dog]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Rejecting mapping update to [myindex] as the final mapping would have more than 1 type: [people, dog]"
  },
  "status": 400
}

但这正是我想要做的,伙计!我的索引中包含“ 1种以上”。

您知道要实现这个目标我必须改变什么?

非常感谢。

1 个答案:

答案 0 :(得分:0)

从Elastic 6.0.0起不支持多种映射类型。有关详细信息,请参见breaking changes

您仍然可以通过实现自己的自定义类型字段来有效地使用多种类型。

例如:

{
    "mappings": {
        "doc": {
            "properties": {
                "type": {
                    "type": "keyword"
                },
                "first_name": {
                    "type": "text"
                },
                "last_name": {
                    "type": "text"
                }

            }
        }
    }
}

这在removal of types中有描述。