具体有:
[这些操作都是在CentOS下操作的,主要带大家熟悉下Elasticsearch环境]
- 1.1.下载& Linux下ElasticSearch安装
- 1.2.中文分词插件IK
- 1.3.索引
- 1.4.如何数据管理
[在Springboot环境下,利用JAVA环境操作索引]
- 2.1.新增索引
- 2.2.查询索引
- 2.3.删除索引
[在Springboot环境下,管理数据]
- 3.1.WEB HTTP提交数据<单条提交、批量提交>
- 3.2.WEB HTTP方式条件查询
- 3.3.WEB HTTP删除数据
1. 数据管理
1.1. 新增数据
1.1.1. 单实例新增
http方式提交数据,案例中我将数据格式做了规范,提交过程中需要指定索引名,以及JSON格式数据,这样尽可能在实际过程中做到通用。
为此我用交互对象Vo来接受前端传递来的数据,再解析该Vo,获取要提交的目标索引。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
@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; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package xyz.wongs.weathertop.palant.vo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import xyz.wongs.weathertop.base.entiy.ElasticEntity;
@Data @NoArgsConstructor @AllArgsConstructor public class ElasticDataVo<T> {
private String idxName;
private ElasticEntity elasticEntity;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package xyz.wongs.weathertop.base.entiy;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import xyz.wongs.weathertop.base.persistence.mybatis.entity.BaseEntity;
import java.util.Map;
@Data @AllArgsConstructor @NoArgsConstructor public class ElasticEntity<T> {
private String id;
private Map data; }
|

1.1.2. 批量提交
1.2. 条件查询
为了通用,我在web端也分装了一层Vo,为了演示需要我仅写一个 match 精确匹配的查询。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
@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; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
@Data @NoArgsConstructor @AllArgsConstructor public class QueryVo {
private String idxName;
private String className;
private Map<String,Map<String,Object>> query; }
|

1.3. 删除数据
1.3.1. 单实例删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
@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
3. 相关章节
一、SpringBoot集成Elasticsearch7.4 实战(一)
二、SpringBoot集成Elasticsearch7.4 实战(二)
三、SpringBoot集成Elasticsearch7.4 实战(三)