/**
* 文档搜索接口
*
* @param page
* @param pageNum
* @param keywords
* @param tag
* @return
*/
@GetMapping("")
public Result<List> listPage(
@RequestParam(required = false, defaultValue = Constants.DEFAULT_MIN_PAGE_STR) int page,
@RequestParam(required = false, defaultValue = Constants.DEFAULT_MIN_PAGE_NUM_STR) int pageNum,
String keywords, String tag, String sort, String type) {
return documentService.listPage(keywords, tag, sort, type, page, pageNum);
}
/**
* 文档详情接口
*
* @param docId
* @return
*/
@GetMapping("{docId}")
public Result<Document> getByDocId(@PathVariable String docId, String source, String keyWords, String type) {
return Result.newSuccess().withData(documentService.getByDocId(docId, source, keyWords, type));
}
@Override
public Result<List> listPage(String keywords, String tag, String sort, String type, int page, int pageNum) {
if (StringUtils.isNotBlank(keywords)) {
searchMapper.insert(keywords);
}
if (page < Constants.DEFAULT_MIN_PAGE) {
page = Constants.DEFAULT_MIN_PAGE;
}
if (pageNum < Constants.DEFAULT_MIN_PAGE_NUM) {
pageNum = Constants.DEFAULT_MIN_PAGE_NUM;
} else if (pageNum > Constants.DEFAULT_MAX_PAGE_NUM) {
pageNum = Constants.DEFAULT_MAX_PAGE_NUM;
}
if (StringUtils.isBlank(keywords)) {
keywords = "*";
}
String q;
//模糊查询与精确查询
if (StringUtils.isNotBlank(type) && "accurate".equals(type)) {
q = String.format("(title:\"%s\" OR content:\"%s\")", keywords, keywords);
} else {
q = String.format("(title:%s OR content:%s)", keywords, keywords);
}
if (StringUtils.isNotBlank(tag)) {
q += String.format(" AND (tag1:%s OR tag2:%s OR tag3:%s OR tag4:%s)", tag, tag, tag, tag);
}
SolrQuery query = new SolrQuery(q);
//添加高亮
if (!keywords.equals("*")) {
query.setHighlight(true);
query.addHighlightField("title");// 高亮字段
query.setHighlightSimplePre("<span class='keywords'>");//标记,高亮关键字前缀
query.setHighlightSimplePost("</span>");
query.setHighlightFragsize(100000);
}
query.setFields("docId", "title", "docNum", "id");
query.setStart((page - 1) * pageNum);
query.setRows(pageNum);
if ("level".equalsIgnoreCase(sort)) {
query.setSort("levelOrder", SolrQuery.ORDER.desc);
} else if (StringUtils.isNotBlank(tag)) {
query.setSort("publishDateMillis", SolrQuery.ORDER.desc);
}
try {
QueryResponse queryResponse = solrClient.query(query);
SolrDocumentList list = queryResponse.getResults();
//如果keywords为空直接返回
if (keywords.equals("*") || queryResponse.getHighlighting().isEmpty()) {
return Result.newSuccess().withTotal((int) list.getNumFound()).withData(Document.fromSolr(list));
}
List<Document> documentList = new ArrayList<>();
documentList = getDocument(queryResponse, list, documentList);
return Result.newSuccess().withTotal((int) list.getNumFound()).withData(documentList);
} catch (Exception e) {
logger.error("keywords[{}], tag[{}], page[{}], pageNum[{}], type[{}]", keywords, tag, page, pageNum, type);
logger.error(e.getLocalizedMessage(), e);
}
return Result.newError();
}
/**
* @Author: mahongfei
* @description: 遍历高亮集合和关键字查询的集合,根据id将高亮集合的title、content放到条件查询的document
*/
public List<Document> getDocument(QueryResponse queryResponse, SolrDocumentList list, List<Document> documentList) {
Map<String, Map<String, List<String>>> results = queryResponse.getHighlighting();
Set<String> keys = results.keySet();
Map<String, List<String>> innermap;
Set<String> innerkeys;
List<String> list1;
for (String key : keys) {
for (SolrDocument solrDocument : list) {
Document document = Document.fromSolr(solrDocument);
if (solrDocument.get("id").equals(key)) {
innermap = results.get(key);
innerkeys = innermap.keySet();
for (String innerKey : innerkeys) {
list1 = innermap.get(innerKey);
for (String param : list1) {
if ("title".equals(innerKey)) {
document.setTitle(param);
} else if ("content".equals(innerKey)) {
document.setContent(param);
}
}
}
documentList.add(document);
}
}
}
return documentList;
}
@Override
public Document getByDocId(String docId, String source, String keyWords, String type) {
SolrQuery query = new SolrQuery();
//添加高亮
String q;
if (StringUtils.isNotBlank(keyWords) && StringUtils.isNotBlank(type)) {
//模糊查询与精确查询
if (StringUtils.isNotBlank(type) && "accurate".equals(type)) {
q = String.format("((docId:%s AND title:\"%s\") OR (docId:%s AND content:\"%s\"))", docId, keyWords, docId, keyWords);
} else {
q = String.format("((docId:%s AND title:%s) OR (docId:%s AND content:%s))", docId, keyWords, docId, keyWords);
}
query.setHighlight(true);
query.addHighlightField("title");// 高亮字段
query.addHighlightField("content");
query.setHighlightSimplePre("<span class='keywords'>");//标记,高亮关键字前缀
query.setHighlightSimplePost("</span>");
query.setHighlightFragsize(100000);
} else {
q = "docId:" + docId;
}
query.setQuery(q);
try {
QueryResponse queryResponse = solrClient.query(query);
SolrDocumentList list = queryResponse.getResults();
if (!list.isEmpty()) {
readMapper.insert(docId, source, Document.fromSolr(list.get(0)).getTitle());
//如果keywords为空直接返回
if (StringUtils.isBlank(keyWords) || StringUtils.isBlank(type)) {
return Document.fromSolr(list.get(0));
}
List<Document> documentList = new ArrayList<>();
documentList = getDocument(queryResponse, list, documentList);
for (Document document : documentList) {
return document;
}
}
} catch (Exception e) {
logger.error("{}: docId[{}], keyWords[{}], type[{}]", e.getLocalizedMessage(), docId, keyWords, type, e);
}
return null;
}