0b14e7755cf3a0d84124899f8a4ea55bafe003a2..c7584948cb01407c9db17dc082acc91bed11a97e
2023-05-31 354798ggg
文档检索调整
c75849 对比 | 目录
2023-05-31 354798ggg
文档检索调整
9209f0 对比 | 目录
已添加4个文件
已修改1个文件
327 ■■■■ 文件已修改
src/main/java/com/product/lucene/config/CmnConst.java 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/lucene/config/SystemCode.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/lucene/controller/SearchTermController.java 88 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/lucene/service/LuceneService.java 99 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/lucene/service/SearchTermService.java 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/lucene/config/CmnConst.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,35 @@
package com.product.lucene.config;
public class CmnConst {
    public static final String PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT = "product_sys_document_search_content";
    public static final String PRODUCT_SYS_DOCUMENT_SEARCH = "product_sys_document_search";
    public static final String PRODUCT_SYS_ATTACHMENTS="product_sys_attachments";
    public static final String TABLE_NAME="table_name";
    public static final String ATTACHMENT_UUID="attachment_uuid";
    public static final String FILE_NAME="file_name";
    public static final String FILE_CONTENT="file_content";
    public static final String ORG_FIELDS="org_fileds";
    public static final String USER_FIELDS="user_fileds";
    public static final String TIME_FIELD="time_field";
    public static final String TITLE_FIELD="title_field";
    public static final String ATTACHMENT_DATA_FIELD="attachment_data_field";
    public static final String ATTACHMENT_DATA_TABLE="attachment_data_table";
    public static final String SEARCH_KEY="search_key";
    public static final String SEARCH_KEYWORD="search_keyword";
    public static final String SEARCH_HISTORY="search_history";
    public static final String SEARCH_CONTENT="search_content";
    public static final String CONTENT_TYPE="content_type";
    public static final String TITLE="title";
    public static final String IS_PERSSION="is_perssion";
    public static final String SERVICE_UUID="service_uuid";
    public static final String DATE_TIME="date_time";
    public static final String TOTALCOUNT="totalCount";
    public static final String DATA="data";
    public static final String HIGHLIGHT_BEFORE="<span style='color:red'>";
    public static final String HIGHLIGHT_AFTER="</span>";
}
src/main/java/com/product/lucene/config/SystemCode.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,33 @@
package com.product.lucene.config;
import com.product.common.enums.IEnum;
import com.product.common.enums.ModuleEnum;
public enum SystemCode implements IEnum {
    ADD_SEARCH_CONTENT_EXIST("新增常用词条已存在", ModuleEnum.LUCENE.getValue() + "001"),
    DELETE_SEARCH_CENTENT_FAIL("删除搜索词条失败", ModuleEnum.LUCENE.getValue() + "002"),
    ADD_SEARCH_CENTENT_FAIL("删除搜索词条失败", ModuleEnum.LUCENE.getValue() + "003"),
    LIST_SEARCH_CENTENT_FAIL("删除搜索词条失败", ModuleEnum.LUCENE.getValue() + "004"),
    ;
    private String text;
    private String value;
    private SystemCode(String text, String value) {
        this.text = text;
        this.value = value;
    }
    @Override
    public String getText() {
        return text;
    }
    @Override
    public String getValue() {
        return value;
    }
}
src/main/java/com/product/lucene/controller/SearchTermController.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,88 @@
package com.product.lucene.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.product.core.controller.support.AbstractBaseController;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.lucene.config.CmnConst;
import com.product.lucene.config.SystemCode;
import com.product.lucene.service.SearchTermService;
import com.product.module.sys.version.ApiVersion;
import com.product.util.BaseUtil;
import com.product.util.ResultInfo;
@RequestMapping("/api/search-term")
@RestController
public class SearchTermController extends AbstractBaseController{
    @Autowired
    SearchTermService searchTermService;
    /**
     *     èŽ·å–æ£€ç´¢è¯æ¡ï¼ˆå¸¸ç”¨è¯æ¡å’ŒåŽ†å²è®°å½•ï¼‰
     * @param request
     */
    @RequestMapping("/list/{version}")
    @ApiVersion(1)
    public String listSearchTerm(HttpServletRequest request) {
        try {
            searchTermService.listSearchTerm();
            return ResultInfo.success(searchTermService.listSearchTerm());
        } catch (BaseException e) {
            e.printStackTrace();
            return this.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(SystemCode.LIST_SEARCH_CENTENT_FAIL.getValue(), SystemCode.LIST_SEARCH_CENTENT_FAIL.getText());
        }
    }
    /**
     *     æ–°å¢žæ£€ç´¢è¯æ¡ï¼ˆå¸¸ç”¨è¯æ¡ï¼‰
     * @param request
     */
    @RequestMapping("/add/{version}")
    @ApiVersion(1)
    public String addSearchTerm(HttpServletRequest request) {
        try {
            FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT);
            return OK_Add(searchTermService.addSearchTerm(fse.getString(CmnConst.SEARCH_CONTENT), fse.getString(CmnConst.CONTENT_TYPE)));
        } catch (BaseException e) {
            e.printStackTrace();
            return this.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(SystemCode.ADD_SEARCH_CENTENT_FAIL.getValue(), SystemCode.ADD_SEARCH_CENTENT_FAIL.getText());
        }
    }
    /**
     *     åˆ é™¤æ£€ç´¢è¯æ¡ï¼ˆå¸¸ç”¨è¯æ¡å’ŒåŽ†å²è®°å½•ï¼‰
     * @param request
     */
    @RequestMapping("/delete/{version}")
    @ApiVersion(1)
    public String deleteSearchTerm(HttpServletRequest request) {
        try {
            FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT);
            searchTermService.deleteSearchTerm(fse.getUUID());
            boolean succ = searchTermService.deleteSearchTerm(fse.getUUID());
            if (succ) {
                return OK();
            }
            throw new BaseException(SystemCode.DELETE_SEARCH_CENTENT_FAIL.getValue(), SystemCode.DELETE_SEARCH_CENTENT_FAIL.getText());
        } catch (BaseException e) {
            e.printStackTrace();
            return this.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(SystemCode.DELETE_SEARCH_CENTENT_FAIL.getValue(), SystemCode.DELETE_SEARCH_CENTENT_FAIL.getText());
        }
    }
}
src/main/java/com/product/lucene/service/LuceneService.java
@@ -2,10 +2,11 @@
import com.alibaba.fastjson.JSONObject;
import com.product.common.lang.StringUtils;
import com.product.core.config.CoreConst;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.permission.PermissionService;
import com.product.core.websocket.config.CmnConst;
import com.product.lucene.config.CmnConst;
import com.product.lucene.util.FileUtils;
import com.product.util.BaseUtil;
@@ -54,20 +55,20 @@
     * @throws IOException
     */
    public void createdIndex(FieldSetEntity fse) {
        String function_uuid=fse.getString("function_uuid");    //获取功能
        String function_uuid=fse.getString(CoreConst.FUNCTION_UUID);    //获取功能
        File file=(File)fse.getObject("file");    //获取附件
        
        //获取全文检索配置
        FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false);
        FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH, "function_uuid=?", new Object[] {function_uuid}, false);
        if (fseConfig!=null) {
            String table_uuid=fseConfig.getString("table_name");
            String table_uuid=fseConfig.getString(CmnConst.TABLE_NAME);
            
            //获取缓存表配置
            FieldSetEntity fseTable=baseDao.getFieldSetEntity("product_sys_datamodel_table", table_uuid, false);
            FieldSetEntity fseTable=baseDao.getFieldSetEntity(CoreConst.PRODUCT_SYS_DATAMODEL_TABLE, table_uuid, false);
            if (fseTable!=null) {
                
                //获取附件uuids
                String attachments=fse.getString("attachment_uuid");
                String attachments=fse.getString(CmnConst.ATTACHMENT_UUID);
                if (!StringUtils.isEmpty(attachments)) {
                    
                    //遍历获取附件信息
@@ -76,16 +77,16 @@
                    for (int i = 0; i < attachment.length; i++) {
                        
                        //获取附件信息
                        FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", attachment[i], false);
                        FieldSetEntity fseAttachment=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, attachment[i], false);
                        if (fseAttachment!=null) {
                            
                            if (file!=null) {
                                //生成索引信息
                                Document doc=new Document();
                                doc.add(new StringField("uuid", attachment[i], Field.Store.YES));    //附件UUID
                                doc.add(new TextField("function_uuid", function_uuid, Field.Store.YES));    //功能UUID
                                doc.add(new TextField("file_name", fseAttachment.getString("file_name"), Field.Store.YES));    //文件名
                                doc.add(new TextField("file_content", FileUtils.FileToString(file), Field.Store.YES));    //文件内内容
                                doc.add(new StringField(CoreConst.UUID, attachment[i], Field.Store.YES));    //附件UUID
                                doc.add(new TextField(CoreConst.FUNCTION_UUID, function_uuid, Field.Store.YES));    //功能UUID
                                doc.add(new TextField(CmnConst.FILE_NAME, fseAttachment.getString(CmnConst.FILE_NAME), Field.Store.YES));    //文件名
                                doc.add(new TextField(CmnConst.FILE_CONTENT, FileUtils.FileToString(file), Field.Store.YES));    //文件内内容
                                docs.add(doc);
                            }
                        }
@@ -120,49 +121,49 @@
        JSONObject jsonReturn=new JSONObject();
        
        // æ¨¡ç³ŠåŒ¹é…,匹配词
        String search_key=fse.getString("search_key");
        String search_key=fse.getString(CmnConst.SEARCH_KEY);
        if (!BaseUtil.strIsNull(search_key)) {
            //查询解析器
            QueryParser parser = new MultiFieldQueryParser(new String [] {"file_name","file_content"}, new SmartChineseAnalyzer());
            QueryParser parser = new MultiFieldQueryParser(new String [] {CmnConst.FILE_NAME,CmnConst.FILE_CONTENT}, new SmartChineseAnalyzer());
            Query query = parser.parse(search_key);
            
            //高亮格式
            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(CmnConst.HIGHLIGHT_BEFORE, CmnConst.HIGHLIGHT_AFTER);
            //高亮匹配器
            Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
            Fragmenter fragmenter = new SimpleFragmenter(100);
            highlighter.setTextFragmenter(fragmenter);
            
            //分页查询文档
            TopDocs topDocs = searchByPage(fse.getInteger("cpage"), fse.getInteger("pagesize"), indexSearcher, query);
            TopDocs topDocs = searchByPage(fse.getInteger(CoreConst.CPAGE), fse.getInteger(CoreConst.PAGESIZE), indexSearcher, query);
            
            ScoreDoc[] hits = topDocs.scoreDocs;
            for (int i = 0; i < hits.length; i++) {
                Document doc = indexSearcher.doc(hits[i].doc);
                JSONObject json=new JSONObject();
                if (!StringUtils.isEmpty(doc.get("function_uuid"))) {
                    getConfig(json, doc.get("function_uuid"), doc.get("uuid"));
                if (!StringUtils.isEmpty(doc.get(CoreConst.FUNCTION_UUID))) {
                    getConfig(json, doc.get(CoreConst.FUNCTION_UUID), doc.get(CoreConst.UUID));
                }
                json.put("function_uuid", doc.get("function_uuid"));
                json.put(CoreConst.FUNCTION_UUID, doc.get(CoreConst.FUNCTION_UUID));
                
                // å†…容增加高亮显示
                String file_name=highlighter.getBestFragment(new SmartChineseAnalyzer(), "file_name",doc.get("file_name"));
                String file_name=highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_NAME,doc.get(CmnConst.FILE_NAME));
                if (StringUtils.isEmpty(file_name)) {
                    file_name=doc.get("file_name");
                    file_name=doc.get(CmnConst.FILE_NAME);
                }
                String file_content=highlighter.getBestFragment(new SmartChineseAnalyzer(), "file_content",doc.get("file_content"));
                String file_content=highlighter.getBestFragment(new SmartChineseAnalyzer(), CmnConst.FILE_CONTENT,doc.get(CmnConst.FILE_CONTENT));
                if (StringUtils.isEmpty(file_content)) {
                    file_content=doc.get("file_content");
                    file_content=doc.get(CmnConst.FILE_CONTENT);
                }
                json.put("file_name", file_name);
                json.put("file_content", file_content);
                json.put(CmnConst.FILE_NAME, file_name);
                json.put(CmnConst.FILE_CONTENT, file_content);
                jsonTotal.add(json);
            }
            
            jsonReturn.put("totalCount", topDocs.totalHits.value);
            jsonReturn.put(CmnConst.TOTALCOUNT, topDocs.totalHits.value);
        }
        jsonReturn.put("data", jsonTotal);
        jsonReturn.put(CmnConst.DATA, jsonTotal);
        return jsonReturn;
    }
    
@@ -174,26 +175,26 @@
     */
    public void getConfig(JSONObject json, String function_uuid, String uuid) {
        
        FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter("product_sys_document_search", "function_uuid=?", new Object[] {function_uuid}, false);
        FieldSetEntity fseConfig=baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH, "function_uuid=?", new Object[] {function_uuid}, false);
        if (fseConfig!=null) {
            
            FieldSetEntity fseFunction=baseDao.getFieldSetEntity("product_sys_functions", function_uuid, false);
            FieldSetEntity fseFunction=baseDao.getFieldSetEntity(CoreConst.PRODUCT_SYS_FUNCTIONS, function_uuid, false);
            
            //获取权限过滤字段
            String org_fields=fseConfig.getString("org_fileds");
            String user_fields=fseConfig.getString("user_fileds");
            String org_fields=fseConfig.getString(CmnConst.ORG_FIELDS);
            String user_fields=fseConfig.getString(CmnConst.USER_FIELDS);
            
            //获取附件信息
            FieldSetEntity fseAttachment=baseDao.getFieldSetEntity("product_sys_attachments", uuid, false);
            FieldSetEntity fseAttachment=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false);
            baseDao.loadPromptData(fseAttachment);
            if (fseAttachment!=null) {
                StringBuilder filter=new StringBuilder();
                filter.append(fseAttachment.getString("attachment_data_field"));
                filter.append(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD));
                filter.append(" =? ");
                if (StringUtils.isEmpty(org_fields)) {
                    if (!StringUtils.isEmpty(user_fields)) {
                        filter.append(" AND ");
                        filter.append(permissionService.getDataFilter(fseAttachment.getString("attachment_data_table"), user_fields));
                        filter.append(permissionService.getDataFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), user_fields));
                    }
                }else {
                    if (StringUtils.isEmpty(user_fields)) {
@@ -201,35 +202,35 @@
                        filter.append(permissionService.getDataFilter(org_fields));
                    }else {
                        filter.append(" AND ");
                        filter.append(permissionService.getDataFilter(fseAttachment.getString("attachment_data_table"), user_fields, org_fields));
                        filter.append(permissionService.getDataFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), user_fields, org_fields));
                    }
                }
                
                
                //获取原数据
                FieldSetEntity fseService=baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), filter.toString(), new Object[] {uuid}, false);
                FieldSetEntity fseService=baseDao.getFieldSetEntityByFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), filter.toString(), new Object[] {uuid}, false);
                if (fseService==null) {//权限过滤无原数据
                    FieldSetEntity fseService2=baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), fseAttachment.getString("attachment_data_field")+"=?", new Object[] {uuid}, false);
                    FieldSetEntity fseService2=baseDao.getFieldSetEntityByFilter(fseAttachment.getString(CmnConst.ATTACHMENT_DATA_TABLE), fseAttachment.getString(CmnConst.ATTACHMENT_DATA_FIELD)+"=?", new Object[] {uuid}, false);
                    if (fseService2==null) {
//                        json.put("date_time", fseService2.getString(fseConfig.getString("time_field")));
                        json.put("title", "无相关业务数据");
                        json.put("function_name", fseFunction.getString("function_name"));
                        json.put("is_perssion", 1);
                        json.put(CmnConst.TITLE, "无相关业务数据");
                        json.put(CoreConst.FUNCTION_NAME, fseFunction.getString(CoreConst.FUNCTION_NAME));
                        json.put(CmnConst.IS_PERSSION, 1);
                    }else {
                        json.put("service_uuid", fseService2.getString("uuid"));
                        json.put("date_time", fseService2.getString(fseConfig.getString("time_field")));
                        json.put("title", fseService2.getString(fseConfig.getString("title_field")));
                        json.put("function_name", fseFunction.getString("function_name"));
                        json.put("is_perssion", 0);
                        json.put(CmnConst.SERVICE_UUID, fseService2.getString(CoreConst.UUID));
                        json.put(CmnConst.DATE_TIME, fseService2.getString(fseConfig.getString(CmnConst.TIME_FIELD)));
                        json.put(CmnConst.TITLE, fseService2.getString(fseConfig.getString(CmnConst.TITLE_FIELD)));
                        json.put(CoreConst.FUNCTION_NAME, fseFunction.getString(CoreConst.FUNCTION_NAME));
                        json.put(CmnConst.IS_PERSSION, 0);
                    }
                    
                }else {
                    json.put("service_uuid", fseService.getString("uuid"));
                    json.put("date_time", fseService.getString(fseConfig.getString("time_field")));
                    json.put("title", fseService.getString(fseConfig.getString("title_field")));
                    json.put("function_name", fseFunction.getString("function_name"));
                    json.put(CmnConst.SERVICE_UUID, fseService.getString(CoreConst.UUID));
                    json.put(CmnConst.DATE_TIME, fseService.getString(fseConfig.getString(CmnConst.TIME_FIELD)));
                    json.put(CmnConst.TITLE, fseService.getString(fseConfig.getString(CmnConst.TITLE_FIELD)));
                    json.put(CoreConst.FUNCTION_NAME, fseFunction.getString(CoreConst.FUNCTION_NAME));
                }
                json.put("created_by", fseAttachment.getString(CmnConst.CREATED_BY));
                json.put(CoreConst.CREATED_BY, fseAttachment.getString(CoreConst.CREATED_BY));
            }
        }
    }
