跨域搜索
This commit is contained in:
commit
3966773736
@ -98,15 +98,16 @@ public class EsearchUtils {
|
|||||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||||
// 2.创建BoolQueryBuilder对象
|
// 2.创建BoolQueryBuilder对象
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
// 3.设置boolQueryBuilder条件
|
// // 3.设置boolQueryBuilder条件
|
||||||
MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue());
|
// //MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue());
|
||||||
// 4.添加查询条件到boolQueryBuilder中
|
// MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("knowTitle", kmsSearchDto.getSearchValue());
|
||||||
boolQueryBuilder.must(matchPhraseQueryBuilder);
|
// // 4.添加查询条件到boolQueryBuilder中
|
||||||
|
// boolQueryBuilder.must(matchPhraseQueryBuilder);
|
||||||
if("1".equals(kmsSearchDto.getSearchBy())) {
|
if("1".equals(kmsSearchDto.getSearchBy())) {
|
||||||
boolQueryBuilder.should(QueryBuilders.termsQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue()));
|
boolQueryBuilder.should(QueryBuilders.termsQuery("knowTitle", kmsSearchDto.getSearchValue()));
|
||||||
}
|
}
|
||||||
else if("2".equals(kmsSearchDto.getSearchBy())) {
|
else if("2".equals(kmsSearchDto.getSearchBy())) {
|
||||||
boolQueryBuilder.should(QueryBuilders.wildcardQuery(kmsSearchDto.getSearchKey(), "*" + kmsSearchDto.getSearchValue() + "*"));
|
boolQueryBuilder.should(QueryBuilders.wildcardQuery("knowTitle", "*" + kmsSearchDto.getSearchValue() + "*"));
|
||||||
}
|
}
|
||||||
// 查询条件--->生成DSL查询语句
|
// 查询条件--->生成DSL查询语句
|
||||||
searchSourceBuilder.query(boolQueryBuilder);
|
searchSourceBuilder.query(boolQueryBuilder);
|
||||||
@ -119,7 +120,7 @@ public class EsearchUtils {
|
|||||||
// 设置排序规则
|
// 设置排序规则
|
||||||
String sortBy = kmsSearchDto.getSortBy();
|
String sortBy = kmsSearchDto.getSortBy();
|
||||||
SortOrder sortType = kmsSearchDto.getSortType().equals("1") ? SortOrder.DESC : SortOrder.ASC;
|
SortOrder sortType = kmsSearchDto.getSortType().equals("1") ? SortOrder.DESC : SortOrder.ASC;
|
||||||
searchSourceBuilder.sort(sortBy, sortType);
|
//searchSourceBuilder.sort(sortBy, sortType);
|
||||||
SearchRequest searchRequest = new SearchRequest(indexName);
|
SearchRequest searchRequest = new SearchRequest(indexName);
|
||||||
searchRequest.source(searchSourceBuilder);
|
searchRequest.source(searchSourceBuilder);
|
||||||
|
|
||||||
@ -133,6 +134,7 @@ public class EsearchUtils {
|
|||||||
/*for (Map.Entry<String, Object> stringObjectEntry : sourceAsMap.entrySet()) {
|
/*for (Map.Entry<String, Object> stringObjectEntry : sourceAsMap.entrySet()) {
|
||||||
System.out.println(sourceAsMap.get(stringObjectEntry.getKey()));
|
System.out.println(sourceAsMap.get(stringObjectEntry.getKey()));
|
||||||
}*/
|
}*/
|
||||||
|
System.out.println("1111");
|
||||||
T main = JSON.parseObject(JSON.toJSONString(sourceAsMap), clazz);
|
T main = JSON.parseObject(JSON.toJSONString(sourceAsMap), clazz);
|
||||||
rsList.add(main);
|
rsList.add(main);
|
||||||
}
|
}
|
||||||
|
@ -20,16 +20,16 @@ public class BannerApplicationRunner implements ApplicationRunner {
|
|||||||
public void run(ApplicationArguments args) {
|
public void run(ApplicationArguments args) {
|
||||||
ThreadUtil.execute(() -> {
|
ThreadUtil.execute(() -> {
|
||||||
ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒,保证输出到结尾
|
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" +
|
||||||
"但愿老死程序间,不愿匍匐客户前;\n\t" +
|
"但愿老死程序间,不愿屈膝客户前;\n\t" +
|
||||||
"宝马奔驰富者趣,地铁公交程序员。\n\t" +
|
"宝马奔驰富者趣,地铁公交码农缘。\n\t" +
|
||||||
"别人笑我忒疯癫,我笑他人程序烂;\n\t" +
|
"他人笑我太疯癫,我笑他人代码烂;\n\t" +
|
||||||
"不见满街小姐姐,哪个归得程序员?\n\t" +
|
"不见满街网红妹,哪个曾怜程序员?\n\t" +
|
||||||
"---------------------------------------------------------OK");
|
"-------------------------代码如诗心似弦, 昼夜不倦解难关。 屏前独坐灯如昼, 梦想成真在眼前。--------------OK");
|
||||||
|
|
||||||
// log.info("\n----------------------------------------------------------\n\t" +
|
// log.info("\n----------------------------------------------------------\n\t" +
|
||||||
// "项目启动成功!\n\t" +
|
// "项目启动成功!\n\t" +
|
||||||
|
@ -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.api.task.dto.BpmProcessInstanceCreateReqDTO;
|
||||||
import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum;
|
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.stereotype.Service;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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 cn.iocoder.yudao.module.bpm.dal.mysql.knows.CommentMapper;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
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 static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
|
||||||
import cn.iocoder.yudao.framework.common.util.elasticsearch.EsearchUtils;
|
import cn.iocoder.yudao.framework.common.util.elasticsearch.EsearchUtils;
|
||||||
|
|
||||||
@ -43,6 +56,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
|
|||||||
|
|
||||||
private boolean isEsFlag = true;
|
private boolean isEsFlag = true;
|
||||||
private String indexName = "knows_index";
|
private String indexName = "knows_index";
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RestHighLevelClient esClient;
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Long createKnowledge(Long userId,KnowledgeSaveReqVO createReqVO){
|
public Long createKnowledge(Long userId,KnowledgeSaveReqVO createReqVO){
|
||||||
@ -126,6 +142,7 @@ public class KnowledgeServiceImpl implements KnowledgeService {
|
|||||||
// return knowledgeMapper.selectPage(userId, pageReqVO);
|
// return knowledgeMapper.selectPage(userId, pageReqVO);
|
||||||
KnowledgeDO knowledgeDo = new KnowledgeDO();
|
KnowledgeDO knowledgeDo = new KnowledgeDO();
|
||||||
return EsearchUtils.selectForEs(kmsSearchDto,knowledgeDo,indexName,KnowledgeDO.class);
|
return EsearchUtils.selectForEs(kmsSearchDto,knowledgeDo,indexName,KnowledgeDO.class);
|
||||||
|
// return selectForEs(kmsSearchDto);
|
||||||
}
|
}
|
||||||
// ==================== 子表(评论) ====================
|
// ==================== 子表(评论) ====================
|
||||||
|
|
||||||
@ -173,4 +190,61 @@ public class KnowledgeServiceImpl implements KnowledgeService {
|
|||||||
knowledgeMapper.updateById(new KnowledgeDO().setId(id).setFlowStatus(status));
|
knowledgeMapper.updateById(new KnowledgeDO().setId(id).setFlowStatus(status));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public List<KnowledgeDO> 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<KnowledgeDO> rsList = Lists.newArrayList();
|
||||||
|
// try {
|
||||||
|
// SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
// //解析搜索结果
|
||||||
|
// for (SearchHit sh : response.getHits()) {
|
||||||
|
// Map<String, Object> sourceAsMap = sh.getSourceAsMap();
|
||||||
|
// /*for (Map.Entry<String, Object> 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;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user