Intro
ElasticSearch在进行批量操作bulk
的时候,如果指定了_index, _type
,却不指定_id
就会报错。
直接向ElasticSearch执行QueryDSL,报错内容如下:
"type": "action_request_validation_exception",
"reason": "Validation Failed: 1: an id must be provided if version type or value are set;"
而如果你使用了elasticsearch-rest-high-level-client
这个jar包依赖,会报错:
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: id is missing;
细心检查代码中,批量插入ElasticSearch中的文档数据,是否有ID为null,避免即可。
后面是一些验证过程,可以略过不看。
环境
jar包依赖:elasticsearch-rest-high-level-client-6.1.0
ElasticSearch 6.1.0
Kibana 6.1.0
命令记录
可以把以下命令全部复制到Kibana中的Dev Tools
,依次执行,查看响应结果。
如果你还没有Kibana环境,那也可以看最后一节,有分步的执行内容和每一步对应的结果。
// 新建索引(两个字段:name, age)
PUT student
{
"mappings": {
"es6type": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}
// 查看索引定义
GET student
// 查询数据(此时为空)
GET student/_search
// 批量操作bulk(本次批量操作演示只操作一条),【不写_id就会报错】。
POST _bulk
{"create":{"_index":"student","_type":"es6type"}}
{"name":"李天然","age":20}
// 写ID
POST _bulk
{"create":{"_index":"student","_type":"es6type","_id":"10001"}}
{"name":"李天然","age":20}
// 再次查询
GET student/_search
// 删除索引
DELETE student
模拟环境测试
- 新建索引(两个字段:name, age)
PUT student
{
"mappings": {
"es6type": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}
创建成功:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "student"
}
- 查看索引定义
GET student
可以看到,除了我们自己手动定义的两个字段,还有其他一些默认的设置。
{
"student": {
"aliases": {},
"mappings": {
"es6type": {
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "keyword"
}
}
}
},
"settings": {
"index": {
"creation_date": "1586271102464",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "KPiEJ2xUQdGvC5u8YBk_Qg",
"version": {
"created": "6010099"
},
"provided_name": "student"
}
}
}
}
- 查询数据(此时为空)
GET student/_search
结果中hits
为0,即命中率为0条。
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
- 批量操作bulk(本次批量操作演示只操作一条),【不写_id就会报错】。
POST _bulk
{"create":{"_index":"student","_type":"es6type"}}
{"name":"李天然","age":20}
没写ID,报错:
- 写ID
POST _bulk
{"create":{"_index":"student","_type":"es6type","_id":"10001"}}
{"name":"李天然","age":20}
这次执行成功了:
{
"took": 19,
"errors": false,
"items": [
{
"create": {
"_index": "student",
"_type": "es6type",
"_id": "10001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
}
]
}
- 再次查询
GET student/_search
查询到1条
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "student",
"_type": "es6type",
"_id": "10001",
"_score": 1,
"_source": {
"name": "李天然",
"age": 20
}
}
]
}
}
- 删除索引
DELETE student
删除成功:
{
"acknowledged": true
}