From 6def2d88d3bcf1a24c7ae473dadef8cea4b07385 Mon Sep 17 00:00:00 2001 From: Pancaihua <75729660@qq.com> Date: Fri, 16 Aug 2024 14:45:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?elasticsearch=20=E7=9A=84client=E7=9A=84?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../banner/core/BannerApplicationRunner.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java index 2b1b4b9..5bee89b 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java @@ -20,16 +20,16 @@ public class BannerApplicationRunner implements ApplicationRunner { public void run(ApplicationArguments args) { ThreadUtil.execute(() -> { ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒,保证输出到结尾 - log.info("\n----------------程序猿------------------------------------------\n\t" + + log.info("\n-------------程序猿---------------------------------------------\n\t" + "写字楼里写字间,写字间里程序员; \n\t" + - "程序员来写程序,又拿程序换酒钱。\n\t" + - "酒醒只在网上坐,酒醉还在网下眠; \n\t" + - "酒醉酒醒日复日,网上网下年复年。\n\t" + - "但愿老死程序间,不愿匍匐客户前;\n\t" + - "宝马奔驰富者趣,地铁公交程序员。\n\t" + - "别人笑我忒疯癫,我笑他人程序烂;\n\t" + - "不见满街小姐姐,哪个归得程序员?\n\t" + - "---------------------------------------------------------OK"); + "程序员来写程序,代码换得几杯钱。\n\t" + + "酒醒只在屏前坐,酒醉依旧在线眠; \n\t" + + "醉醒如梦日复日,网上网下年复年。\n\t" + + "但愿老死程序间,不愿屈膝客户前;\n\t" + + "宝马奔驰富者趣,地铁公交码农缘。\n\t" + + "他人笑我太疯癫,我笑他人代码烂;\n\t" + + "不见满街网红妹,哪个曾怜程序员?\n\t" + + "----------代码如诗心似弦, 昼夜不倦解难关。 屏前独坐灯如昼, 梦想成真在眼前。-----------------------------OK"); // log.info("\n----------------------------------------------------------\n\t" + // "项目启动成功!\n\t" + From 36a39872bbabda2e5a85147db639461669a32fe4 Mon Sep 17 00:00:00 2001 From: Pancaihua <75729660@qq.com> Date: Mon, 19 Aug 2024 14:53:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?elasticsearch=20=E7=9A=84client=E7=9A=84?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/elasticsearch/EsearchUtils.java | 24 +++--- .../banner/core/BannerApplicationRunner.java | 2 +- .../service/knows/KnowledgeServiceImpl.java | 74 +++++++++++++++++++ 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java index 1dd6e1d..421b5e0 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java @@ -98,16 +98,17 @@ public class EsearchUtils { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 2.创建BoolQueryBuilder对象 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); - // 3.设置boolQueryBuilder条件 - MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue()); - // 4.添加查询条件到boolQueryBuilder中 - boolQueryBuilder.must(matchPhraseQueryBuilder); -// if("1".equals(kmsSearchDto.getSearchBy())) { -// boolQueryBuilder.should(QueryBuilders.termsQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue())); -// } -// else if("2".equals(kmsSearchDto.getSearchBy())) { -// boolQueryBuilder.should(QueryBuilders.wildcardQuery(kmsSearchDto.getSearchKey(), "*" + kmsSearchDto.getSearchValue() + "*")); -// } +// // 3.设置boolQueryBuilder条件 +// //MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue()); +// MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("knowTitle", kmsSearchDto.getSearchValue()); +// // 4.添加查询条件到boolQueryBuilder中 +// boolQueryBuilder.must(matchPhraseQueryBuilder); + if("1".equals(kmsSearchDto.getSearchBy())) { + boolQueryBuilder.should(QueryBuilders.termsQuery("knowTitle", kmsSearchDto.getSearchValue())); + } + else if("2".equals(kmsSearchDto.getSearchBy())) { + boolQueryBuilder.should(QueryBuilders.wildcardQuery("knowTitle", "*" + kmsSearchDto.getSearchValue() + "*")); + } // 查询条件--->生成DSL查询语句 searchSourceBuilder.query(boolQueryBuilder); Integer pageNum = 1; @@ -119,7 +120,7 @@ public class EsearchUtils { // 设置排序规则 String sortBy = kmsSearchDto.getSortBy(); SortOrder sortType = kmsSearchDto.getSortType().equals("1") ? SortOrder.DESC : SortOrder.ASC; - searchSourceBuilder.sort(sortBy, sortType); + //searchSourceBuilder.sort(sortBy, sortType); SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.source(searchSourceBuilder); @@ -133,6 +134,7 @@ public class EsearchUtils { /*for (Map.Entry stringObjectEntry : sourceAsMap.entrySet()) { System.out.println(sourceAsMap.get(stringObjectEntry.getKey())); }*/ + System.out.println("1111"); T main = JSON.parseObject(JSON.toJSONString(sourceAsMap), clazz); rsList.add(main); } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java index 5bee89b..babfb72 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java @@ -29,7 +29,7 @@ public class BannerApplicationRunner implements ApplicationRunner { "宝马奔驰富者趣,地铁公交码农缘。\n\t" + "他人笑我太疯癫,我笑他人代码烂;\n\t" + "不见满街网红妹,哪个曾怜程序员?\n\t" + - "----------代码如诗心似弦, 昼夜不倦解难关。 屏前独坐灯如昼, 梦想成真在眼前。-----------------------------OK"); + "-------------------------代码如诗心似弦, 昼夜不倦解难关。 屏前独坐灯如昼, 梦想成真在眼前。--------------OK"); // log.info("\n----------------------------------------------------------\n\t" + // "项目启动成功!\n\t" + diff --git a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java index d45ec14..b6b78ff 100644 --- a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java +++ b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java @@ -6,6 +6,18 @@ import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; +import com.alibaba.fastjson.JSON; +import org.apache.commons.compress.utils.Lists; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.search.sort.SortOrder; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import org.springframework.transaction.annotation.Transactional; @@ -21,6 +33,7 @@ import cn.iocoder.yudao.module.bpm.dal.mysql.knows.KnowledgeMapper; import cn.iocoder.yudao.module.bpm.dal.mysql.knows.CommentMapper; import javax.annotation.Resource; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +//import static cn.iocoder.yudao.framework.common.util.elasticsearch.EsearchUtils.esClient; import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*; import cn.iocoder.yudao.framework.common.util.elasticsearch.EsearchUtils; @@ -43,6 +56,9 @@ public class KnowledgeServiceImpl implements KnowledgeService { private boolean isEsFlag = true; private String indexName = "knows_index"; + + @Resource + private RestHighLevelClient esClient; @Override @Transactional(rollbackFor = Exception.class) public Long createKnowledge(Long userId,KnowledgeSaveReqVO createReqVO){ @@ -127,6 +143,7 @@ public class KnowledgeServiceImpl implements KnowledgeService { // return knowledgeMapper.selectPage(userId, pageReqVO); KnowledgeDO knowledgeDo = new KnowledgeDO(); return EsearchUtils.selectForEs(kmsSearchDto,knowledgeDo,indexName,KnowledgeDO.class); +// return selectForEs(kmsSearchDto); } // ==================== 子表(评论) ==================== @@ -174,4 +191,61 @@ public class KnowledgeServiceImpl implements KnowledgeService { knowledgeMapper.updateById(new KnowledgeDO().setId(id).setFlowStatus(status)); } +// public List selectForEs(KmsSearchDto kmsSearchDto) { +// kmsSearchDto.setSearchKey("knowTitle"); +// // 1.创建并设置SearchSourceBuilder对象 +// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); +// // 2.创建BoolQueryBuilder对象 +// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); +// // 3.设置boolQueryBuilder条件 +// //MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue()); +// // 4.添加查询条件到boolQueryBuilder中 +// //boolQueryBuilder.must(matchPhraseQueryBuilder); +// if("1".equals(kmsSearchDto.getSearchBy())) { +// boolQueryBuilder.should(QueryBuilders.termsQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue())); +// } +// else if("2".equals(kmsSearchDto.getSearchBy())) { +// boolQueryBuilder.should(QueryBuilders.wildcardQuery(kmsSearchDto.getSearchKey(), "*" + kmsSearchDto.getSearchValue() + "*")); +// } +// // 查询条件--->生成DSL查询语句 +// searchSourceBuilder.query(boolQueryBuilder); +// Integer pageNum = 1; +// Integer pageSize = kmsSearchDto.getPageSize(); +// // 第几页 +// searchSourceBuilder.from((pageNum - 1) * pageSize); +// // 每页多少条数据 +// searchSourceBuilder.size(pageSize); +// // 设置排序规则 +// String sortBy = kmsSearchDto.getSortBy(); +// SortOrder sortType = kmsSearchDto.getSortType().equals("1") ? SortOrder.DESC : SortOrder.ASC; +// //searchSourceBuilder.sort(sortBy, sortType); +// System.out.println("sortBy="+sortBy); +// System.out.println(sortType); +//// searchSourceBuilder.sort( +//// SortBuilders.fieldSort("createTime") +//// .unmappedType("date") +//// .order(SortOrder.DESC) +//// ); +// +// SearchRequest searchRequest = new SearchRequest("knows_index"); +// searchRequest.source(searchSourceBuilder); +// //返回信息 +// List rsList = Lists.newArrayList(); +// try { +// SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT); +// //解析搜索结果 +// for (SearchHit sh : response.getHits()) { +// Map sourceAsMap = sh.getSourceAsMap(); +// /*for (Map.Entry stringObjectEntry : sourceAsMap.entrySet()) { +// System.out.println(sourceAsMap.get(stringObjectEntry.getKey())); +// }*/ +// System.out.println("111111"); +// KnowledgeDO main = JSON.parseObject(JSON.toJSONString(sourceAsMap), KnowledgeDO.class); +// rsList.add(main); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } +// return rsList; +// } }