Skip to content

Commit

Permalink
add:add new es search controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrettluo committed Apr 27, 2024
1 parent b60a06d commit e38f014
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/com/jiaruiblog/controller/DocumentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ public BaseApiResult list(@RequestBody DocumentDTO documentDTO)
return iFileService.list(documentDTO);
}

@ApiOperation(value = "2.1 查询文档的分页列表页,限制了分类和标签", notes = "根据参数查询文档列表")
@PostMapping(value = "/listNew")
public BaseApiResult listNew(@RequestBody DocumentDTO documentDTO)
throws IOException {
String userId = documentDTO.getUserId();
if (StringUtils.hasText(documentDTO.getFilterWord()) &&
documentDTO.getType() == FilterTypeEnum.FILTER) {
String filterWord = documentDTO.getFilterWord();
//非法敏感词汇判断
SensitiveFilter filter = SensitiveFilter.getInstance();
int n = filter.checkSensitiveWord(filterWord, 0, 1);
//存在非法字符
if (n > 0) {
// todo 非法字符的计算可能不准确:
// NullPointer, Null, java, on
log.error("这个人输入了非法字符--> {},不知道他到底要查什么~", filterWord);
} else {
redisService.incrementScoreByUserId(filterWord, RedisServiceImpl.SEARCH_KEY);
if (StringUtils.hasText(userId)) {
redisService.addSearchHistoryByUserId(userId, filterWord);
}
}
}
return iFileService.listNew(documentDTO);
}

@ApiOperation(value = "2.2 查询文档的详细信息", notes = "查询文档的详细信息")
@GetMapping(value = "/detail")
public BaseApiResult detail(@RequestParam(value = "docId") String id) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/jiaruiblog/entity/vo/DocumentVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public class DocumentVO {

private Date createTime;

private List<PageVO> pageVOList;

}
21 changes: 21 additions & 0 deletions src/main/java/com/jiaruiblog/entity/vo/PageVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.jiaruiblog.entity.vo;

import lombok.Data;

/**
* @ClassName PageVO
* @Description TODO
* @Author luojiarui
* @Date 2024/4/23 22:33
* @Version 1.0
**/
@Data
public class PageVO {

private Integer order;

private String content;

private Integer page;

}
12 changes: 12 additions & 0 deletions src/main/java/com/jiaruiblog/service/ElasticService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.jiaruiblog.service;

import com.jiaruiblog.entity.FileDocument;
import com.jiaruiblog.entity.vo.PageVO;
import com.jiaruiblog.util.BaseApiResult;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author jiarui.luo
Expand All @@ -19,6 +22,15 @@ public interface ElasticService {
*/
List<FileDocument> search(String keyword) throws IOException;

/**
* search
* @param keyword String
* @param docIdSet 限制其
* @return result
* @throws IOException exception
*/
Map<String, List<PageVO>> search(String keyword, Set<String> docIdSet) throws IOException;

BaseApiResult getWordStat() throws IOException;

}
10 changes: 10 additions & 0 deletions src/main/java/com/jiaruiblog/service/IFileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;


/**
Expand Down Expand Up @@ -111,6 +112,8 @@ BaseApiResult uploadByUrl(String category, List<String> tags, String name,
*/
FileDocument getByMd5(String md5);

List<FileDocument> getByMd5Set(Set<String> md5Set);

/**
* queryById
* @param docId String
Expand Down Expand Up @@ -152,6 +155,13 @@ BaseApiResult uploadByUrl(String category, List<String> tags, String name,
*/
BaseApiResult list(DocumentDTO documentDTO);

/**
* 分页检索目前的文档信息
* @param documentDTO DocumentDTO
* @return result
*/
BaseApiResult listNew(DocumentDTO documentDTO);

/**
*根据文档的详情,查询该文档的详细信息
*
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/jiaruiblog/service/impl/ElasticServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.jiaruiblog.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.jiaruiblog.entity.FileDocument;
import com.jiaruiblog.entity.FileObj;
import com.jiaruiblog.entity.data.WordCloudItem;
import com.jiaruiblog.entity.vo.PageVO;
import com.jiaruiblog.service.ElasticService;
import com.jiaruiblog.util.BaseApiResult;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -184,6 +186,77 @@ public List<FileDocument> search(String keyword) throws IOException {
return fileDocumentList;
}

@Override
public Map<String, List<PageVO>> search(String keyword, Set<String> docIdSet) throws IOException {
// 查询es后返回的内容
Map<String, List<PageVO>> result = new HashMap<>();
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
// 使用lk分词器查询,会把插入的字段分词,然后进行处理
SearchSourceBuilder srb = new SearchSourceBuilder();
srb.query(QueryBuilders.matchPhraseQuery(PIPELINE_NAME, keyword));

// 每页10个数据
srb.size(10);
// 起始位置从0开始
srb.from(0);

//设置highlighting
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highlightContent = new HighlightBuilder.Field(PIPELINE_NAME);
highlightContent.fragmentSize(100); // 设置片段大小为最大值,即返回全部片段
highlightContent.highlighterType("unified");
highlightBuilder.field(highlightContent);
highlightBuilder.preTags("<em>");
highlightBuilder.postTags("</em>");
highlightBuilder.numOfFragments(100);

//highlighting会自动返回匹配到的文本,所以就不需要再次返回文本了
String[] includeFields = new String[]{"name", "id"};
String[] excludeFields = new String[]{PIPELINE_NAME};
srb.fetchSource(includeFields, excludeFields);

//把刚才设置的值导入进去
srb.highlighter(highlightBuilder);
searchRequest.source(srb);
SearchResponse res;
try {
res = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (ConnectException e) {
log.error("连接es失败!", e.getCause());
res = null;
}

if (res == null || res.getHits() == null) {
return Maps.newHashMap();
}
//获取hits,这样就可以获取查询到的记录了
SearchHits hits = res.getHits();

//hits是一个迭代器,所以需要迭代返回每一个hits
for (SearchHit hit : hits) {
//获取返回的字段
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//这个就会把匹配到的文本返回,而且只返回匹配到的部分文本
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField highlightField = highlightFields.get(PIPELINE_NAME);

List<PageVO> pageVOList = new ArrayList<>();
for (Text fragment : highlightField.getFragments()) {
PageVO pageVO = new PageVO();
pageVO.setContent(fragment.string());
pageVOList.add(pageVO);
}

if (sourceAsMap.containsKey("id")) {
String id = (String) sourceAsMap.get("id");
if (Objects.nonNull(id)) {
result.put(id, pageVOList);
}
}
}
return result;
}

/**
* @Author luojiarui
* @Description 根据文档的id删除文档
Expand Down
Loading

0 comments on commit e38f014

Please sign in to comment.