ES Docs-3:Modifying Data

本文详细介绍了如何使用 Elasticsearch 进行数据修改、索引、替换、更新和删除操作,包括 PUT、POST 方法的应用,以及批量处理 API 的使用。重点阐述了通过 ID 对文档进行修改和删除的方法,并讨论了脚本更新的限制。

Modifying Data

Indexing/Replacing Documents

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'

If we then executed the above command again with a different (or same) document, Elasticsearch will replace (i.e. reindex) a new document on top of the existing one with the ID of 1

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "Micheal"
}'

If, on the other hand, we use a different ID, a new document will be indexed and the existing document(s) already in the index remains untouched.

we are using the POST verb instead of PUT since we didn’t specify an ID.

curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
  "name": "Jane Doe"
}'

Updating Documents

This example shows how to update our previous document (ID of 1) by changing the name field to "Jane Doe":
改变name field

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe" }
}'

添加age field

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe", "age": 20 }
}'

脚本更新(失败)

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "script" : "ctx._source.age += 5"
}'

返回

{
  "error" : {
    "root_cause" : [ {
      "type" : "remote_transport_exception",
      "reason" : "[es_node_1][127.0.0.1:9300][indices:data/write/update[s]]"
    } ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "scripts of type [inline], operation [update] and lang [groovy] are disabled"
    }
  },
  "status" : 400
}

Deleting Documents

根据ID删除Doc

es@qiandu1-yuwencai:~/cluster/elasticsearch-2.3.1$ curl -XDELETE 'localhost:9200/customer/external/2?pretty'

返回

{
  "found" : true,
  "_index" : "customer",
  "_type" : "external",
  "_id" : "2",
  "_version" : 2,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

The delete-by-query plugin can delete all documents matching a specific query.

Batch Processing

**_bulk API**

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'

返回

{
  "took" : 17,
  "errors" : false,
  "items" : [ {
    "index" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "1",
      "_version" : 5,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 200
    }
  }, {
    "index" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "2",
      "_version" : 1,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 201
    }
  } ]
}

当然,也可以进行索引的更新和删除操作,不过删除操作没有参数

{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

This functionality is important in that it provides a very efficient mechanism to do multiple operations as fast as possible with as little network roundtrips as possible.

The bulk API executes all the actions sequentially and in order. If a single action fails for whatever reason, it will continue to process the remainder of the actions after it. When the bulk API returns, it will provide a status for each action (in the same order it was sent in) so that you can check if a specific action failed or not.

参考资料

来源: https://www.elastic.co/guide/en/elasticsearch/reference/current/_batch_processing.html

转载于:https://www.cnblogs.com/myitroad/p/5405482.html

#spring: # jpa: # hibernate: # ddl-auto: none # ★★记得上线时改回来 # show-sql: false # database-platform: org.hibernate.dialect.Oracle10gDialect # open-in-view: false # datasource: # url: jdbc:oracle:thin:@192.168.88.26:1521:orcl # driver-class-name: oracle.jdbc.OracleDriver # username: dba_mgr # password: totodir #hikari数据库连接池 # hikari: # pool-name: Retail_HikariCP # minimum-idle: 5 #最小空闲连接数量 # idle-timeout: 300000 #空闲连接存活最大时间,默认600000(10分钟) # maximum-pool-size: 10 #连接池最大连接数,默认是10 # auto-commit: true #此属性控制从池返回的连接的默认自动提交行为,默认值:true # max-lifetime: 600000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟 # connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000 # connection-test-query: select 1 from dual #server: # servlet: # session: # timeout: 300m # port: 7086 # undertow: # url-charset: UTF-8 #logging: # file: # path: /hyerpinterfacelog # name: hyerpinterface.log server: port: 7086 servlet: session: timeout: 1800s spring: #jpa: # show-sql: true # properties: # hibernate: # format_sql: true datasource: dynamic: primary: oracle strict: false datasource: syoracle: url: jdbc:oracle:thin:@192.168.88.26:1521:orcl username: dba_mgr password: totodir driver-class-name: oracle.jdbc.OracleDriver druid: initial-size: 10 min-idle: 10 max-active: 500 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 lyoracle: url: jdbc:oracle:thin:@192.168.88.30:1521:orcl username: dba_mgr password: totodir driver-class-name: oracle.jdbc.OracleDriver druid: initial-size: 10 min-idle: 10 max-active: 500 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 mybatis-plus: global-config: db-config: # 主键ID类型 # id-type: none logic-delete-field: deleted logic-delete-value: 1 logic-not-delete-value: 0 configuration: # 驼峰下划线转换 map-underscore-to-camel-case: true # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl import com.alibaba.druid.pool.DruidDataSource; import com.hyerp.hyerpinterface.config.DynamicDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; @Configuration public class DataSourceConfig { // 沈阳数据源 (0101) @Bean("syOracleDataSource") @ConfigurationProperties(prefix = "spring.datasource.syoracle") public DataSource syOracleDataSource() { return new DruidDataSource(); // 使用Druid连接池 } // 洛阳数据源 (0102) @Bean("lyOracleDataSource") @ConfigurationProperties(prefix = "spring.datasource.lyoracle") public DataSource lyOracleDataSource() { return new DruidDataSource(); // 使用Druid连接池 } // 主数据源(动态路由) @Primary @Bean("dynamicDataSource") public DataSource dynamicDataSource() { Map<Object, Object> targetDataSources = new HashMap<>(2); // 使用部门编码作为数据源标识 targetDataSources.put("-5716907732163998808", syOracleDataSource()); // 沈阳连接 targetDataSources.put("4294613579574035536", syOracleDataSource()); // 沈阳传感 targetDataSources.put("0103", lyOracleDataSource()); // 洛阳 // 默认使用沈阳数据源 return new DynamicDataSource(syOracleDataSource(), targetDataSources); } }package com.hyerp.hyerpinterface.dao; import com.baomidou.dynamic.datasource.annotation.DS; import com.hyerp.hyerpinterface.domain.entity.OaItemAbove; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import javax.transaction.Transactional; import java.util.List; import java.util.Optional; /** * @Author GGZ * @Date 2023/3/20 14:06 * @Version 1.5 */ public interface OaitemRepo extends JpaRepository<OaItemAbove, String> { List<OaItemAbove> findByIdnum(String Idnum); List<OaItemAbove> findByShowvalueAndRef(String showvalue, String refid); List<OaItemAbove> findByShowvalue(String showvalue); @Transactional @DS("syoracle") @Query(value = "SELECT SHOWVALUE FROM OACTP_ENUM_ITEM WHERE IDNUM = ?", nativeQuery = true) Optional<OaItemAbove> selectidnumop(@Param("idnum") String idnum); @Transactional @DS("syoracle") @Query(value = "SELECT SHOWVALUE FROM OACTP_ENUM_ITEM WHERE IDNUM = 8347348803478963605", nativeQuery = true) } } 该sql语句在数据库能查到SELECT SHOWVALUE FROM OACTP_ENUM_ITEM WHERE IDNUM = 8347348803478963605在程序中查询是空怎么解决
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值