es搜索引擎的个人理解:将数据库中 ,在需要做全文检索的业务数据同步一份到es搜索引擎里面,建立在同一个索引下的数据集。
1.项目引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.建立es与数据库数据的实体类
package com.test.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.Date;
/**
- Es 索引库实体
* - @author wangwei
@date 2023-07-31
*/
@Document(indexName = "rabbit1")
@Data
public class EsModel {/** 索引id/
@Field(type = FieldType.Long)
private Long id;/** es中对应的数据库业务单据id*/
@Field(analyzer = "ik_max_word")
private Long ctId;/** es中业务数据 对应的业务数据库表名称*/
@Field(analyzer = "ik_max_word")
private String ctTableName;/** es中业务数据 对应的 业务简称*/
@Field(analyzer = "ik_max_word")
private String ctName;
/**** es中业务数据 对应的 业务内容详细信息*/
@Field(analyzer = "ik_max_word")
private String ctContent;
/** 备注 */
@Field(analyzer = "ik_max_word")
private String remark;
/** 备用1 */
@Field(analyzer = "ik_max_word")
private String by1;
/** 备用2 */
@Field(analyzer = "ik_max_word")
private String by2;
/** 备用3 */
@Field(analyzer = "ik_max_word")
private String by3;
/** 备用4 */
@Field(analyzer = "ik_max_word")
private String by4;
/** 备用5 */
@Field(analyzer = "ik_max_word")
private String by5;
}
3.建立es操作接口
package com.test.repository;
import com.test.model.EsModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
- @author wangwei
- @date 2023-07-31 14:12:36
*/
@Repository
public interface EsRepository extends ElasticsearchRepository {
/****
* 删除es操作
*/
public void deleteById(Long id);
}
4.具体业务使用
package com.test.business.service.impl;
import java.io.IOException;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.test.business.domain.;
import com.test.business.domain.dto.TbAjkDto;
import com.test.business.domain.dto.TbAjkExportDto;
import com.test.business.mapper.;
import com.test.common.exception.CustomException;
import com.test.constant.EsConstants;
import com.test.model.EsModel;
import com.test.model.TbAjkEs;
import com.test.model.TbAjkEsRepository;
import com.test.repository.EsRepository;
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.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import com.test.business.service.ITbAjkService;
import org.springframework.transaction.annotation.Transactional;
/**
- 库详情Service业务层处理
* - @author wangwei
- @date 2023-07-18
*/
@Service
public class TbAjkServiceImpl implements ITbAjkService {
@Autowired
private TbAjkMapper tbAjkMapper;
@Autowired
private TbAjgjcMapper tbAjgjcMapper;
@Autowired
private TbAjxyrMapper tbAjxyrMapper;
@Autowired
private TbAjxxlMapper tbAjxxlMapper;
@Autowired
private TbAjwllMapper tbAjwllMapper;
@Autowired
private TbAjzjlMapper tbAjzjlMapper;
@Autowired
private TbAjtxlMapper tbAjtxlMapper;
@Autowired
private PublicSqlMapper publicSqlMapper;
@Autowired
private TbAjkEsRepository tbAjkEsRepository;
@Autowired
private EsRepository esRepository;
@Autowired
private RestHighLevelClient client;
/**
* 查询库详情
*
* @param id 详情主键
* @return 库详情
*/
@Override
public TbAjk selectTbAjkById(Long id) {
return tbAjkMapper.selectTbAjkByCtId(id);
}
/**
* 查询详情列表
*
* @param 库详情
* @return 库详情
*/
@Override
public List<TbAjk> selectTbAjkList(TbAjk tbAjk) {
return tbAjkMapper.selectTbAjkList(tbAjk);
}
/**
* 新增库详情
* 新增es中的信息
*
* @param tbAjk 详情
* @return 结果
*/
@Transactional(rollbackFor = Exception.class)
@Override
public int insertTbAjk(TbAjk tbAjk) {
int i = tbAjkMapper.insertTbAjk(tbAjk);
if (i == 1) {
EsModel esModel = new EsModel();
Integer integer = publicSqlMapper.selectInsertId();
esModel.setId(Long.valueOf(integer));
esModel.setCtId(Long.valueOf(integer));
esModel.setCtTableName("tb_ajk");
esModel.setCtName(tbAjk.getAjmc());
esModel.setCtContent(tbAjk.getCjsy());//处事由
esModel.setBy1(tbAjk.getLaay());//案由
esModel.setBy2(tbAjk.getAjlb());//类别
esModel.setBy3(tbAjk.getCjnr());//内容
esModel.setBy4(tbAjk.getContent());//出内容
esModel.setBy5(tbAjk.getCbpcs());//承办
esModel.setRemark(tbAjk.getJyaq());//简要情
try {
esRepository.save(esModel);
} catch (Exception e) {
throw new CustomException("ES数据同步失败,请联系管理员处理!");
}
} else {
throw new CustomException("数据库操作失败,请联系管理员处理!");
}
return 1;
}
/**
* 修改库详情
* 修改es中的信息
* es: 1. 查询es中的数据
* es: 2. 修改es中的数据
*
* @param 库详情
* @return 结果
*/
@Transactional(rollbackFor = Exception.class)
@Override
public int updateTbAjk(TbAjk tbAjk) {
int i = tbAjkMapper.updateTbAjk(tbAjk);
if (i == 1) {
// public UpdateRequest(String index, String type, String id) {
// es-update https://www.jb51.net/article/246798.htm
UpdateRequest updateRequest = new UpdateRequest(EsConstants.ES_INDEX, EsConstants.ES_INDEX_TYPE, String.valueOf(tbAjk.getCtId()));
EsModel esModel = new EsModel();
esModel.setId(Long.valueOf(tbAjk.getCtId()));
esModel.setCtId(Long.valueOf(tbAjk.getCtId()));
esModel.setCtName(tbAjk.getAjmc());
esModel.setCtContent(tbAjk.getCjsy());//事由
esModel.setBy1(tbAjk.getLaay());//案由
esModel.setBy2(tbAjk.getAjlb());//类别
esModel.setBy3(tbAjk.getCjnr());//内容
esModel.setBy4(tbAjk.getContent());//出内容
esModel.setBy5(tbAjk.getCbpcs());//承办
esModel.setRemark(tbAjk.getJyaq());//简要情
//更新 将对象转换为json
updateRequest.doc(JSON.toJSONString(esModel), XContentType.JSON);
//客户端发送请求,进行更新
UpdateResponse update = null;
try {
update = client.update(updateRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new CustomException("ES数据同步失败可能原因是es中的业务ctId在数据库中不存在,请查看es客户端查询验证,请联系管理员处理!");
}
if (!"OK".equals(update.status().toString())) {
throw new CustomException("ES数据同步失败,请联系管理员处理!");
}
} else {
throw new CustomException("数据库操作失败,请联系管理员处理!");
}
return 1;
}
/**
* 批量删除库详情
*
* @param ids 需要删除的详情主键
* @return 结果
*/
@Transactional(rollbackFor = Exception.class)
@Override
public int deleteTbAjkByIds(Long[] ids) {
int i = tbAjkMapper.deleteTbAjkByCtIds(ids);
if (i != 1) {
throw new CustomException("数据库操作失败,请联系管理员处理!");
}
for (int ii = 0; ii < ids.length; ii++) {
try {
esRepository.deleteById(ids[ii]);
} catch (Exception e) {
throw new CustomException("ES同步数据失败,请联系管理员处理!");
}
}
return 1;
}
/**
* 删除详情信息
*
* @param id 详情主键
* @return 结果
*/
@Transactional(rollbackFor = Exception.class)
@Override
public int deleteTbAjkById(Long id) {
int i = tbAjkMapper.deleteTbAjkByCtId(id);
if (i != 1) {
throw new CustomException("数据库操作失败,请联系管理员处理!");
}
try {
esRepository.deleteById(id);
} catch (Exception e) {
throw new CustomException("ES同步数据失败,请联系管理员处理!");
}
return 1;
}
}
5.es查询说明
5.1 es案字段key查询
@Override
public AjaxResult searchES(String key) {
Pageable pageable = null;
Page byAjmc = tbAjkEsRepository.findByAjmc(key, pageable);
return AjaxResult.success(byAjmc);
}
5.2 全文检索(高阶用法)
说明:按业务需要在withQuery(matchQuery("", key))中天剑需要检索的字段键值,进行全文检索
/*
* //全文检索
* @param key
* @return
*/
@Override
public AjaxResult searchWhole(String key) {
Pageable pageable = null;
if (StringUtils.isEmpty(key)) {
System.out.println("key is null");
}
//全文检索
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchQuery("ctName", key))
.withQuery(matchQuery("ctContent", key))
.withQuery(matchQuery("remark", key))
// .withPageable(pageable)
.build();
Page<EsModel> esModels = elasticsearchRestTemplate.queryForPage(searchQuery, EsModel.class);
return AjaxResult.success(esModels);
}
6.es客户端查看数据的操作