写点什么

Elasticsearch 定制 Dynamic Mapping 策略

用户头像
escray
关注
发布于: 2021 年 03 月 09 日
Elasticsearch 定制 Dynamic Mapping 策略

Elasticsearch Custom Dynamic Mapping,内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,英文内容来自 Elasticsearch: The Definitive Guide [2.x]。

date_detection


默认会按照一定格式识别 date,比如 yyyy-MM-dd。但是如果某个 field 先过来一个 2017-01-01 的值,就会被自动 dynamic mapping 成 date,后面如果再来一个"hello world"之类的值,就会报错。可以手动关闭某个 type 的 date_detection,如果有需要,自己手动指定某个 field 为 date 类型。


PUT /my_index/_mapping/my_type{    "date_detection": false}
复制代码
定制自己的 dynamic mapping template(type level)


PUT /my_index{    "mappings": {        "my_type": {            "dynamic_templates": [                { "en": {                      "match":              "*_en",                       "match_mapping_type": "string",                      "mapping": {                          "type":           "string",                          "analyzer":       "english"                      }                }}            ]				}		}}
PUT /my_index/my_type/1{  "title": "this is my first article"}
PUT /my_index/my_type/2{  "title_en": "this is my first article"}
复制代码


title 没有匹配到任何的 dynamic 模板,默认就是 standard 分词器,不会过滤停用词,is 会进入倒排索引,用 is 来搜索是可以搜索到的


title_en 匹配到了 dynamic 模板,就是 english 分词器,会过滤停用词,is 这种停用词就会被过滤掉,用 is 来搜索就搜索不到了

定制自己的 default mapping template(index level)


PUT /my_index{    "mappings": {        "_default_": {            "_all": { "enabled":  false }        },        "blog": {            "_all": { "enabled":  true  }        }    }}
复制代码


发布于: 2021 年 03 月 09 日阅读数: 20
用户头像

escray

关注

Let's Go 2017.11.19 加入

在学 Elasticsearch 的项目经理

评论

发布
暂无评论
Elasticsearch 定制 Dynamic Mapping 策略