广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot整合ES-Elasticsearch的实例
  • 109
分享到

SpringBoot整合ES-Elasticsearch的实例

2024-04-02 19:04:59 109人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录概述添加Maven依赖配置application.yml创建索引对象SpringBoot操作ES数据的三种方式实现索引对应的Repository文档操作文档保存、查询、删除分页查

概述

本文介绍 Spring Boot 项目中整合 elasticsearch 并实现 CRUD 操作,包括分页、滚动等功能。

添加Maven依赖

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

配置application.yml

spring:
  elasticsearch:
    rest:
      uris: 192.168.1.81:9200

创建索引对象

package com.practice.elkstudy.entity;
import cn.hutool.core.date.DateTime;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;

@Document(indexName = "article")
@Data
public class ArticleEntity {
    @Id
    private String id;
    private String title;
    private String content;
    private Integer userId;
    private Date createTime = DateTime.now();
}

SpringBoot操作ES数据的三种方式

  • 实现ElasticsearchRepository接口
  • 引入ElasticsearchRestTemplate
  • 引入ElasticsearchOperations

实现索引对应的Repository

package com.practice.elkstudy.repository;
import com.practice.elkstudy.entity.ArticleEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleRepository extends ElasticsearchRepository<ArticleEntity,String> {
}

文档操作

下面可以使用这个 ArticleRepository 来操作 ES 中的 Article 数据。

我们这里没有手动创建这个 Article 对应的索引,由 elasticsearch 默认生成。

下面的接口,实现了 spring boot 中对 es 数据进行插入、更新、分页查询、滚动查询、删除等操作。可以作为一个参考。

其中,使用了 Repository 来获取、保存、删除 ES 数据;使用 ElasticsearchRestTemplate 或 ElasticsearchOperations 来进行分页/滚动查询。

文档保存、查询、删除

package com.practice.elkstudy.controller.controller;
import com.practice.elkstudy.entity.ArticleEntity;
import com.practice.elkstudy.repository.ArticleRepository;
import org.springframework.WEB.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping("/elk")
public class ArticleController {
    @Resource
    private ArticleRepository articleRepository;
    
    @GetMapping("/byId")
    public String findById(@RequestParam String id) {
        Optional<ArticleEntity> record = articleRepository.findById(id);
        return  record.toString();
    }
    
    @PostMapping("/saveArticle")
    public String saveArticle(@RequestBody ArticleEntity article) {
        ArticleEntity result = articleRepository.save(article);
        return result.toString();
    }
	@DeleteMapping("/deleteById")
    public String deleteArticle(@RequestParam String id) {
        articleRepository.deleteById(id);
        return "success";
    }
}

分页查询与滚动查询

package com.practice.elkstudy.controller.controller;
import com.practice.elkstudy.entity.ArticleEntity;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchHitsImpl;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/elk")
public class ArticleAdvanceController {
    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private ElasticsearchOperations operations;
    
    @GetMapping("/queryPage")
    public String queryPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
        query.setPageable(PageRequest.of(pageNum, pageSize));
        // 方法1
        SearchHits<ArticleEntity> search = restTemplate.search(query, ArticleEntity.class);
        // 方法2
        // SearchHits<ArticleEntity> search = operations.search(query, ArticleEntity.class);
        List<ArticleEntity> articles = search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        return articles.toString();
    }
    
    @GetMapping(value = "/scrollQuery")
    public String scroll(String scrollId, Integer pageSize) {
        if (pageSize == null || pageSize <= 0) {
            return "please input query page num";
        }
        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
        query.setPageable(PageRequest.of(0, pageSize));
        SearchHits<ArticleEntity> searchHits;
        if (StringUtils.isEmpty(scrollId) || scrollId.equals("0")) {
            // 开启一个滚动查询,设置该scroll上下文存在60s
            // 同一个scroll上下文,只需要设置一次query(查询条件)
            searchHits = restTemplate.searchScrollStart(60000, query, ArticleEntity.class, IndexCoordinates.of("article"));
            if (searchHits instanceof SearchHitsImpl) {
                scrollId = ((SearchHitsImpl) searchHits).getScrollId();
            }
        } else {
            // 继续滚动
            searchHits = restTemplate.searchScrollContinue(scrollId, 60000, ArticleEntity.class, IndexCoordinates.of("article"));
        }
        List<ArticleEntity> articles = searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        if (articles.size() == 0) {
            // 结束滚动
            restTemplate.searchScrollClear(Collections.singletonList(scrollId));
            scrollId = null;
        }
        if (Objects.isNull(scrollId)) {
            Map<String, String> result = new HashMap<>(2);
            result.put("articles", articles.toString());
            result.put("message", "已到末尾");
            return result.toString();
        } else {
            Map<String, String> result = new HashMap<>();
            result.put("count", String.valueOf(searchHits.getTotalHits()));
            result.put("pageSize", String.valueOf(articles.size()));
            result.put("articles", articles.toString());
            result.put("scrollId", scrollId);
            return result.toString();
        }
    }
}

