ElasticSearch bulk 批量插入报错 ActionRequestValidationException: Validation Failed: 1: id is missing;

50 篇文章 0 订阅
35 篇文章 0 订阅

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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值