杭州公司展廳設(shè)計(jì)公司網(wǎng)站seo優(yōu)化分析
請(qǐng)確保已有可用的ES,若沒(méi)有,請(qǐng)移步:Docker安裝部署ElasticSearch(ES)
新建SpringBoot項(xiàng)目
這里是用的springboot版本是2.6.0
引入依賴
<!-- 排除springboot中內(nèi)置的es依賴,以防和easy-es中的依賴沖突--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></exclusion><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.14.0</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.14.0</version></dependency><dependency><groupId>cn.easy-es</groupId><artifactId>easy-es-boot-starter</artifactId><version>2.0.0-beta1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
配置文件
注意修改address信息,該為自己的ES地址
easy-es:enable: trueaddress : ES地址:9200global-config:process_index_mode: manual
項(xiàng)目結(jié)構(gòu)
listener部分我們先不用管
啟動(dòng)類
添加掃描ESMapper的注解,指定路徑
@EsMapperScan("com.mine.easyEs.mapper")
實(shí)體類
@Data
public class Document {@Id/*** es中的唯一id*/private String id;/*** 文檔標(biāo)題*/private String title;/*** 文檔內(nèi)容*/private String content;/*** 創(chuàng)建時(shí)間*/private Date createTime;
}
Controller類
包括對(duì)索引操作和對(duì)數(shù)據(jù)進(jìn)行操作的接口
@RestController
@RequestMapping("/ee")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DocumentController {private final IDocumentService documentService;/*** 創(chuàng)建索引* @return 結(jié)果信息* @throws Exception*/@GetMapping("/createIndex")public String createIndex() throws Exception {return documentService.createIndex();}/*** 刪除索引* @return 結(jié)果信息*/@GetMapping("/deleteIndex")public String deleteIndex(){return documentService.deleteIndex();}/*** 查詢ES所有數(shù)據(jù)* @return 查詢Document結(jié)果對(duì)象集合*/@GetMapping("/findAll")public List<Document> findAll(){return documentService.findAllData();}/*** ES新增數(shù)據(jù)* @param document 新增數(shù)據(jù)對(duì)象* @return 結(jié)果信息* @throws Exception*/@GetMapping("/add")public String addData(Document document) throws Exception {return documentService.addData(document);}/*** 修改ES數(shù)據(jù)* @param document 修改數(shù)據(jù)對(duì)象*/@GetMapping("/update")public String updateData(Document document){return documentService.updateData(document);}/*** 根據(jù)id刪除ES數(shù)據(jù)* @param id 需要?jiǎng)h除的數(shù)據(jù)的id* @return*/@GetMapping("/delete")public String deleteData(String id){return documentService.deleteDataById(id);}/*** 分詞匹配查詢content字段* @param value 查詢內(nèi)容* @return*/@GetMapping("/match")public List<Document> findMatch(String value){return documentService.findMatch(value);}}
Mapper接口
繼承BaseMapper,整體操作都與MybatisPlus類似
public interface DocumentMapper extends BaseEsMapper<Document> {
}
Serice接口
public interface IDocumentService {/*** 查詢ES所有數(shù)據(jù)* @return 查詢Document結(jié)果對(duì)象集合*/List<Document> findAllData();/*** 創(chuàng)建索引* @return 結(jié)果信息* @throws Exception*/String createIndex() throws Exception;/*** 刪除索引* @return 結(jié)果信息*/String deleteIndex();/*** ES新增數(shù)據(jù)* @param document 新增數(shù)據(jù)實(shí)體類* @return 結(jié)果信息* @throws Exception*/String addData(Document document) throws Exception;/*** 根據(jù)id刪除ES數(shù)據(jù)* @param id 需要?jiǎng)h除的數(shù)據(jù)的id* @return*/String deleteDataById(String id);/*** 修改ES數(shù)據(jù)* @param document 修改數(shù)據(jù)對(duì)象*/String updateData(Document document);/*** 分詞匹配查詢content字段* @param value 查詢內(nèi)容* @return*/List<Document> findMatch(String value);
}
Service實(shí)現(xiàn)類
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DocumentServiceImpl implements IDocumentService {private final DocumentMapper documentMapper;/*** 查詢ES所有數(shù)據(jù)* @return 查詢Document結(jié)果對(duì)象集合*/@Overridepublic List<Document> findAllData() {LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();wrapper.matchAllQuery();return documentMapper.selectList(wrapper);}/*** 創(chuàng)建索引* @return 結(jié)果信息* @throws Exception*/@Overridepublic String createIndex() throws Exception {StringBuilder msg = new StringBuilder();String indexName = Document.class.getSimpleName().toLowerCase();boolean existsIndex = documentMapper.existsIndex(indexName);if (existsIndex){throw new Exception("Document實(shí)體對(duì)應(yīng)索引已存在,刪除索引接口:deleteIndex");}boolean success = documentMapper.createIndex();if (success){msg.append("Document索引創(chuàng)建成功");}else {msg.append("索引創(chuàng)建失敗");}return msg.toString();}/*** 刪除索引* @return 結(jié)果信息*/@Overridepublic String deleteIndex() {StringBuilder msg = new StringBuilder();String indexName = Document.class.getSimpleName().toLowerCase();if (documentMapper.deleteIndex(indexName)){msg.append("刪除成功");}else {msg.append("刪除失敗");}return msg.toString();}/*** ES新增數(shù)據(jù)* @param document 新增數(shù)據(jù)實(shí)體類* @return 結(jié)果信息* @throws Exception*/@Overridepublic String addData(Document document) throws Exception {if (StringUtils.isEmpty(document.getTitle()) || StringUtils.isEmpty(document.getContent())) {throw new Exception("請(qǐng)補(bǔ)全title及content數(shù)據(jù)");}document.setCreateTime(new Date());documentMapper.insert(document);return "Added successfully!";}/*** 根據(jù)id刪除ES數(shù)據(jù)* @param id 需要?jiǎng)h除的數(shù)據(jù)的id* @return*/@Overridepublic String deleteDataById(String id) {documentMapper.deleteById(id);return "Success";}/*** 修改ES數(shù)據(jù)* @param document 修改數(shù)據(jù)對(duì)象*/@Overridepublic String updateData(Document document) {documentMapper.updateById(document);return "Success";}/*** 分詞匹配查詢content字段* @param value 查詢內(nèi)容* @return*/@Overridepublic List<Document> findMatch(String value) {LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();wrapper.match(Document::getContent,value);wrapper.orderByDesc(Document::getCreateTime);List<Document> documents = documentMapper.selectList(wrapper);return documents;}
}
啟動(dòng)、測(cè)試
使用PostMan測(cè)試
刪除索引
創(chuàng)建索引
添加數(shù)據(jù)
查看所有數(shù)據(jù)
修改數(shù)據(jù)
刪除數(shù)據(jù)
測(cè)試完成