SpringBoot集成Elasticsearch7.4 实战(2)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Springboot集成Elasticsearch完成对文档数据的基本管理

elasticsearch

1. 数据管理

1.1. 新增数据

1.1.1. 单实例新增

http方式提交数据,案例中我将数据格式做了规范,提交过程中需要指定索引名,以及JSON格式数据,这样尽可能在实际过程中做到通用。

为此我用交互对象Vo来接受前端传递来的数据,再解析该Vo,获取要提交的目标索引。

  • 前端Web端实现

  

/**

 * @Description 新增数据

 * @param elasticDataVo

 * @return xyz.wongs.drunkard.base.message.response.ResponseResult

 * @throws

 * @date 2019/11/20 17:10

 */

@RequestMapping(value = "/add",method = RequestMethod.POST)

public ResponseResult add(@RequestBody ElasticDataVo elasticDataVo){

 ResponseResult response = getResponseResult();

 try {

 if(!StringUtils.isNotEmpty(elasticDataVo.getIdxName())){

 response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());

 response.setMsg("索引为空,不允许提交");

 response.setStatus(false);

 log.warn("索引为空");

 return response;

 }

 ElasticEntity elasticEntity = new ElasticEntity();

 elasticEntity.setId(elasticDataVo.getElasticEntity().getId());

 elasticEntity.setData(elasticDataVo.getElasticEntity().getData());

  

 baseElasticService.insertOrUpdateOne(elasticDataVo.getIdxName(), elasticEntity);

  

 } catch (Exception e) {

 response.setCode(ResponseCode.ERROR.getCode());

 response.setMsg("服务忙,请稍后再试");

 response.setStatus(false);

 log.error("插入数据异常,metadataVo={},异常信息={}", elasticDataVo.toString(),e.getMessage());

 }

 return response;

}
  • ElasticDataVo类

  

package xyz.wongs.drunkard.palant.vo;

  

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import xyz.wongs.drunkard.base.entiy.ElasticEntity;

  

/**

 * @ClassName ElasticDataVo

 * @Description http交互Vo对象

 * @author WCNGS@QQ.COM

 * @Github <a>https://github.com/rothschil</a>

 * @date 2019/11/21 9:10

 * @Version 1.0.0

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class ElasticDataVo<T> {

  

 /**

 * 索引名

 */

 private String idxName;

 /**

 * 数据存储对象

 */

 private ElasticEntity elasticEntity;

  

}

  
  
  • ElasticEntity类

  

package xyz.wongs.drunkard.base.entiy;

  

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import xyz.wongs.drunkard.base.persistence.mybatis.entity.BaseEntity;

  

import java.util.Map;

  

/**

 * @ClassName ElasticEntity

 * @Description  数据存储对象

 * @author WCNGS@QQ.COM

 * @Github <a>https://github.com/rothschil</a>

 * @date 2019/11/21 9:10

 * @Version 1.0.0

*/

@Data

@AllArgsConstructor

@NoArgsConstructor

public class ElasticEntity<T> {

  

 /**

 * 主键标识,用户ES持久化

 */

 private String id;

  

 /**

 * JSON对象,实际存储数据

 */

 private Map data;

}

  
  • Postman提交

测试用例

1.1.2. 批量提交

1.2. 条件查询

为了通用,我在web端也分装了一层Vo,为了演示需要我仅写一个 match 精确匹配的查询。

  • 前端web实现

/**

 * @Description

 * @param queryVo 查询实体对象

 * @return xyz.wongs.drunkard.base.message.response.ResponseResult

 * @throws

 * @date 2019/11/21 9:31

 */

@RequestMapping(value = "/get",method = RequestMethod.GET)

