【问题描述】:
手动设置mapping,给指定字段设置是否进行分词,以及分词器类型。
环境信息:学习elasticsearch时,案例使用的是5.x版本,自己本地使用的是6.x版本。
案例如下,5.x版本编辑:
PUT /website
{
"mappings": {
"article": {
"properties": {
"my_field1": {
"type": "text",
"analyzer": "english"
},
"my_field2": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
【定为分析】:
在使用6.x编辑时,报错,不存在string类型,且“index”值类型为boolean
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "No handler for type [string] declared on field [my_field2]"
}
],
"type": "mapper_parsing_exception",
"reason": "No handler for type [string] declared on field [my_field2]"
},
"status": 400
}
思路一:考虑版本差异。
【问题根因】:
版本差异问题,在5.x中type类型支持text/string, 但是6.x以后只使用text类型。
5.x中设置字段不进行分词使用"type":"string","index":"not_analyzed",而在6.x中设置字段使用"type":"keyword"
案例6.x如下:
PUT /website/_mapping/article
{
"properties":{
"my_field2":{
"type": "keyword"
}
}
}
【解决方案】:
使用"type":"keyword"