使用springboot对Elasticsearch 进行索引的增、删、改、查

这篇具有很好参考价值的文章主要介绍了使用springboot对Elasticsearch 进行索引的增、删、改、查。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一 SpringBoot + Elasticsearch 项目环境搭建

1.1 修改pom文件添加依赖

目前使用spring-boot-starter-parent版本为2.2.8.RELEASE

对应spring-data-elasticsearch版本为2.2.8.RELEASE,版本对应可以自行百度,如果不行直接用elasticsearch-rest-high-level-client工具类吧

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.5.0</version>
        </dependency>
1.2 新建配置文件
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES配置类
 *
 * @author lc
 * @version 1.0
 * @date 2022/3/25 10:53
 */
@Configuration
public class ElasticSearchClientConfig {


    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.1.100", 9200, "http")));
        return client;
    }
}

二 RestHighLevelClient的使用

RestHighLevelClient是Elasticsearch 的操作方法,我们先进行引用吧。文章来源地址https://www.toymoban.com/news/detail-733729.html

@Autowired
private RestHighLevelClient client;

1、创建索引

 @Test
    void testCreateIndex() throws IOException {
        //1 创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("zlc_index");
        //2 客户端执行请求
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);

        System.out.println(createIndexResponse);
    }

2、索引是否存在

@Test
    void testExistIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("zlc_index");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

3、删除索引

@Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("zlc_index");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

4、添加文档

@Test
    void testAddDocument() throws IOException {
        //创建对象
        UserES user = new UserES();
        user.setUserName("suwerw");
        user.setUserPhone("178245774");
        //创建请求
        IndexRequest request = new IndexRequest("zlc_index");
        //规则 put /zlc_index/_doc/1
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");

        //将数据放入请求
        request.source(JSON.toJSONString(user), XContentType.JSON);

        //客户端发送请求,获取响应结果
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }

5、判断文档是否存在

@Test
    void testIsExists() throws IOException {
        GetRequest getRequest = new GetRequest("zlc_index", "1");
        //不获取返回的 _source 的上下文,提高效率
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");

        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

6、获取文档

 @Test
    void testGetDocument() throws IOException {
        GetRequest getRequest = new GetRequest("zlc_index", "1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse);
        System.out.println(getResponse.getSourceAsString());
    }

7、更新文档信息

 @Test
    void testUpdateDocument() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("zlc_index", "1");
        updateRequest.timeout("1s");

        UserES user = new UserES();
        user.setUserName("Zhou_LC");
        user.setUserPhone("233669");
        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);

        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse);
        System.out.println(updateResponse.status());
    }

8、删除文档

  @Test
    void testDeleteDocument() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("zlc_index", "1");
        deleteRequest.timeout("1s");

        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse);
        System.out.println(deleteResponse.status());
    }

到了这里,关于使用springboot对Elasticsearch 进行索引的增、删、改、查的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot中进行elasticSearch查询,使用QueryBuilders构建各类条件查询

    BoolQueryBuilder对象使用must方法build,多个and使用多个must BoolQueryBuilder对象使用should方法build,多个or使用多个should使用 本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。 首发链接:https://www.cnblogs.com/lingyejun/p/17557467.html

    2024年02月16日
    浏览(30)
  • Elasticsearch集群进行索引的备份与恢复

    准备需要三台服务器 ip如下: 10.1.40.110,10.1.40.12,10.1.40.8 我们要进行集群部署首先要进行,创建备份共享文件存储地址 由于是集群部署且服务器不同所以这边用到了NFS进行文件的共享 备份到集群共享目录 步骤: 1) 、搭建集群共享目录 (这里使用NFS也可以使用其它共享目录

    2024年02月03日
    浏览(30)
  • ElasticSearch篇——Restful风格详解以及常见的命令,涵盖_cat命令查看ES默认数据、索引和文档的增删改查以及复杂搜索,超详细、超全面、超细节!

    一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要是用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更加简洁,更有层次,更易于实现缓存等机制。 一、基本Rest命令说明 1、命令 对应的就是head可视化界面的下面的信息(换句话

    2024年01月16日
    浏览(37)
  • spring elasticsearch:启动项目时自动创建索引

    在springboot整合spring data elasticsearch项目中,当索引数量较多,mapping结构较为复杂时,我们常常希望启动项目时能够自动创建索引及mapping,这样就不用再到各个环境中创建索引了 所以今天咱们就来看看如何自动创建索引 如股票使用的是spring data elasticsearch包,其 @Document 注解中

    2024年02月11日
    浏览(38)
  • Spring boot 实现 Elasticsearch 动态创建索引

    查找了很多方法都是通过Spring EL表达式实现 @Document(IndexName=\\\" #{demo.getIndexName} \\\") 这种方式的问题在于没法解决数据库里生成的序号,例如我希望通过公司ID生成索引编号。后来在外网上找到一个大佬提出的解决方案,这位大佬把两种方案都实现了一遍。 通过entityId自定义index

    2024年02月11日
    浏览(28)
  • 【SpringBoot】整合Elasticsearch 操作索引及文档

    官网操作文档:Elasticsearch Clients | Elastic                踩坑太多了。。。这里表明一下Spring Boot2.4以上版本可能会出现问题,所以我降到了2.2.1.RELEASE。对于现在2023年6月而言,Es版本已经到了8.8,而SpringBoot版本已经到了3.x版本。如果是高版本的Boot在配置类的时候会发现

    2024年02月09日
    浏览(47)
  • spring data elasticsearch:启动项目时自动创建索引

    在springboot整合spring data elasticsearch项目中,当索引数量较多,mapping结构较为复杂时,我们常常希望启动项目时能够自动创建索引及mapping,这样就不用再到各个环境中创建索引了 所以今天咱们就来看看如何自动创建索引 我们已经在实体类中声明了索引数据结构了,只需要识别

    2024年02月05日
    浏览(33)
  • SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)

    RestHighLevelClient 是 Elasticsearch 官方提供的Java高级客户端,用于与 Elasticsearch 集群进行交互和执行各种操作。 主要特点和功能如下 : 强类型 :RestHighLevelClient 提供了强类型的 API,可以在编码过程中获得更好的类型安全性和 IDE 支持。 兼容性 :RestHighLevelClient 是 Elasticsearch 官方

    2024年02月11日
    浏览(35)
  • SpringBoot+Elasticsearch按日期实现动态创建索引(分表)

    😊 @ 作者: 一恍过去 💖 @ 主页: https://blog.csdn.net/zhuocailing3390 🎊 @ 社区: Java技术栈交流 🎉 @ 主题: SpringBoot+Elasticsearch按日期实现动态创建索引(分表) ⏱️ @ 创作时间: 2023年02月19日 SpringBoot+Elasticsearch,通过 @Document 注解,利用EL表达式指定到配置文件,实现动态生成

    2023年04月08日
    浏览(33)
  • spring data elasticsearch:动态配置实体类索引名称indexName

    最近接到一个需要,需要在spring data elasticsearch关联的实体类中动态的根据配置文件动态创建索引名称,比如开发环境下索引名称为user-dev,测试环境下为user-test,生产环境为user-prod 一开始接到这个需要觉得很怪,因为不同环境的区分直接搭建不同的es服务器环境不就行了吗,

    2023年04月21日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包