elasticsearch 的client的完善
This commit is contained in:
parent
50d7208e05
commit
36a39872bb
@ -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<String, Object> 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);
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public class BannerApplicationRunner implements ApplicationRunner {
|
||||
"宝马奔驰富者趣,地铁公交码农缘。\n\t" +
|
||||
"他人笑我太疯癫,我笑他人代码烂;\n\t" +
|
||||
"不见满街网红妹,哪个曾怜程序员?\n\t" +
|
||||
"----------代码如诗心似弦, 昼夜不倦解难关。 屏前独坐灯如昼, 梦想成真在眼前。-----------------------------OK");
|
||||
"-------------------------代码如诗心似弦, 昼夜不倦解难关。 屏前独坐灯如昼, 梦想成真在眼前。--------------OK");
|
||||
|
||||
// log.info("\n----------------------------------------------------------\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.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<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