为什么 Elasticsearch 中的字段是按类型指定的,而不是按索引指定的?

Why are fields specified by type rather than index in Elasticsearch?

如果 Elasticsearch 索引中的多个类型具有同名字段,则这些字段必须具有相同的映射以尝试创建 "foobar" 属性 作为字符串和长"...

例如,如果您尝试 PUT 以下索引映射:

{
  "mappings": {
    "type_one": {
      "properties": {
        "foobar": { 
          "type": "string"
        }
      }
    },
    "type_two": {
      "properties": {
        "foobar": { 
          "type": "long"
        }
      }
    }
  }
}

...会返回如下错误

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Failed to parse mapping [type_one]: mapper [foobar] cannot be changed from type [long] to [string]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [type_one]: mapper [foobar] cannot be changed from type [long] to [string]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "mapper [foobar] cannot be changed from type [long] to [string]"
    }
  },
  "status": 400
}

以下内容来自elasticsearch网站:

Conflicts between fields in different types

Fields in the same index with the same name in two different types must have the same mapping, as they are backed by the same field internally. Trying to update a mapping parameter for a field which exists in more than one type will throw an exception, unless you specify the update_all_types parameter, in which case it will update that parameter across all fields with the same name in the same index.

如果具有相同名称的字段必须对索引中的所有类型具有相同的映射,那么为什么要为每个类型指定字段映射?为什么不指定整个索引的字段,然后确定每个类型分配哪些字段。

例如这样的事情:

{
   "fields":{
      "PropA":{
         "type":"string"
      },
      "PropB":{
         "type":"long"
      },
      "PropC":{
         "type":"boolean"
      }
   },
   "types":{
      "foo":[
         "PropA",
         "PropB"
      ],
      "foo":[
         "PropA",
         "PropC"
      ],
      "foo":[
         "PropA",
         "PropC",
         "PropC"
      ]
   }
}

像这样的映射格式会不会更简洁,更好地表示实际允许的内容?

我问的原因是因为我正在创建一个索引模板 JSON 文件,其中包含 15 种类型的大约 80 个不同字段。许多字段用于多个(如果不是所有)类型。因此,无论何时我需要更新字段,我都必须确保为使用它的每种类型更新它。

看来我不是唯一觉得这令人困惑的人。

Remove support for types? #15613

听起来像删除对每个索引的多种类型的支持并在索引级别指定字段是在 roadmap for future versions