@runzhliu
2018-01-25T02:06:48.000000Z
字数 1015
阅读 1295
elasticsearch keyword text
关于 Elasticsearch 有很多中文博客多有介绍 keyword 和 text 的区别,基本都是源自于官方文档,笔者在这里把官方社区中的博客贴出来,并作简单翻译,供其他人参考。
在 Elasticsearch 5.0 发布后,string 类型被正式去除。原因在于该类型总是会制造很多混乱,因为在 Elasticsearch 中有两种不同的方式来检索字符,既可以搜索全字符,又可以搜索词项 token。而一般前者的 mapping 是需要加上 not_analyzed,而后者是需要 analyzed。这一特性保留在了 Elasticsearch 6.1 当中。
为了避免这些误会,string 类型被分为 text 和 keyword 两种类型,前者作为全文检索,后者是关键词检索的类型。
而默认的字符串动态 mapping 也做了修改,字符串会被动态的以 text 和 keyword 两种类型被索引。
如果需要索引以下字段:
{"foo": "bar"}
动态 mapping 的效果为:
{"foo": {"type" "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}
当然你可以采用显示的方式来定义 mapping 以避免同时被索引成两种类型。
下面展示的是如何索引文档的字符型字段成需要的类型。
针对以前需要 analyzed 的 string 字段,可以简单的使用 text 和 index: true 来设定。
{"foo": {"type" "string","index": "analyzed"}}
{"foo": {"type" "text","index": true}}
针对以前需要 not_analyzed 的 string 字段,可以简单使用 keyword 和 index: true 来设定。
{"foo": {"type" "string","index": "not_analyzed"}}
{"foo": {"type" "keyword","index": true}}