ES深度分页 vs 滚动查询

之前遇到的一个问题,日志检索的接口太慢了。

开始使用的是深度分页,即1,2,3…10,这样的分页查询,查询条件较多(十多个参数)、查询数据量较大(单个日志索引约2亿条数据)。

分页查询速度慢的原因在于:ES的分页查询,如查询第100页数据,每页10条,是先从每个分区(shard,一个索引默认是5个shard)中把命中的前100*10条数据查出来,然后协调节点进行合并操作,最后给出100页的数据。也就是说,实际被加载到内存的数据远远超过理想情况。

这样,索引分片数越多,查询页数越多,查询速度就越慢。ES默认的max_result_window是10000条,也就是正常情况下,用分页查询到10000条数据时,就不会在返回下一页数据了。

如果不需要进行跳页,比如直接查询第100页数据,或者数据量非常大,那么可以考虑用scroll查询。在scroll查询下,第1次需要根据查询参数开启一个scroll上下文,设置上下文缓存时间。以后的滚动只需要根据第一次返回的scrollId来进行即可。

scroll只支持往下滚动,如果想要往前滚动,还可以根据scrollId缓存查询结果,这样就可以实现上下文滚动查询了一一就像大家经常使用的淘宝商品检索时上下滚动一样。 

SpringBoot集成ES基本使用

#配置es
#Liunx 上的ip地址和配置端口号
spring.elasticsearch.rest.uris=192.168.113.129:9200

在test中测试

import com.alibaba.fastJSON.jsON;
import com.hzx.pojo.User;
import com.hzx.utils.ESconst;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TerMQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
@Autowired
private RestHighLevelClient client;
@Test
void contextLoads() throws IOException {
    //创建索引请求
    CreateIndexRequest request = new CreateIndexRequest("hong_index");
    //客户端执行请求 IndicesClient  create创建请求  RequestOptions.DEFAULT默认请求参数
    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    //获取返回的参数
    System.out.println(createIndexResponse);
}
@Test
void test2() throws IOException {
    //获取指定索引库
    GetIndexRequest request = new GetIndexRequest("hong_index2");
    //判断获取索引是否存在
    boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
    //如果索引存在就返回为true  或者 为false
    System.out.println(exists);
}
@Test
void test3() throws IOException {
    //删除指定索引库
    DeleteIndexRequest request = new DeleteIndexRequest("hong_index");
    //获取删除索引
    AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
    //检查索引是否被删除
    System.out.println(delete.isAcknowledged());
}
    //测试添加文档
    @Test
    void test4() throws IOException {
        //创建对象
        User user = new User("枣信",18);
        //创建索引库
        IndexRequest request = new IndexRequest("hong_index");
        //规则 为 put /hong_index/_doc/1
        //创建的id
        request.id("1");
        //创建的时间
        request.timeout(TimeValue.timeValueSeconds(1));
//        request.timeout("1s");
        //将数据放入到请求     JSON.toJSONString(user)将对象转换为json
        request.source(JSON.toJSONString(user), XContentType.JSON);
        //客户端发送请求   向索引中添加数据
        IndexResponse indices = client.index(request, RequestOptions.DEFAULT);
        //获取返回的json对象
        System.out.println(indices.toString());
        //获取发送请求的状态 添加为CREATED  更新为OK
        System.out.println(indices.status());
    }
