add 增加 搜索引擎 crud 演示案例

2.X
疯狂的狮子li 3 years ago
parent f1f06a7421
commit 4c5ae5f4f0

@ -93,6 +93,10 @@
<!-- <artifactId>tencentcloud-sdk-java</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>

@ -0,0 +1,86 @@
package com.ruoyi.demo.controller;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.domain.Document;
import com.ruoyi.demo.esmapper.DocumentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* crud
*
* @author Lion Li
*/
@RestController
@RequiredArgsConstructor
public class EsCrudController {
private final DocumentMapper documentMapper;
/**
* ()
*
* @param title
*/
@GetMapping("/select")
public Document select(String title) {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getTitle, title);
return documentMapper.selectOne(wrapper);
}
/**
* ()
*
* @param key
*/
@GetMapping("/search")
public List<Document> search(String key) {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.like(Document::getTitle, key);
return documentMapper.selectList(wrapper);
}
/**
*
*/
@PostMapping("/insert")
public Integer insert(@RequestBody Document document) {
return documentMapper.insert(document);
}
/**
*
*/
@PutMapping("/update")
public R<Void> update(@RequestBody Document document) {
// 测试更新 更新有两种情况 分别演示如下:
// case1: 已知id, 根据id更新 (为了演示方便,此id是从上一步查询中复制过来的,实际业务可以自行查询)
documentMapper.updateById(document);
// case2: id未知, 根据条件更新
// LambdaEsUpdateWrapper<Document> wrapper = new LambdaEsUpdateWrapper<>();
// wrapper.like(Document::getTitle, document.getTitle());
// Document document2 = new Document();
// document2.setTitle(document.getTitle());
// document2.setContent(document.getContent());
// documentMapper.update(document2, wrapper);
return R.ok();
}
/**
*
*
* @param id
*/
@DeleteMapping("/delete/{id}")
public R<Integer> delete(@PathVariable String id) {
// 测试删除数据 删除有两种情况:根据id删或根据条件删
return R.ok(documentMapper.deleteById(id));
}
}

@ -0,0 +1,25 @@
package com.ruoyi.demo.domain;
import lombok.Data;
/**
*
*/
@Data
public class Document {
/**
* esid
*/
private String id;
/**
*
*/
private String title;
/**
*
*/
private String content;
}

@ -0,0 +1,7 @@
package com.ruoyi.demo.esmapper;
import cn.easyes.core.conditions.interfaces.BaseEsMapper;
import com.ruoyi.demo.domain.Document;
public interface DocumentMapper extends BaseEsMapper<Document> {
}

@ -45,3 +45,51 @@ spring:
url: ${datasource.system-master.url}
username: ${datasource.system-master.username}
password: ${datasource.system-master.password}
--- # elasticsearch 功能配置
# 文档地址: https://www.easy-es.cn/
# 更改包名需要去 EasyEsConfiguration 修改包扫描(后续版本支持配置文件读取)
easy-es:
# 是否开启EE自动配置
enable: true
# es连接地址+端口 格式必须为ip:port,如果是集群则可用逗号隔开
address : localhost:9200
# 默认为http
schema: http
# 注意ES建议使用账号认证 不使用会报警告日志
#如果无账号密码则可不配置此行
#username:
#如果无账号密码则可不配置此行
#password:
# 心跳策略时间 单位:ms
keep-alive-millis: 18000
# 连接超时时间 单位:ms
connectTimeout: 5000
# 通信超时时间 单位:ms
socketTimeout: 5000
# 请求超时时间 单位:ms
requestTimeout: 5000
# 连接请求超时时间 单位:ms
connectionRequestTimeout: 5000
# 最大连接数 单位:个
maxConnTotal: 100
# 最大连接路由数 单位:个
maxConnPerRoute: 100
global-config:
# 开启控制台打印通过本框架生成的DSL语句,默认为开启,测试稳定后的生产环境建议关闭,以提升少量性能
print-dsl: true
# 异步处理索引是否阻塞主线程 默认阻塞 数据量过大时调整为非阻塞异步进行 项目启动更快
asyncProcessIndexBlocking: true
db-config:
# 是否开启下划线转驼峰 默认为false
map-underscore-to-camel-case: true
# id生成策略 customize为自定义,id值由用户生成,比如取MySQL中的数据id,如缺省此项配置,则id默认策略为es自动生成
id-type: customize
# 字段更新策略 默认为not_null
field-strategy: not_null
# 默认开启,查询若指定了size超过1w条时也会自动开启,开启后查询所有匹配数据,若不开启,会导致无法获取数据总条数,其它功能不受影响.
enable-track-total-hits: true
# 数据刷新策略,默认为不刷新
refresh-policy: immediate
# 是否全局开启must查询类型转换为filter查询类型 默认为false不转换
enable-must2-filter: false

Loading…
Cancel
Save