SpringBoot + ElasticSearch8.4.3 实现简单CRUD、批量操作

1 Java High Level REST Client

在这里插入图片描述

2 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springboot-es</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.10.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>


        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- 做断点下载使用-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>



        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.4.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>elasticsearch-rest-client</artifactId>
                    <groupId>org.elasticsearch.client</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>8.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
            <version>2.0.1</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

3 application.yml

server:
  port: 8500




config:
  es:
    host: 192.168.38.80
    port: 9200
    scheme: http
    username: elastic
    password: 123456

4 EsConfig

package com.es.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsConfig {


    @Value("${config.es.host}")
    private String host;

    @Value("${config.es.port}")
    private Integer port;

    @Value("${config.es.scheme}")
    private String scheme;

    @Value("${config.es.username}")
    private String username;

    @Value("${config.es.password}")
    private String password;

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, scheme)).
                setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        RestClient restClient = builder.build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }


}

5 ProductVo

@AllArgsConstructor
@NoArgsConstructor
@Data
public class ProductPojo {


    private Long id;

    private String name;

    private String desc;

    private Integer price;

    private String level;

    private String type;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    private List<String> tags;

}

6 索引

		@Test
    public void testLambaIndex() throws IOException {
        final String indexName = "test_index";
        ElasticsearchIndicesClient indices = elasticsearchClient.indices();
        boolean flag = indices.exists(request -> request.index(indexName)).value();
        if (flag) {
            System.out.println("索引已经存在");
        } else {
            CreateIndexResponse createIndexResponse = indices.create(request -> request.index(indexName));
            System.out.println("创建索引:" + createIndexResponse.acknowledged());
        }

        flag = indices.exists(r -> r.index(indexName)).value();
        if (flag) {
            System.out.println("索引已经存在");
        }
        DeleteIndexResponse deleteIndexResponse = indices.delete(r -> r.index(indexName));
        System.out.println("删除索引:" + deleteIndexResponse.acknowledged());
    }

7 创建数据

@Test
public void testCreateDocument() throws IOException {
	  //准备
	  ProductPojo productPojo = new ProductPojo();
	  productPojo.setCreateTime(new Date());
	  productPojo.setId(1000L);
	  productPojo.setDesc("小米手机10");
	  productPojo.setName("小米手机10");
	  productPojo.setType("手机");
	  productPojo.setLevel("旗舰机");
	  productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
	  productPojo.setPrice(5999);
	  //创建
	  elasticsearchClient.create(r -> r.index("product_index").id(String.valueOf(productPojo.getId())).document(productPojo));
}

@Test
public void testCreateBatchDocument() throws IOException {

  BulkRequest.Builder br = new BulkRequest.Builder();

  for (long i = 1001L; i <= 1002; i++) {
      ProductPojo productPojo = new ProductPojo();
      productPojo.setCreateTime(new Date());
      productPojo.setId(i);
      productPojo.setDesc("小米手机10");
      productPojo.setName("小米手机10");
      productPojo.setType("手机");
      productPojo.setLevel("旗舰机");
      productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
      productPojo.setPrice(5999);
      br.operations(bulk -> bulk.index(
              index -> index.index("product_index")
                      .id(String.valueOf(productPojo.getId()))
                      .document(productPojo)
      ));
  }
  elasticsearchClient.bulk(br.build());
}

8 修改数据

@Test
public void updateDocument() throws IOException {

    ProductPojo productPojo = new ProductPojo();
    productPojo.setCreateTime(new Date());
    productPojo.setId(1000L);
    productPojo.setDesc("小米手机11");
    productPojo.setName("小米手机11");
    productPojo.setType("手机");
    productPojo.setLevel("旗舰机");
    productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
    productPojo.setPrice(5999);
    elasticsearchClient.update(r -> r.index("product_index").id("1000").doc(productPojo), ProductPojo.class);

}


@Test
public void updateBatchDocument() throws IOException {
    BulkRequest.Builder br = new BulkRequest.Builder();

    for (long i = 1001L; i <= 1002; i++) {
        ProductPojo productPojo = new ProductPojo();
        productPojo.setCreateTime(new Date());
        productPojo.setId(i);
        productPojo.setDesc("小米手机10");
        productPojo.setName("小米手机10");
        productPojo.setType("手机");
        productPojo.setLevel("旗舰机");
        productPojo.setTags(Arrays.asList("120HZ刷新率", "120W快充", "120倍聚焦"));
        productPojo.setPrice(5999);
        br.operations(bulk -> bulk.index(
                index -> index.index("product_index")
                        .id(String.valueOf(productPojo.getId()))
                        .document(productPojo)
        ));
    }
    elasticsearchClient.bulk(br.build());


}

9 删除数据

@Test
public void testDeleteDocument() throws IOException {

    elasticsearchClient.delete(r -> r.index("product_index").id(String.valueOf(1000L)));

}

@Test
public void testBatchTest() throws IOException {

    elasticsearchClient.deleteByQuery(
            r -> r.index("product_index").query(
                    q -> q.terms(
                            t -> t.field("id").terms(
                                    v -> v.value(Arrays.asList(FieldValue.of("1000"), FieldValue.of("1001"), FieldValue.of("1002")))
                            )
                    )
            )
    );

}

10 简单数据查询

@Test
public void testQuery() throws IOException {
    SearchResponse<ProductPojo> search = elasticsearchClient.search(
            req -> {
                req.index("product_index").query(
                        q -> q.term(
                                m -> m.field("name.keyword").value("小米手机10")
                        )
                );
                return req;
            },
            ProductPojo.class
    );
    for (Hit<ProductPojo> pro : search.hits().hits()) {
        System.out.println(JSON.toJSON(pro.source()));
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

响彻天堂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值