//获取文档信息
@Test
void test6() throws IOException {
    //根据索引传入的id获取
    GetRequest getRequest = new GetRequest("hong_index","1");
    //通过get获取信息
    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    //根据指定的Source获取对应内容
    System.out.println(getResponse.getSourceAsString());
    //打印json对象
    System.out.println(getResponse);
}
//更新 修改信息
@Test
void test7() throws IOException {
    //根据索引库传入的id更新
    UpdateRequest updateRequest = new UpdateRequest("hong_index","1");
    //更新时间
    updateRequest.timeout("1s");
    //创建对象
    User user = new User("李四", 26);
    //更新    将对象转换为json
    updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
    //客户端发送请求,进行更新
    UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
    //获取更新状态
    System.out.println(update.status());
}
//删除文档信息
@Test
void test8() throws IOException {
    //根据传入的索引id进行删除
    DeleteRequest request = new DeleteRequest("hong_index","1");
    //发送请求,删除
    DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
    //获取删除的状态   没有删除成功为NOT_FOUND 删除成功为OK
    System.out.println(delete.status());
}
//批量添加数据
@Test
void test9() throws IOException {
    //创建批量添加
    BulkRequest bulkRequest = new BulkRequest();
    //添加时间
    bulkRequest.timeout("8s");
    //创建一个arraylist集合
    ArrayList<User> userList = new ArrayList<>();
    userList.add(new User("李四",19));
    userList.add(new User("王五",25));
    userList.add(new User("赵刚",30));
    userList.add(new User("张三",21));
    userList.add(new User("赵六",36));
    userList.add(new User("小武",20));
    //批量处理请求
    for (int i = 0; i < userList.size(); i++) {
        //批量更新和删除 在这修改对应的请求即可       不添加id(""+(i+1)) 会默认随机id,在大数据情况下,让他默认随机id
        bulkRequest.add(new IndexRequest("hong_index").id(""+(i+1)).source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
    }
    //批量添加发送请求
    BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    //获取批量添加的状态 返回false代表添加成功
    System.out.println(bulk.hasFailures());
}
//查询索引信息
@Test
void test10() throws IOException {
    //查询
    SearchRequest searchRequest = new SearchRequest(ESconst.ES_INDEX);
    //构建搜索条件
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //查询条件,可以使用QueryBuilders工具来实现
    // QueryBuilders.termQuery精确查询
    // QueryBuilders.matchQuery()查询所有
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "李四");
    //查询的时间
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    //将查询的sourceBuilder放入searchRequest中
    searchRequest.source(sourceBuilder);
    //发送请求
    SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
    //获取信息
    System.out.println(JSON.toJSONString(search.getHits()));
    //循环变量出信息
    for(SearchHit documentFields : search.getHits().getHits()){
        //获取所有信息
        System.out.println(documentFields.getSourceAsMap());
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。 

--结束END--

本文标题: SpringBoot整合ES-Elasticsearch的实例

本文链接: https://www.lsjlt.com/news/147926.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot整合ES-Elasticsearch的实例
    目录概述添加Maven依赖配置application.yml创建索引对象SpringBoot操作ES数据的三种方式实现索引对应的Repository文档操作文档保存、查询、删除分页查...
    99+
    2022-11-13
  • SpringBoot整合ElasticSearch的示例代码
    ElasticSearch作为基于Lucene的搜索服务器,既可以作为一个独立的服务部署,也可以签入Web应用中。SpringBoot作为Spring家族的全新框架,使得使用SpringBoot开发Spring应用变得非常简单。本文要介绍如...
    99+
    2023-05-31
    spring boot elasticsearch
  • springboot如何整合elasticsearch
    目录前言不同方式演示简单看一下集成先决配置Spring Data Elasticsearch方式ElasticsearchRestTemplate方式总结前言 推荐首先查看sprin...
    99+
    2023-05-18
    springboot整合elasticsearch springboot elasticsearch
  • ElasticSearch整合SpringBoot搭建配置
    目录前言项目搭建配置客户端索引API初探 & Index APIping创建索引 & create索引是否存在 & exist删除索引结束语前言 目前正在出...
    99+
    2023-02-22
    ElasticSearch整合SpringBoot ElasticSearch SpringBoot
  • SpringBoot整合ES高级查询方式
    目录1、配置2、API操作ES2.1 查询索引列表2.2 TermsQuery2.3 WildcardQuery2.4 RangeQuery2.5 MatchQuery2.6 Mul...
    99+
    2022-11-13
  • springboot整合quartz实例demo
    目录一、quartz简介二、demo结构三、代码详解四、代码效果一、quartz简介 1.Quartz是一个开源的任务调度框架。基于定时、定期的策略来执行任务是它的核心功能,比如x年...
    99+
    2022-11-13
  • 使用SpringBoot如何实现对ElasticSearch进行整合
    这篇文章给大家介绍使用SpringBoot如何实现对ElasticSearch进行整合,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、实体设计:Tutorial.javapublic class Tutorial i...
    99+
    2023-05-31
    springboot elasticsearch
  • SpringBoot整合MongoDB完整实例代码
    目录一、新建项目二、docker-compose 配置mongoDB三、SpringBoot配置MongoDB问题:Exception authenticating MongoCre...
    99+
    2022-11-13
  • SpringBoot整合JPA的实例代码
    JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。JPA 的目标之一是制定一个可以由很多供应商实现的API,并且开发人员可以编码来实现该...
    99+
    2023-05-31
    spring boot jpa
  • SpringBoot整合Pulsar的实现示例
    目录一、添加pom.xml依赖二、Pulsar 参数类三、Pulsar 配置类四、不同消费数据类型的监听器五、Pulsar的核心服务类六、Pulsar整合Spring Cloud一、...
    99+
    2022-11-13
  • Springboot整合FreeMarker的实现示例
    目录一、项目搭建1、新建模块2、导入依赖 :将不相关的依赖删掉3、新建软件包,放入student实体类4、新建StudentMapper接口5、Springboot04Applica...
    99+
    2022-11-13
  • Springboot整合https的实例代码
    目录1 简介2 密码学基础2.1 密码体制2.2 两种加密方式2.2.1 对称加密2.2.2 非对称加密2.3 证书3 Springboot整合HTTPS3.1 先让Web跑起来3....
    99+
    2022-11-13
  • springboot 整合 clickhouse的实现示例
    目录前言前置准备使用jdbc方式操作clickhouse与springboot的整合代码完整整合步骤前言 了解了clickhouse的基础概念和相关的理论之后,本篇将通过实例代码演示...
    99+
    2022-11-13
  • SpringBoot整合WebService的实现示例
    目录SpringBoot搭建WebService程序一、定义规范接口二、搭建WebService服务端三、搭建WebService客户端WebService是一种传统的SOA技术架构...
    99+
    2022-11-12
  • SpringBoot整合JWT的实现示例
    目录一. JWT简介二. Java实现JWT(SpringBoot方式整合)JWT总结一. JWT简介 1. 什么是JWT? JWT(JSON Web Token)是为了在...
    99+
    2022-11-12
  • SpringBoot整合Redis的实现示例
    目录1.需求说明2.整合实现2.1.创建Springboot工程2.2.redis配置3.编写测试类4.注意事项和细节1.需求说明 在 springboot 中 , 整合 redis...
    99+
    2023-01-28
    SpringBoot整合Redis SpringBoot Redis整合
  • springboot整合quartz实例分析
    这篇文章主要讲解了“springboot整合quartz实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot整合quartz实例分析”吧!一、quartz简介Quart...
    99+
    2023-06-29
  • 解决SpringBoot整合ElasticSearch遇到的连接问题
    SpringBoot整合ElasticSearch的连接问题 failed to load elasticsearch nodes : org.elasticsearch.clie...
    99+
    2022-11-12
  • springboot整合spring-retry的实现示例
    目录1、背景2、解决方案2.1 pom文件2.2 applicat启动类2.3 controller类2.4 service测试类(重点)2.5 项目启动2.6 使用swagger进...
    99+
    2022-11-12
  • Java springboot 整合 Nacos的代码实例
    本篇内容主要讲解“Java springboot 整合 Nacos的代码实例”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java springboot 整合 Nacos的代码实例”吧!Naco...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作