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
    评论
要在Python中进行Elasticsearch增删改查操作,你可以使用Elasticsearch的官方库elasticsearch-py来实现。下面是一个简单的例子来演示如何进行增删改查操作: 引用: 首先,你需要导入Elasticsearch库并连接到Elasticsearch服务器: ```python from elasticsearch import Elasticsearch # 连接到Elasticsearch服务器 es = Elasticsearch([ES_IP], http_auth=('elastic', '123456'), port=ES_PORT) ``` 引用: 查询数据: - 查询所有数据: ```python body = { "size": 111, # 最大显示数量,es默认展示10条 "query": { "match_all": {} } } res = es.search(index='my-index', body=body, request_timeout=30) ``` - 查询具体某个字段: ```python body = { "size": 10000, # 最大显示数量 "query": { "match": { "text": { "query": search_key, "analyzer": "ik_smart", # 用来指定搜索的词语按那种拆词粒度拆词 "operator": "or", # 按拆分后的词查询时,词与词之间是 and 还是 or 的关系 "minimum_should_match": "75%" # 该参数用来控制应该匹配的分词的最少数量,至少匹配几个词才召回查询的结果 } } } } res = es.search(index='my-index', body=body, request_timeout=30) ``` 引用: 获取拆分后的词: 你可以使用Elasticsearch的analyze API来获取拆分后的词,例如: ```python body = { "text": "惠普 p2015dn", "analyzer": "ik_max_word" } res = es.indices.analyze(index='my-index', body=body) key_list = [dic['token'] for dic in res['tokens']] print(key_list) # ['惠普', 'p2015dn', 'p', '2015', 'dn'] ``` 以上是关于在Python中使用Elasticsearch进行增删改查的基本操作。你可以根据具体的需求使用不同的查询方式,并根据返回的结果进行相应的处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python 使用 Elasticsearch 增删查改](https://blog.csdn.net/Waller_/article/details/109810964)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值