6.0 的 mapping 变更有四条:
_all元字段默认禁用且 6.0+ 索引不能再配置(连带include_in_all禁用);boolean 字段的 coercion 只认true/false;动态模板里无法识别的match_mapping_type不再静默忽略而是抛异常;date 字段的locale会校验取值。前两条要分开排查——一个改变结果集却不报错,一个直接报错。本文按 v6.8.23 逐条给出原文,并补上match_mapping_type的合法取值清单(原文举的"拼错string"本身是合法值)。
说明
实测环境:本地起的 Elasticsearch 6.8.23 单节点实例(官方 tar 包直接跑,堆内存 512MB,机器学习关闭),curl 连本地 9200 端口。本文「实测」小节的命令和输出都出自这个实例。
来源和边界先交代:本文的依据出自 v6.8.23 标签下的官方文件(mappings.asciidoc、templates.asciidoc),文中引用的原文均来自这些文件。示例取自官方示例;哪些是文档写明、哪些是我的推断,正文里逐处标注。
以下均出自 6.0 迁移文档(migrate_6_0/mappings.asciidoc @ v6.8.23,全文 47 行,四条)。
图 1:官方文件 mappings.asciidoc(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 mappings.asciidoc
boolean 的 coercion 收紧
Previously, Elasticsearch recognized the strings
true,false,on,off,yes,no,0,1as booleans. Elasticsearch 6.0 recognizes only the stringstrueandfalseas booleans and will throw an error otherwise. For backwards compatibility purposes, during the 6.x series the previous coercion rules will continue to work on pre-6.0 indices. This means that you do not need to change affected existing mappings immediately. However, it is not possible to create new indices from existing index templates that violate the strictbooleancoercion rules.
(第 5–12 行)
三个要点:
- 6.0 之后创建的索引:只认
true/false,其他值报错; - 6.0 之前创建的索引:6.x 全系列继续按旧规则工作,不用立刻改;
- 索引模板:即使模板是旧的,只要违反新规则,就不能用它创建新索引。
第三条是最容易卡住的地方:索引本身没问题,等按模板滚动创建新索引时才报错——故障出现在几个月后的例行滚动上,而不是升级当天。滚动索引、按时间分索引这类场景尤其要注意。
处理方式:把模板里的 boolean 定义和写入端的数据一起改成 true/false。写入端改造是根本——数据源里用 Y/N、1/0、on/off 表示布尔的,要在写入前转换。兼容期是给你迁移的时间,不是让你继续用旧写法:宽松解析会积累脏数据,等要严格处理时库里什么格式都有,收紧是在止损,代价是先清理。
图 2:官方文件 templates.asciidoc(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 templates.asciidoc
_all 默认关闭
On new mappings, the
_allmeta field that contains a copy of the text from each field is now disabled by default. Thequery_stringandsimple_query_stringqueries that previously used_allto search will now check if_allis enabled/disabled and switch to executing the query across all fields if_allis disabled._allcan no longer be configured for indices created with Elasticsearch version 6.0 or later.
_all 是把所有字段的文本拷到一起形成的大字段,为"不指定字段就全文搜"这个用法服务。它的问题是索引体积翻倍,而且这个把内容混在一起的字段几乎没法做有意义的调优。
配套地,include_in_all 在 6.0+ 索引上被禁用:
Since the ++_all++ field is now disabled by default and cannot be configured for indices created with Elasticsearch 6.0 or later, the
include_in_allsetting is now disallowed for these indices' mappings.
关掉之后,查询语义变了
这条容易被当成"只是默认关了个字段",实际改变了 query_string 的行为:
| 5.x | 6.0 | |
|---|---|---|
query_string 不带字段时 | 打在 _all 上 | 先检查 _all,没启用就改成遍历所有字段 |
| 匹配范围 | _all 里拷进来的那些字段 | 全部字段(数值、日期等也参与) |
| 相关性 | 基于 _all 的评分 | 基于各字段的评分,排序会变 |
第三行是实际影响:同一个查询升级前后可能返回不同的顺序,甚至不同的命中集合。这类变化不报错,只表现为"搜索结果怎么和以前不一样了"。
替代做法:显式建组合字段
需要"跨字段搜索"的场景,用 copy_to 建一个自己的组合字段,并把 query_string 的 default_field 指向它:
PUT my_index
{
"mappings": {
"properties": {
"title": { "type": "text", "copy_to": "search_field" },
"body": { "type": "text", "copy_to": "search_field" },
"search_field": { "type": "text" }
}
}
}
字段集合明确(你知道搜的是哪几个字段)、可以单独调优分析器、索引体积也可控——比 _all 那种"全部拷一份"清楚得多。
未知 match_mapping_type 不再静默忽略
Previously Elasticsearch would silently ignore any dynamic templates that included a
match_mapping_typetype that was unrecognized. An exception is now thrown on an unrecognized type.
以前写错类型名,整条动态模板被静默忽略——该字段没按预期映射,而用户毫不知情。6.0 起抛异常。
这里要纠正一个常见误解:string 是合法值。官方列出的可自动检测类型是(templates.asciidoc 第 54–64 行):
boolean, date, double, long, object, string
外加 * 匹配所有类型。所以不能拿"写成 string"当错误示例。真正会踩的是这两类:
- 写了 JSON 检测不出来的类型名,比如
integer、float——因为 JSON 分不出long和integer、double和float,检测时总是取更宽的该(整数给long、带小数点的给double); - 纯拼写错误,比如
str、boolen。
排查时按上面那份合法清单逐个核对模板里的 match_mapping_type。
图 3:官方页面 boolean.asciidoc(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 docs/reference/mapping/types/boolean.asciidoc
date 字段的 locale 校验
The
localeoption ofdatefields previously allowed almost any string values, and unrecognized values would simply be ignored. Unrecognized values are now rejected on indices created with Elasticsearch 6.0 or newer.
同样是静默忽略 → 显式拒绝。locale 写错以前会被忽略、按默认 locale 解析,6.0+ 索引上直接报错。
两条主线要分开排查
这四条里,_all 关闭和 boolean 收紧影响面最大,但排查手段完全不同:
- 影响结果的(
_all关闭):要比对结果集。拿升级前后的同一批查询对比命中条数和顺序,少了就说明受影响。这个只能靠测,看报错永远查不到; - 影响能否执行的(boolean 严格解析、未知
match_mapping_type、非法locale):要看报错。这类在请求或建索引时就失败,日志里直接能看到,反而好查。
把两类搞混会浪费时间:用"看报错"的方式去查第一类永远查不到,用"比对结果"的方式去查第二类没必要。
关于 _all 还要强调一层:它是"以前能搜到、现在搜不到"的典型。这类问题没有任何错误提示,只有业务方反馈"搜索结果变少了"才会被发现,而那时可能已经过了很久。
结论是:凡是"放宽变收紧"或"默认关闭"的改动,升级后都要做一次结果集比对——报错你会立刻知道,结果变少不会。
升级 6.0 的检查清单
- 清点依赖
_all的查询(query_string/simple_query_string不带字段的用法),改成显式default_field或copy_to组合字段; - mapping 里删掉
include_in_all; - 检查所有索引模板的 boolean 定义与写入数据,统一成
true/false——重点排查滚动创建场景; - 动态模板里的
match_mapping_type按上面那份合法清单逐个核对(注意integer/float不合法); - date 字段的
locale取值核对(用标准 IETF BCP 47 语言标签,如zh-CN、en-US); - 6.0 之前创建的索引可以慢慢迁,但模板必须先改干净,否则新索引创建会失败;
- 升级后做一次结果集比对,重点看"不带字段的全文查询"。
在 6.8.23 上跑一遍
同一句 query_string,只换 default_field:
图 4:本地 6.8.23 单节点实例上的实测输出。上:指到真字段 t;下:指到 _all
curl -s -X POST "http://localhost:9200/t1/_search?filter_path=hits.total" \
-H 'Content-Type: application/json' \
-d '{"query":{"query_string":{"query":"hello","default_field":"t"}}}'
curl -s -X POST "http://localhost:9200/t1/_search?filter_path=hits.total" \
-H 'Content-Type: application/json' \
-d '{"query":{"query_string":{"query":"hello","default_field":"_all"}}}'
指到真字段返回 {"hits":{"total":1}};指到 _all 返回 {"hits":{"total":0}}。
两条都是 200,没有报错。 这就是 _all 默认关闭最难查的地方:它算不上查询失败,表现是"查得到 0 条"。搜索框没结果、告警规则不触发,看起来都像"最近没数据",没人会想到是字段没了。
另外验到 boolean 那条:把 "yes" 塞给 boolean 字段会直接报错(failed to create query),那一条是显式失败,反而好发现。
结论是:_all 这类"静默变空"的改动,升级后要用一组已知能命中的样例查询做冒烟测试,比对命中数;只看接口有没有报错是发现不了的。
写在最后
6.0 这四条 mapping 变更方向一致:把静默忽略改成显式拒绝(match_mapping_type、locale)、把宽松解析改成严格解析(boolean)、把一个隐式的大字段关掉(_all)。
我的建议是排查分两类走:会报错的那三条(boolean、未知类型、非法 locale)看日志就行,一天能改完;_all 那条不报错,只能靠升级前后的结果集比对发现,而它恰恰是最容易被业务方先感知的一条。
参考
本文对照 Elasticsearch
v6.8.23标签的官方文档核对,引文逐字取自原文。图 4 的两次 query_string 来自本地 6.8.23 单节点实例。
