Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于elasticsearch动态映射同名字段类型不一致的问题 #9

Open
DimonHo opened this issue Sep 8, 2017 · 1 comment
Open

Comments

@DimonHo
Copy link
Owner

DimonHo commented Sep 8, 2017

问题重现步骤(注意,在5.x之前的版本重现)

  1. 创建索引myindex并定义类型mytype1的mapping
PUT myindex
{
  "mappings": {
    "mytype1": {
      "properties": {
        "name": {
          "type": "integer"
        }
      }
    }
  }
}
  1. 向mytype1中插入一条数据
PUT myindex/mytype1/1
{
  "name":1234
}
  1. 向mytype2中插入一条字符串类型的数字,虽然mytype2我们没有事先定义,但它会动态映射并创建mytype2。
PUT myindex/mytype2/1
{
  "name":"1234"
}
  1. 向mytype3中插入一条超出int范围的数字。
PUT myindex/mytype3/1
{
  "name":123591823479123
}

查看索引的mapping就会发现,name字段在这三个type中分别被映射成了integer,string,long类型。

GET myindex/_mapping
  • 返回mapping结果:
{
  "myindex": {
    "mappings": {
      "mytype1": {
        "properties": {
          "name": {
            "type": "integer"
          }
        }
      },
      "mytype2": {
        "properties": {
          "name": {
            "type": "string"
          }
        }
      },
      "mytype3": {
        "properties": {
          "name": {
            "type": "long"
          }
        }
      }
    }
  }
}
  • 这样会有什么危害呢?

如果是简单的查询,你可能无法发现问题,但是如果你需要对这个字段排序或聚合,就会报数据类型转换异常。这是个很隐蔽的错误,如果不是刚好在这个字段排序或聚合,根本发现不了它。所以,如果是5.x之前的版本,如果存在多个type有同名字段的情况,最好先定义好字段类型一致,而不是用动态映射,动态映射无法保证字段类型的一致性。

但是这一切,在5.x版本后就不会再发生了,5.x插入数据之前就会做验证,如果索引中已存在该字段,不论是在哪个type中新增数据,都必须是已存在字段的类型或可以互相转换的类型。否则将新增数据失败

参考:

  1. nested: NumberFormatException[Invalid shift value in prefixCoded bytes (is encoded value really an INT?)] elastic/elasticsearch#9191
  2. Mapping updates should be synchronous elastic/elasticsearch#8688
@DimonHo
Copy link
Owner Author

DimonHo commented Sep 8, 2017

  • 在5.x中实验的结果截图
    qq 20170908152831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant