SpringBoot RestHighLevelClient 按版本更新

SpringBoot RestHighLevelClient 按版本更新

  • [1 查询](#1 查询)
  • [2 更新](#2 更新)

RestHighLevelClient 是 Elasticsearch 提供的一个用于与 Elasticsearch 集群交互的高级 REST 客户端。它是基于 Java 的客户端,旨在提供一种简单且功能丰富的方式来执行各种 Elasticsearch 操作,包括索引、搜索、更新和删除文档,以及管理索引和集群。

主要特点 解释
高级 API 提供了一组高级 API,简化了与 Elasticsearch 交互的复杂性。
线程安全 设计为线程安全,可以在多线程环境中使用。
轻松配置 可以通过简单的配置连接到 Elasticsearch 集群。
支持所有 Elasticsearch 功能 几乎支持所有 Elasticsearch 提供的功能,包括文档操作、搜索、聚合、索引管理、集群管理等。
同步和异步操作 支持同步和异步两种操作方式,满足不同的使用场景。
xml 复制代码
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.13.4</version>
</dependency>

1 查询

java 复制代码
import lombok.extern.slf4j.Slf4j;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.jeecg.modules.demo.cate.entity.CateRule;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NewTest {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RestHighLevelClient client;

    @Test
    public void test() {
        search("student_test", "1", CateRule.class);
    }

    /**
     * 查询
     *
     * @param index 索引
     * @param id    ID
     * @param cls   类
     * @param <T>   类
     * @return 类
     */
    private <T> T search(String index, String id, Class<T> cls) {
        try {
            GetRequest get = new GetRequest(index, id);
            GetResponse response = client.get(get, RequestOptions.DEFAULT);
            if (response.isExists()) {
                return objectMapper.readValue(response.getSourceAsString(), cls);
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("查询失败!", e);
        }
        return null;
    }

}

2 更新

java 复制代码
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.test.context.junit4.SpringRunner;

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NewTest {

    @Autowired
    private RestHighLevelClient client;

    @Test
    public void test() {
        forceUpdate(Document.create(), "student_test", "1");
    }

    /**
     * 按版本更新
     *
     * @param doc   Document
     * @param index 索引
     * @param id    ID
     * @return 结果
     */
    private boolean forceUpdate(Document doc, String index, String id) {
        try {
            // 查询
            GetRequest get = new GetRequest(index, id);
            GetResponse rep1 = client.get(get, RequestOptions.DEFAULT);
            if (!rep1.isExists()) {
                return false;
            }
            long seqNo = rep1.getSeqNo();
            long primaryTerm = rep1.getPrimaryTerm();

            // 更新
            UpdateRequest update = new UpdateRequest(index, id)
                    .doc(doc.toJson(), XContentType.JSON)
                    .setIfSeqNo(seqNo)
                    .setIfPrimaryTerm(primaryTerm);
            UpdateResponse rep2 = client.update(update, RequestOptions.DEFAULT);
            if (rep2.status() == RestStatus.OK) {
                return true;
            }
        } catch (Exception e) {
            log.error("更新失败!", e);
        }
        return false;
    }

}
相关推荐
我是小酒12 小时前
掌握 Spring:从新手到高手的常见问题汇总
java·后端·spring·springboot
赚钱给孩子买茅台喝2 天前
智能BI项目第一期
java·人工智能·springboot·react
java1234_小锋2 天前
免费分享一套SpringBoot+Vue学生信息管理系统【论文+源码+SQL脚本】,帅呆了~~
java·springboot·java毕业设计·学生信息·java学生信息·springboot学生信息·vue学生信息
蓝染-惣右介2 天前
【若依RuoYi-Vue | 项目实战】帝可得后台管理系统(一)
java·后端·物联网·vue·springboot
张某布响丸辣2 天前
Nginx 负载均衡:优化网站性能与可扩展性的利器
java·运维·nginx·负载均衡·springboot
爱新觉罗13 天前
Mybatis-plus-Generator 3.5.5 自定义模板支持 (DTO/VO 等) 配置
mybatis·springboot
IT学长编程3 天前
计算机毕业设计 大学志愿填报系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·大学志愿填报系统
兴趣使然h4 天前
ElasticSearch的DSL查询④(DSL查询、RestClient的DSL查询)
java·大数据·elasticsearch·搜索引擎·springboot
万笑佛5 天前
springboot-实现csv文件导出功能
springboot
justry_deng9 天前
ConfigurationProperties配置报黄、无描述的问题
springboot·配置说明·美化配置·配置报黄·配置规范化