package com.walker.support.es.util; import com.walker.infrastructure.utils.StringUtils; import com.walker.support.es.Constants; import com.walker.support.es.SaveData; import com.walker.support.es.SaveDataFile; import org.apache.commons.codec.binary.Base64; import org.elasticsearch.common.text.Text; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.springframework.util.FileCopyUtils; import java.io.File; import java.util.HashMap; import java.util.Map; /** * 描述: * @author 时克英 * @date 2020年7月13日 下午2:19:24 */ public class FullTextUtils { /** * 返回高亮对象中的文本 * @param hl * @return */ public static final String getOneFieldHighlightText(Map hl){ StringBuilder sb = new StringBuilder(); if(hl != null){ Text[] texts = null; for(HighlightField hf : hl.values()){ texts = hf.getFragments(); if(texts != null && texts.length > 0){ // sb.append(texts[0].toString()).append("\n"); sb.append(texts[0].toString()); } } } return sb.toString(); } /** * 返回检索需要的json字符串 * @param queryText 查询字符串 * @param dataSecurity 权限类型:0无限制,1个人,2部门,3单位 * @param securityInfo 用户id | 部门id | 单位id * @return */ public static final String acquireSearchQueryJson(String queryText, int dataSecurity, String securityInfo){ StringBuilder text = new StringBuilder(); text.append("{\"bool\":{"); text.append("\"must\":["); // 如果存在空格,说明要多个条件同时匹配 String[] findKeys = queryText.split(" "); if(findKeys.length > 1){ // 通过设置多个multi_match + must来实现and效果 int i = 0; for(String k : findKeys){ if(i > 0){ text.append(","); } text.append("{\"multi_match\":{\"query\":\"").append(k).append("\","); // 这里设置查询字符串按照语意分词 text.append("\"analyzer\":\"ik_smart\","); text.append("\"fields\":[\"title\", \"text_db\", \"attachment.content\", \"xmname\"]}}"); i++; } } else { text.append("{\"multi_match\":{\"query\":\"").append(queryText).append("\","); text.append("\"fields\":[\"title\", \"text_db\", \"attachment.content\"]}}"); } if(dataSecurity == Constants.DATA_SECURITY_PERSON){ text.append(",{\"match\":{\"user_id\":\"").append(securityInfo).append("\"}}"); } else if(dataSecurity == Constants.DATA_SECURITY_DEPT){ text.append(",{\"match\":{\"dept_id\":\"").append(securityInfo).append("\"}}"); } else if(dataSecurity == Constants.DATA_SECURITY_ORG){ text.append(",{\"match\":{\"org_id\":\"").append(securityInfo).append("\"}}"); } else { // 无限制 } text.append("]}}"); return text.toString(); } /** * query 里面的内容,不带query本身 * @param _id * @return */ public static String acquireSearchOneById(String _id){ StringBuilder text = new StringBuilder(); text.append("{"); text.append("\"term\":{\"_id\":\"").append(_id).append("\"}"); text.append("}"); return text.toString(); } public static final Map translate(SaveData d){ Map map = new HashMap<>(); if(d instanceof SaveDataFile){ SaveDataFile sdf = (SaveDataFile)d; map.put("id", sdf.getId()); map.put("create_time", sdf.getCreateTime()); map.put("data", sdf.getData()); map.put("dept_id", sdf.getDeptId()); map.put("org_id", sdf.getOrgId()); map.put("path", sdf.getPath()); map.put("text_db", sdf.getTextDb()); map.put("title", sdf.getTitle()); map.put("update_time", sdf.getUpdateTime()); map.put("user_id", sdf.getUserId()); // map.put("text_html", d.getTextHtml()); map.put("xmid", sdf.getXmid()); map.put("xmname", sdf.getXmname()); map.put("user_name", sdf.getUserName()); } else { map.put("id", d.getId()); map.put("create_time", d.getCreateTime()); map.put("data", d.getData()); map.put("dept_id", d.getDeptId()); map.put("org_id", d.getOrgId()); map.put("path", d.getPath()); map.put("text_db", d.getTextDb()); map.put("title", d.getTitle()); map.put("update_time", d.getUpdateTime()); map.put("user_id", d.getUserId()); map.put("text_html", d.getTextHtml()); map.put("user_name", d.getUserName()); } return map; } /** * 创建简单的索引表,为测试返回分词用 * @param tokenType * @return */ public static final String createSimpleIndex(String tokenType, boolean attachment){ StringBuilder json2 = new StringBuilder(); json2.append("{\n"); json2.append("\"properties\":{\n"); // 项目id json2.append("\"id\":{\"type\":\"keyword\"},"); json2.append("\"title\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"},"); if(attachment){ json2.append("\"create_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"},"); // 附件信息 json2.append("\"attachment\":{"); json2.append("\"properties\":{\"content\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"}}"); json2.append("}"); } else { json2.append("\"create_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"}"); } json2.append("}"); json2.append("}"); return json2.toString(); } /** * 创建索引库 * @param type 表类型(暂未使用) * @return */ public static final String createIndexJson(String type, String tokenType){ // if(StringUtils.isEmpty(type)){ // type = "table"; // } StringBuilder json2 = new StringBuilder(); json2.append("{\n"); if(!StringUtils.isEmpty(type)){ json2.append("\"").append(type).append("\":{\n"); } json2.append("\"properties\":{\n"); System.out.println("+++++++++++++++++"); // 项目id json2.append("\"id\":{\"type\":\"keyword\"},"); // json.append("\"title\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\"},"); json2.append("\"title\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"},"); json2.append("\"path\":{\"type\":\"keyword\"},"); // 原始html内容 json2.append("\"text_html\":{\"type\":\"text\"},"); json2.append("\"text_db\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"},"); json2.append("\"create_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"},"); json2.append("\"update_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"},"); json2.append("\"user_name\":{\"type\":\"keyword\"},"); // 用户数据权限 json2.append("\"user_id\":{\"type\":\"keyword\"},"); json2.append("\"dept_id\":{\"type\":\"keyword\"},"); json2.append("\"org_id\":{\"type\":\"keyword\"},"); // 附件信息 /** */ json2.append("\"attachment\":{"); json2.append("\"properties\":{\"content\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"}}"); json2.append("}"); json2.append("}"); if(!StringUtils.isEmpty(type)){ json2.append("}"); } json2.append("}"); return json2.toString(); } public static final String createXmFileJson(String type, String tokenType){ StringBuilder json = new StringBuilder(); json.append("{\n"); if(!StringUtils.isEmpty(type)){ json.append("\"").append(type).append("\":{\n"); } json.append("\"properties\":{\n"); json.append("\"id\":{\"type\":\"keyword\"},"); // json.append("\"title\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\"},"); json.append("\"title\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"},"); // 附件绝对路径 json.append("\"path\":{\"type\":\"keyword\"},"); // 项目id、名称 json.append("\"xmid\":{\"type\":\"keyword\"},"); json.append("\"xmname\":{\"type\":\"keyword\"},"); // 原始html内容 // json.append("\"text_html\":{\"type\":\"keyword\"},"); // json.append("\"text_db\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"},"); json.append("\"create_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"},"); json.append("\"update_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"},"); json.append("\"user_name\":{\"type\":\"keyword\"},"); // 用户数据权限 json.append("\"user_id\":{\"type\":\"keyword\"},"); json.append("\"dept_id\":{\"type\":\"keyword\"},"); json.append("\"org_id\":{\"type\":\"keyword\"},"); // 附件信息 json.append("\"attachment\":{"); // json.append("\"properties\":{\"content\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\"}}"); json.append("\"properties\":{\"content\":{\"type\":\"text\",\"analyzer\":\"").append(tokenType).append("\"}}"); json.append("}"); json.append("}"); if(!StringUtils.isEmpty(type)){ json.append("}"); } json.append("}"); return json.toString(); } public static String getFileBase64(String fileContent){ return Base64.encodeBase64String(fileContent.getBytes()); } public static String getFileBase64Value(String path) throws Exception{ File file = null; try{ file = new File(path); byte[] fileBase64 = Base64.encodeBase64(FileCopyUtils.copyToByteArray(file)); return new String(fileBase64); } catch (Exception e){ e.printStackTrace(); return null; } finally { if(file != null){ } } } public static String getFileBase64ValueByContent(String content){ byte[] fileBase64 = Base64.encodeBase64(content.getBytes()); return new String(fileBase64); } public static byte[] getFileContent(String base64){ return Base64.decodeBase64(base64); } /** * 创建表结构的结构对象 * @param type 表类型(暂未使用) * @return * @throws Exception */ @Deprecated public static final XContentBuilder createFullTextIndexJson(String type) throws Exception{ if(StringUtils.isEmpty(type)){ type = "table"; } XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject(type); { // builder.startObject("_id"); // { // builder.field("path", "mainkey"); // } // builder.endObject(); builder.startObject("properties"); { /** * 设置默认id "_id":{"path":"mainkey"},             "_source" : { "enabled" : false },             "properties" : {                 "mainkey" : { "type" : "string", "index" : "not_analyzed" }             } */ // builder.startObject("mainkey"); // { // builder.field("type", "keyword"); // builder.field("index", "not_analyzed"); // } // builder.endObject(); // 业务主键 builder.startObject("id"); { builder.field("type", "keyword"); } builder.endObject(); // builder.startObject("title"); { builder.field("type", "text"); builder.field("analyzer", "ik_max_word"); } builder.endObject(); // builder.startObject("path"); { builder.field("type", "keyword"); } builder.endObject(); // 数据库中的内容 builder.startObject("text_db"); { builder.field("type", "text"); builder.field("analyzer", "ik_max_word"); } builder.endObject(); // builder.startObject("create_time"); { builder.field("type", "date"); builder.field("format", "yyyy-MM-dd HH:mm:ss"); } builder.endObject(); // builder.startObject("update_time"); { builder.field("type", "date"); builder.field("format", "yyyy-MM-dd HH:mm:ss"); } builder.endObject(); } builder.endObject(); // 附件 builder.startObject("attachment"); { builder.startObject("properties"); { builder.startObject("content"); { builder.field("type", "text"); builder.field("analyzer", "ik_max_word"); } builder.endObject(); } builder.endObject(); } builder.endObject(); } builder.endObject(); } builder.endObject(); return builder; } }