src/main/java/com/product/lucene/service/SearchTermService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,72 @@
package com.product.lucene.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.product.core.config.CoreConst;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.lucene.config.CmnConst;
import com.product.lucene.config.SystemCode;
import com.product.util.BaseUtil;
import cn.hutool.json.JSONObject;
@Component
public class SearchTermService extends AbstractBaseService{
    @Autowired
    BaseDao baseDao;
    /**
     *     èŽ·å–æ‰€æœ‰æœç´¢è¯æ¡
     * @return
     */
    public JSONObject listSearchTerm() {
        JSONObject jsonReturn = new JSONObject();
        jsonReturn.set(CmnConst.SEARCH_KEYWORD, BaseUtil.dataTableEntityToJson(baseDao.listTable(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT, "created_by=? AND content_type=0", new Object[] {SpringMVCContextHolder.getCurrentUserId()})));
        jsonReturn.set(CmnConst.SEARCH_HISTORY, BaseUtil.dataTableEntityToJson(baseDao.listTable(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT, "created_by=? AND content_type=1", new Object[] {SpringMVCContextHolder.getCurrentUserId()})));
        return jsonReturn;
    }
    /**
     *     æ–°å¢žå¸¸ç”¨æœç´¢è¯æ¡
     * @param searchContent
     * @param contentType
     * @return
     */
    public String addSearchTerm(String searchContent, String contentType) {
        //查询数据是否存在
        FieldSetEntity fseExist = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT,
                "created_by=? AND search_content=? AND content_type=?", new Object[] {SpringMVCContextHolder.getCurrentUserId(), searchContent, contentType}, false);
        if ("0".equals(contentType) && fseExist!=null) {
            throw new BaseException(SystemCode.ADD_SEARCH_CONTENT_EXIST.getValue(), SystemCode.ADD_SEARCH_CONTENT_EXIST.getText());
        }
        //新增
        FieldSetEntity fseAddInfo = new FieldSetEntity();
        fseAddInfo.setTableName(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT);
        if (fseExist!=null) {
            fseAddInfo.setValue(CoreConst.UUID, fseExist.getUUID());
        }
        fseAddInfo.setValue(CmnConst.SEARCH_CONTENT, searchContent);
        fseAddInfo.setValue(CmnConst.CONTENT_TYPE, contentType);
        BaseUtil.createCreatorAndCreationTime(fseAddInfo);
        baseDao.saveFieldSetEntity(fseAddInfo);
        return  fseAddInfo.getUUID();
    }
    /**
     *     åˆ é™¤å•个搜索词条
     * @param uuid
     * @return
     */
    public boolean deleteSearchTerm(String uuid) {
        return baseDao.delete(CmnConst.PRODUCT_SYS_DOCUMENT_SEARCH_CONTENT, "uuid=?", new Object[] {uuid});
    }
}