elasticsearch-手動設定_mapping中欄位型別及分詞器-ES5.X與ES6.X區別
【問題描述】:
手動設定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"