public ResponseResult get(@RequestBody QueryVo queryVo){

  

 ResponseResult response = getResponseResult();

  

 if(!StringUtils.isNotEmpty(queryVo.getIdxName())){

 response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());

 response.setMsg("索引为空,不允许提交");

 response.setStatus(false);

 log.warn("索引为空");

 return response;

 }

  

 try {

 Class<?> clazz = ElasticUtil.getClazz(queryVo.getClassName());

 Map<String,Object> params = queryVo.getQuery().get("match");

 Set<String> keys = params.keySet();

 MatchQueryBuilder queryBuilders=null;

 for(String ke:keys){

 queryBuilders = QueryBuilders.matchQuery(ke, params.get(ke));

 }

 if(null!=queryBuilders){

 SearchSourceBuilder searchSourceBuilder = ElasticUtil.initSearchSourceBuilder(queryBuilders);

 List<?> data = baseElasticService.search(queryVo.getIdxName(),searchSourceBuilder,clazz);

 response.setData(data);

 }

 } catch (Exception e) {

 response.setCode(ResponseCode.ERROR.getCode());

 response.setMsg("服务忙,请稍后再试");

 response.setStatus(false);

 log.error("查询数据异常,metadataVo={},异常信息={}", queryVo.toString(),e.getMessage());

 }

 return response;

}

  
  • QueryVo

  

/** 查询Vo对象

 * @ClassName QueryVo

 * @Description

 * @author WCNGS@QQ.COM

 * @Github <a>https://github.com/rothschil</a>

 * @date 2019/10/25 23:16

 * @Version 1.0.0

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class QueryVo {

  

 /**

 * 索引名

 */

 private String idxName;

 /**

 * 需要反射的实体类型,用于对查询结果的封装

 */

 private String className;

 /**

 * 具体条件

 */

 private Map<String,Map<String,Object>> query;

}

  
  • Postman提交

条件查询

1.3. 删除数据

1.3.1. 单实例删除


/**

 * @Description 删除

 * @param elasticDataVo

 * @return xyz.wongs.drunkard.base.message.response.ResponseResult

 * @throws

 * @date 2019/11/21 9:56

 */

@RequestMapping(value = "/delete",method = RequestMethod.POST)

public ResponseResult delete(@RequestBody ElasticDataVo elasticDataVo){

 ResponseResult response = getResponseResult();

 try {

 if(!StringUtils.isNotEmpty(elasticDataVo.getIdxName())){

 response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());

 response.setMsg("索引为空,不允许提交");

 response.setStatus(false);

 log.warn("索引为空");

 return response;

 }

 baseElasticService.deleteOne(elasticDataVo.getIdxName(),elasticDataVo.getElasticEntity());

 } catch (Exception e) {

 e.printStackTrace();

 }

 return response;

  

}

删除数据

1.3.2. 批量删除

2. 源码

Github演示源码 ,记得给Star

Gitee演示源码,记得给Star

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
3天前
|
Java 应用服务中间件 测试技术
深入探索Spring Boot Web应用源码及实战应用
【5月更文挑战第11天】本文将详细解析Spring Boot Web应用的源码架构,并通过一个实际案例,展示如何构建一个基于Spring Boot的Web应用。本文旨在帮助读者更好地理解Spring Boot的内部工作机制,以及如何利用这些机制优化自己的Web应用开发。
14 3
|
4天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
11 1
|
3天前
|
消息中间件 JSON Java
RabbitMQ的springboot项目集成使用-01
RabbitMQ的springboot项目集成使用-01
|
3天前
|
搜索推荐 Java 数据库
springboot集成ElasticSearch的具体操作(系统全文检索)
springboot集成ElasticSearch的具体操作(系统全文检索)
|
4天前
|
消息中间件 Java Spring
Springboot 集成Rabbitmq之延时队列
Springboot 集成Rabbitmq之延时队列
4 0
|
4天前
|
网络协议 Java Spring
Springboot 集成websocket
Springboot 集成websocket
8 0
|
4天前
|
XML Java 数据库
springboot集成flowable
springboot集成flowable
|
19天前
|
Java Maven 开发工具
【ElasticSearch 】IK 分词器安装
【ElasticSearch 】IK 分词器安装
23 1
|
1月前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
1月前
|
Java Windows
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
43 0