ElasticSearch8.1入门,增删改查

新增:

POST /test_index/_doc
{
  "doc": {
    "name":"aaa",
    "price": 100009.00,
    "desc":"very good"
  }
}

修改:

POST /{indexName}/_update/{id}
{
  "doc": {
    "fieldName" : "fieldValue"
  }
}

举例:

POST /test_index/_update/AVYrxH8BeM2aFHTN9y9g
{
  "doc": {
    "name" : "test_index1_shanghai"
  }
}

这种格式已经不再适用,会报错:

POST /{indexName}/_doc/{id}/_update
或者
POST /indexName/{id}/_update

返回错误信息
{
  "error" : "no handler found for uri [/test_index/_doc/AVYrxH8BeM2aFHTN9y9g/_update?pretty=true] and method [POST]"
}

  同样,下面的命令会导致全量更新,如果操作不当,可能会丢失旧版本的数据

POST /test_index/_doc/ClaHxn8BeM2aFHTN-i_e
{
  "doc": {
    "name":"tea cake",
    "price" :39.00,
    "production date":"2022-03-26"
  }
}

并发修改:

以往ES的并发修改使用的是乐观锁机制,使用的时候需要指定version,但是目前version已经不再适用。以下是指定version时ES给出的提示信息:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
  },
  "status" : 400
}

现在,在并发修改的场景下,需要使用另外两个属性。

if_seq_noif_primary_term。这两个属性在新增根据id查询成功后,ES会返回当前数据的seq_no_primary_term

如果把这两个属性放在请求体中,会提示使用方式不对

{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "Field [_seq_no] is a metadata field and cannot be added inside a document. Use the index API request parameters."
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse field [_seq_no] of type [_seq_no] in document with id 'ClaHxn8BeM2aFHTN-i_e'. Preview of field's value: '20'",
    "caused_by" : {
      "type" : "mapper_parsing_exception",
      "reason" : "Field [_seq_no] is a metadata field and cannot be added inside a document. Use the index API request parameters."
    }
  },
  "status" : 400
}

正确的方式:

POST /{indexName}/_doc/{id}?if_seq_no={预期的seq_no}&if_primary_term={预期的primary_term}
{
  "doc": {
    "name":"tea cake",
    "price" :138.00
  }
}

 例子:

POST /test_index/_doc/ClaHxn8BeM2aFHTN-i_e?if_seq_no=20&if_primary_term=1
{
  "doc": {
    "name":"tea cake",
    "price" :138.00
  }
}

查询:

条件查询:

GET /test_index/_search
{
  "query": {
    "match" : {
    "_id" : "AVYrxH8BeM2aFHTN9y9g"
  }
  }
}

查询索引下所有的数据,和以前版本保持一致

GET /{indexName}/_search
{
  "query": {
    "match_all": {}
  }
}

举例:

GET /test_index/_search
{
  "query": {
    "match_all": {}
  }
}

根据_id查询

GET /test_index/_doc/B1ZaxX8BeM2aFHTNly-A

批量查询

如果根据多个id查询多条数据,需要用到_mget。目前_mget必须要指定index,否则会报错:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: index is missing for doc 0;2: index is missing for doc 1;"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: index is missing for doc 0;2: index is missing for doc 1;"
  },
  "status" : 400
}

正确的方式

GET /{indexName}/_mget
{
  "docs":[
    {
      "_id":"xxx"
    },
    {
      "_id":"xxx"
    }
    ]
}

举例:

GET /test_index/_mget
{
  "docs":[
    {
      "_id":"A1YsxH8BeM2aFHTNES_u"
    },
    {
      "_id":"BFYsxH8BeM2aFHTNFy-m"
    }
    ]
}

那不指定索引,可不可以查询所有数据呢?

GET _search
{
  "query": {
    "match_all": {}
  }
}

草稿,内容正在逐渐完善。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值