ElasticSearch 高级操作之批量操作
ElasticSearch 高级操作批量操作:一次操作多条请求来操作ES导入数据:从数据库中查询出来并一次批量导入ES索引库中(因为ES索引库中的数据都来自数据库)。各种查询:各种查询操作索引别名和重建索引:线上环境做迁移,平滑过渡的技术批量操作:Bulk批量操作是将文档的增删改查一系列操作,通过一次请求完成,减少交互量、网络传输的次数和网络损耗,提升性能和速度。批量操作-脚本#批量操作#删除2
·
ElasticSearch 高级操作
批量操作:一次操作多条请求来操作ES
导入数据:从数据库中查询出来并一次批量导入ES索引库中(因为ES索引库中的数据都来自数据库)。
各种查询:各种查询操作
索引别名和重建索引:线上环境做迁移,平滑过渡的技术
批量操作:
Bulk批量操作是将文档的增删改查一系列操作,通过一次请求完成,减少交互量、网络传输的次数和网络损耗,提升性能和速度。
批量操作-脚本
#批量操作
#删除2号记录
#添加8号记录
#修改1号记录为一号
POST _bulk
{"delete":{"_index":"person","_id":"2"}}
{"create":{"_index":"person","_id":"8"}}
{"name": "八号","age":10,"address":"北京昌平区"}
{"update":{"_index":"person","_id":"1"}}
{"doc":{"name":"一号"}}
各个操作之间是相互独立的,即使第一个操作失败,后续也会正常执行。
批量操作-JavaAPI
基础环境介绍:SpringBoot项目
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>demo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:application.yml
elasticsearch:
host: "192.168.23.128"
port: 9200
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo02Application {
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
}
配置类
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
private String host;
private Integer port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
@Bean
public RestHighLevelClient client(){
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(
host, port, "http"
)));
return client;
}
}
测试代码:
//批量操作bulk
@Test
public void testBulk() throws IOException {
//创建bulkRequest对象,是用来整合所有操作的
BulkRequest bulkRequest=new BulkRequest();
//删除8号
DeleteRequest deleteRequest = new DeleteRequest("person","8");
bulkRequest.add(deleteRequest);
//添加10号
Map map = new HashMap();
map.put("name", "十号");
IndexRequest indexRequest = new IndexRequest("person").id("10").source(map);
bulkRequest.add(indexRequest);
//修改3号的name为三号
Map map2 = new HashMap();
map2.put("name", "三号");
UpdateRequest updateRequest = new UpdateRequest("person","3").doc(map2);
bulkRequest.add(updateRequest);
//执行批量操作
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(response.status());
}
测试结果:
结论:测试成功
更多推荐
已为社区贡献2条内容
所有评论(0)