shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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<String, HighlightField> 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<String, String> translate(SaveData d){
        Map<String, String> 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;
    }
}