增加:综合浏览文件全文检索页面增自定义排序功能

master
Guo XIn 1 year ago
parent fe4a19682e
commit 5f1e778fd6
  1. 5
      shandan-browser/src/main/java/com/keyware/shandan/browser/controller/SearchController.java
  2. 26
      shandan-browser/src/main/java/com/keyware/shandan/browser/entity/SearchConditionVo.java
  3. 34
      shandan-browser/src/main/java/com/keyware/shandan/browser/entity/SearchResultSort.java
  4. 37
      shandan-browser/src/main/java/com/keyware/shandan/browser/service/FileSearchService.java
  5. 3
      shandan-browser/src/main/java/com/keyware/shandan/browser/service/MetadataSearchService.java
  6. 33
      shandan-browser/src/main/resources/static/js/browser.js

@ -10,6 +10,7 @@ import com.keyware.shandan.bianmu.entity.MetadataBasicVo;
import com.keyware.shandan.bianmu.service.MetadataService; import com.keyware.shandan.bianmu.service.MetadataService;
import com.keyware.shandan.browser.entity.PageVo; import com.keyware.shandan.browser.entity.PageVo;
import com.keyware.shandan.browser.entity.SearchConditionVo; import com.keyware.shandan.browser.entity.SearchConditionVo;
import com.keyware.shandan.browser.entity.SearchResultSort;
import com.keyware.shandan.browser.service.*; import com.keyware.shandan.browser.service.*;
import com.keyware.shandan.common.entity.Result; import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.util.FileDownload; import com.keyware.shandan.common.util.FileDownload;
@ -138,10 +139,10 @@ public class SearchController {
*/ */
@AppLog(operate = "文件全文检索查询") @AppLog(operate = "文件全文检索查询")
@GetMapping("/full/file") @GetMapping("/full/file")
public Result<Object> searchFile(PageVo page, String search, String metaId) { public Result<Object> searchFile(PageVo page, String search, String metaId, SearchResultSort sort) {
try { try {
return Result.of(fileSearchService.searchFile(page, search, metaId)); return Result.of(fileSearchService.searchFile(page, search, metaId, sort));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return Result.of(null, false, "Elasticsearch 请求异常"); return Result.of(null, false, "Elasticsearch 请求异常");

@ -10,7 +10,6 @@ import org.elasticsearch.search.sort.SortOrder;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -35,7 +34,7 @@ public class SearchConditionVo extends PageVo implements Serializable {
//搜索条件集合 //搜索条件集合
private List<Item> conditions = new ArrayList<>(); private List<Item> conditions = new ArrayList<>();
// 排序 // 排序
private Sort sort = new Sort(); private SearchResultSort sort = new SearchResultSort();
//排序字段 //排序字段
@Deprecated @Deprecated
private String sortFiled; private String sortFiled;
@ -113,27 +112,4 @@ public class SearchConditionVo extends PageVo implements Serializable {
return " " + logicJoin + " \"" + table + "\".\"" + fieldName + "\" " + logicJudgement + " " + fieldValue; return " " + logicJoin + " \"" + table + "\".\"" + fieldName + "\" " + logicJudgement + " " + fieldValue;
} }
} }
/**
* 排序
*/
@Getter
@Setter
public static class Sort {
//排序字段
private String field;
//排序方式
private String sort;
/**
* @return order by sql语句
*/
public String getOrderBy() {
return isEmpty() ? Strings.EMPTY : " order by " + getField() + " " + getSort();
}
public boolean isEmpty() {
return StringUtils.isBlankAny(this.field, this.sort);
}
}
} }

@ -0,0 +1,34 @@
package com.keyware.shandan.browser.entity;
import com.keyware.shandan.common.util.StringUtils;
import joptsimple.internal.Strings;
import lombok.Getter;
import lombok.Setter;
/**
* 排序
*
* @author GuoXin
* @date 2023/8/29
*/
@Getter
@Setter
public class SearchResultSort {
//排序字段
private String field;
//排序方式
private String sort;
/**
* @return order by sql语句
*/
public String getOrderBy() {
return isEmpty() ? Strings.EMPTY : " order by " + getField() + " " + getSort();
}
public boolean isEmpty() {
return StringUtils.isBlankAny(this.field, this.sort);
}
}

@ -1,12 +1,16 @@
package com.keyware.shandan.browser.service; package com.keyware.shandan.browser.service;
import cn.hutool.core.util.ReflectUtil;
import com.keyware.shandan.bianmu.entity.DirectoryVo; import com.keyware.shandan.bianmu.entity.DirectoryVo;
import com.keyware.shandan.bianmu.enums.ReviewStatus; import com.keyware.shandan.bianmu.enums.ReviewStatus;
import com.keyware.shandan.bianmu.service.DirectoryService; import com.keyware.shandan.bianmu.service.DirectoryService;
import com.keyware.shandan.browser.entity.PageVo; import com.keyware.shandan.browser.entity.PageVo;
import com.keyware.shandan.browser.entity.SearchResultSort;
import com.keyware.shandan.common.constants.DirConstant; import com.keyware.shandan.common.constants.DirConstant;
import com.keyware.shandan.common.util.StreamUtil; import com.keyware.shandan.common.util.StreamUtil;
import com.keyware.shandan.common.util.StringUtils; import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.system.entity.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
@ -17,10 +21,15 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -30,6 +39,7 @@ import java.util.List;
* @author Administrator * @author Administrator
* @since 2021/12/21 * @since 2021/12/21
*/ */
@Slf4j
@Service @Service
public class FileSearchService { public class FileSearchService {
@ -38,7 +48,7 @@ public class FileSearchService {
@Autowired @Autowired
private DirectoryService directoryService; private DirectoryService directoryService;
public PageVo searchFile(PageVo page, String text, String metaId) throws IOException { public PageVo searchFile(PageVo page, String text, String metaId, SearchResultSort sort) throws IOException {
SearchRequest request = Requests.searchRequest("shandan"); SearchRequest request = Requests.searchRequest("shandan");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(page.getPage() * page.getSize() - page.getSize()).size(page.getSize()); searchSourceBuilder.from(page.getPage() * page.getSize() - page.getSize()).size(page.getSize());
@ -75,8 +85,33 @@ public class FileSearchService {
highlightBuilder.preTags("<label style=\"color:red\">").postTags("</label>").field("*"); highlightBuilder.preTags("<label style=\"color:red\">").postTags("</label>").field("*");
highlightBuilder.forceSource(true); highlightBuilder.forceSource(true);
searchSourceBuilder.query(builder).highlighter(highlightBuilder); searchSourceBuilder.query(builder).highlighter(highlightBuilder);
// 设置排序
if (!sort.isEmpty()) {
String sortField = sort.getField();
// 使用反射获取文件实体类中字段上的注解设置的字段类型
Field field = ReflectUtil.getField(SysFile.class, sortField);
if (field != null) {
org.springframework.data.elasticsearch.annotations.Field fieldAnno = field.getAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
if(fieldAnno.type() != FieldType.Text
&& fieldAnno.type() != FieldType.Object
&& fieldAnno.type() != FieldType.Auto
&& fieldAnno.type() != FieldType.Nested
&& fieldAnno.type() != FieldType.Ip
&& fieldAnno.type() != FieldType.Attachment){
if(fieldAnno.type() == FieldType.Keyword){
sortField = sortField.concat(".keyword");
}
searchSourceBuilder.sort(sortField, SortOrder.fromString(sort.getSort()));
}else {
log.error("不支持的排序类型:{}:{}", sortField, fieldAnno.type());
}
}
}
request.source(searchSourceBuilder); request.source(searchSourceBuilder);
// 设置排序
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT); SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
return PageVo.ofSearchHits(response.getHits(), page); return PageVo.ofSearchHits(response.getHits(), page);
} }

@ -12,6 +12,7 @@ import com.keyware.shandan.bianmu.mapper.DirectoryResourceMapper;
import com.keyware.shandan.bianmu.service.DirectoryService; import com.keyware.shandan.bianmu.service.DirectoryService;
import com.keyware.shandan.browser.entity.PageVo; import com.keyware.shandan.browser.entity.PageVo;
import com.keyware.shandan.browser.entity.SearchConditionVo; import com.keyware.shandan.browser.entity.SearchConditionVo;
import com.keyware.shandan.browser.entity.SearchResultSort;
import com.keyware.shandan.common.util.StreamUtil; import com.keyware.shandan.common.util.StreamUtil;
import com.keyware.shandan.common.util.StringUtils; import com.keyware.shandan.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -101,7 +102,7 @@ public class MetadataSearchService {
}); });
// 排序设置 // 排序设置
SearchConditionVo.Sort sort = condition.getSort(); SearchResultSort sort = condition.getSort();
if(sort != null && sort.getSort() != null ){ if(sort != null && sort.getSort() != null ){
// 通过反射获取资源类字段注解中的数据库表相关的列名 // 通过反射获取资源类字段注解中的数据库表相关的列名
Field field = ReflectUtil.getField(DirectoryResource.class, sort.getField().trim()); Field field = ReflectUtil.getField(DirectoryResource.class, sort.getField().trim());

@ -152,7 +152,8 @@ layui.use(['layer', 'listPage', 'globalTree', 'gtable', 'form', 'element', 'drop
field: 'dataSource', field: 'dataSource',
title: '数据来源', title: '数据来源',
width: 160, width: 160,
templet: (data) => DICT.getText("data_source", data.dataSource) || data.dataSource || '',sort: true templet: (data) => DICT.getText("data_source", data.dataSource) || data.dataSource || '',
sort: true
}, },
{field: 'taskTime', title: '任务时间', width: 180, align: 'center', sort: true}, {field: 'taskTime', title: '任务时间', width: 180, align: 'center', sort: true},
{field: 'modifyTime', title: '注册时间', width: 160, align: 'center', hide: true}, {field: 'modifyTime', title: '注册时间', width: 160, align: 'center', hide: true},
@ -229,32 +230,39 @@ layui.use(['layer', 'listPage', 'globalTree', 'gtable', 'form', 'element', 'drop
url: `${ctx}/search/full/file?metaId=${id}`, url: `${ctx}/search/full/file?metaId=${id}`,
height: 'full-110', height: 'full-110',
request: {pageName: 'page', limitName: 'size'}, request: {pageName: 'page', limitName: 'size'},
autoSort: false,
limit: 30, limit: 30,
method: 'get', method: 'get',
cols: [[ cols: [[
{field: 'id', title: 'ID', hide: true}, {field: 'id', title: 'ID', hide: true},
{field: 'fileName', title: '文件名称', minWidth: 300, fixed: 'left'}, {field: 'fileName', title: '文件名称', minWidth: 300, fixed: 'left'},
{ {
field: 'source',
title: '文件来源', title: '文件来源',
minWidth: 160, minWidth: 160,
sort: true,
templet: data => DICT.getText('data_source', data.source) || data.source || '' templet: data => DICT.getText('data_source', data.source) || data.source || ''
}, },
{field: 'taskCode', title: '任务代号', minWidth: 160}, {field: 'taskCode', title: '任务代号', sort: true, minWidth: 160},
{ {
field: 'taskNature',
title: '任务性质', title: '任务性质',
minWidth: 160, minWidth: 160,
sort: true,
templet: data => DICT.getText('task_nature', data.taskNature) || data.taskNature || '' templet: data => DICT.getText('task_nature', data.taskNature) || data.taskNature || ''
}, },
{field: 'troopCode', title: '部队代号', minWidth: 160}, {field: 'troopCode', title: '部队代号', sort: true, minWidth: 160},
{field: 'missileNumber', title: '导弹编号', minWidth: 160}, {field: 'missileNumber', title: '导弹编号', sort: true, minWidth: 160},
{field: 'equipmentModel', title: '装备型号', minWidth: 160}, {field: 'equipmentModel', title: '装备型号', sort: true, minWidth: 160},
{ {
field: 'targetNumber',
title: '目标/靶标类型', title: '目标/靶标类型',
minWidth: 160, minWidth: 160,
sort: true,
templet: data => DICT.getText('target_type', data.targetNumber) || data.targetNumber || '' templet: data => DICT.getText('target_type', data.targetNumber) || data.targetNumber || ''
}, },
{field: 'entryStaff', title: '录入人员', minWidth: 160}, {field: 'entryStaff', title: '录入人员', sort: true, minWidth: 160},
{field: 'inputDate', title: '收文时间', width: 160, align: 'center'}, {field: 'inputDate', title: '收文时间', sort: true, width: 160, align: 'center'},
{field: 'remark', title: '文件描述', width: 300}, {field: 'remark', title: '文件描述', width: 300},
{field: 'text', title: '文件内容', width: 300}, {field: 'text', title: '文件内容', width: 300},
{fixed: 'right', title: '操作', toolbar: '#fileRowToolBar', width: 100, align: 'center'} {fixed: 'right', title: '操作', toolbar: '#fileRowToolBar', width: 100, align: 'center'}
@ -262,6 +270,17 @@ layui.use(['layer', 'listPage', 'globalTree', 'gtable', 'form', 'element', 'drop
}, },
}); });
dirFileTable.gtable.on('sort', function (data) {
const {field, type} = data;
let sort = type;
if (!sort || sort === 'null') {
sort = undefined;
}
dirFileTable.table.where.field = field.trim();
dirFileTable.table.where.sort = sort;
dirFileTable.reloadTable({table: dirFileTable.table})
})
// 查看按钮监听 // 查看按钮监听
dirFileTable.addTableRowEvent('details-file', function (obj) { dirFileTable.addTableRowEvent('details-file', function (obj) {
openMaxLayerWithURL(`${ctx}/sys/file/view?fileId=${obj.id}`) openMaxLayerWithURL(`${ctx}/sys/file/view?fileId=${obj.id}`)