原因:
ElasticSearch 7.x 默认不在支持指定索引类型
以下数据执行put请求:
{
"settings": {
"index": {
"number_of_shards": "2",
"number_of_replicas": "0"
}
},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"mail": {
"type": "keyword"
},
"hobby": {
"type": "text"
}
}
}
}
}
报错:
{
error: {
root_cause: [1]
0: {
type: "mapper_parsing_exception"
reason: "Root mapping definition has unsupported parameters: [person : {properties={mail={type=keyword}, name={type=text}, age={type=integer}, hobby={type=text}}}]"
}-
-
type: "mapper_parsing_exception"
reason: "Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [person : {properties={mail={type=keyword}, name={type=text}, age={type=integer}, hobby={type=text}}}]"
caused_by: {
type: "mapper_parsing_exception"
reason: "Root mapping definition has unsupported parameters: [person : {properties={mail={type=keyword}, name={type=text}, age={type=integer}, hobby={type=text}}}]"
}-
}-
status: 400
}
但是对于ElasticSearch 6.x执行时没有问题的,elasticsearch7默认不在支持指定索引类型,默认索引类型是_doc。 官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html
所以在ElasticSearch 7.x中不指定索引类型,创建索引是成功的:
{
"settings": {
"index": {
"number_of_shards": "2",
"number_of_replicas": "0"
}
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"mail": {
"type": "keyword"
},
"hobby": {
"type": "text"
}
}
}
}
插入数据:
POST http://127.0.0.1:9200/itcast/_bulk
{"index":{"_index":"itcast","_type":"person"}}
{"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
{"index":{"_index":"itcast","_type":"person"}}
{"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
{"index":{"_index":"itcast","_type":"person"}}
{"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
{"index":{"_index":"itcast","_type":"person"}}
{"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳"}
{"index":{"_index":"itcast","_type":"person"}}
{"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影"}
出错提示:
mapper [mail] cannot be changed from type [keyword] to [text]
{
took: 188
errors: true
items: [5]
0: {
index: {
_index: "itcast"
_type: "person"
_id: "RxXQ2HUBJ5PULoSjkfd5"
status: 400
error: {
type: "illegal_argument_exception"
reason: "mapper [mail] cannot be changed from type [keyword] to [text]"
}-
}-
}-
1: {
index: {
_index: "itcast"
_type: "person"
_id: "SBXQ2HUBJ5PULoSjkfd-"
status: 400
error: {
type: "illegal_argument_exception"
reason: "mapper [mail] cannot be changed from type [keyword] to [text]"
}-
}-
}-
2: {
index: {
_index: "itcast"
_type: "person"
_id: "SRXQ2HUBJ5PULoSjkfd-"
status: 400
error: {
type: "illegal_argument_exception"
reason: "mapper [mail] cannot be changed from type [keyword] to [text]"
}-
}-
}-
3: {
index: {
_index: "itcast"
_type: "person"
_id: "ShXQ2HUBJ5PULoSjkfd-"
status: 400
error: {
type: "illegal_argument_exception"
reason: "mapper [mail] cannot be changed from type [keyword] to [text]"
}-
}-
}-
4: {
index: {
_index: "itcast"
_type: "person"
_id: "SxXQ2HUBJ5PULoSjkfd-"
status: 400
error: {
type: "illegal_argument_exception"
reason: "mapper [mail] cannot be changed from type [keyword] to [text]"
}-
}-
}-
-
}
因为ElasticSearch 7.x 不再支持type。
修改语句如下:
{"index":{"_index":"itcast"}}
{"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
{"index":{"_index":"itcast"}}
{"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
{"index":{"_index":"itcast"}}
{"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
{"index":{"_index":"itcast"}}
{"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳"}
{"index":{"_index":"itcast"}}
{"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影"}