SearchExplainerService.java
40.7 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
package com.yoho.search.service.searchexplainer;
import com.carrotsearch.hppc.ObjectLookupContainer;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.SearchServiceConfiger;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.es.model.SearchField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.es.utils.SearchFieldUtils;
import com.yoho.search.service.base.ESClientMgr;
import com.yoho.search.service.searchexplainer.models.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Created by ginozhang on 2016/11/16.
*/
@Service
public class SearchExplainerService {
private static final Logger logger = LoggerFactory.getLogger(SearchExplainerService.class);
@Autowired
private ESClientMgr esClientMgr;
@Autowired
private SearchServiceConfiger searchServiceConfiger;
private static final String DEFAULT_ANALYZER = "keyword";
private static final String MATCH_TYPE_STRING = "String Match";
private static final String MATCH_TYPE_RANGE = "Range Match";
private static final String RETURN_TYPE_ALL = "all";
private static final String RETURN_TYPE_MATCH_ONLY = "match_only";
private volatile Map<String, FieldDesc> localFieldDescMap = null;
private volatile Map<String, List<String>> localCopiedFieldMap = null;
private volatile List<String> localMutilFields = null;
private volatile List<FieldWithBoost> localMutilFieldWithBoostList = null;
// 入参和索引中字段名的映射
@SuppressWarnings("serial")
private final static Map<String, EsField> fieldMap = new HashMap<String, EsField>() {
{
// 字符类
put("brand", new EsField(ProductIndexEsField.brandId, MATCH_TYPE_STRING));
put("shop", new EsField(ProductIndexEsField.shopId, MATCH_TYPE_STRING));
put("msort", new EsField(ProductIndexEsField.maxSortId, MATCH_TYPE_STRING));
put("misort", new EsField(ProductIndexEsField.middleSortId, MATCH_TYPE_STRING));
put("sort", new EsField(ProductIndexEsField.smallSortId, MATCH_TYPE_STRING));
put("color", new EsField(ProductIndexEsField.colorIds, MATCH_TYPE_STRING));
put("size", new EsField(ProductIndexEsField.sizeIds, MATCH_TYPE_STRING));
put("price", new EsField(ProductIndexEsField.salesPrice, MATCH_TYPE_STRING));
put("gender", new EsField(ProductIndexEsField.gender, MATCH_TYPE_STRING));
put("specialoffer", new EsField(ProductIndexEsField.specialoffer, MATCH_TYPE_STRING));
put("isdiscount", new EsField(ProductIndexEsField.isDiscount, MATCH_TYPE_STRING));
put("promotion", new EsField(ProductIndexEsField.ispromotion, MATCH_TYPE_STRING));
put("vdt", new EsField(ProductIndexEsField.vipDiscountType, MATCH_TYPE_STRING));
put("attribute", new EsField(ProductIndexEsField.attribute, MATCH_TYPE_STRING));
put("limited", new EsField(ProductIndexEsField.islimited, MATCH_TYPE_STRING));
put("new", new EsField(ProductIndexEsField.isnew, MATCH_TYPE_STRING));
put("outlets", new EsField(ProductIndexEsField.isOutlets, MATCH_TYPE_STRING));
put("status", new EsField(ProductIndexEsField.status, MATCH_TYPE_STRING));
put("style", new EsField(ProductIndexEsField.styleIds, MATCH_TYPE_STRING));
put("sell_channels", new EsField(ProductIndexEsField.sellChannels, MATCH_TYPE_STRING));
put("folder_id", new EsField(ProductIndexEsField.folderId, MATCH_TYPE_STRING));
put("series_id", new EsField(ProductIndexEsField.seriesId, MATCH_TYPE_STRING));
put("day", new EsField(ProductIndexEsField.shelveDay, MATCH_TYPE_STRING));
put("brand", new EsField(ProductIndexEsField.salesPrice, MATCH_TYPE_STRING));
put("shop", new EsField(ProductIndexEsField.salesPrice, MATCH_TYPE_STRING));
put("brand", new EsField(ProductIndexEsField.brandId, MATCH_TYPE_STRING));
put("shop", new EsField(ProductIndexEsField.shopId, MATCH_TYPE_STRING));
put("brand", new EsField(ProductIndexEsField.brandId, MATCH_TYPE_STRING));
put("ageLevel", new EsField(ProductIndexEsField.ageLevel, MATCH_TYPE_STRING));
put("product_skn", new EsField(ProductIndexEsField.productSkn, MATCH_TYPE_STRING));
// 范围类
put("stocknumber", new EsField(ProductIndexEsField.storageNum, MATCH_TYPE_RANGE));
put("p_d", new EsField(ProductIndexEsField.promotionDiscountInt, MATCH_TYPE_RANGE));
put("p_d_int", new EsField(ProductIndexEsField.promotionDiscountInt, MATCH_TYPE_RANGE));
put("first_shelve_time", new EsField(ProductIndexEsField.firstShelveTime, MATCH_TYPE_RANGE));
put("shelve_time", new EsField(ProductIndexEsField.shelveTime, MATCH_TYPE_RANGE));
}
};
public String fieldConvert(String paraField) {
EsField esField = fieldMap.get(paraField);
if (esField == null) {
return null;
}
return esField.getFieldName();
}
public void clearCachedData() {
this.localMutilFields = null;
this.localMutilFieldWithBoostList = null;
this.localCopiedFieldMap = null;
this.localFieldDescMap = null;
}
public Map<String, Object> show(Map<String, String> paramMap) throws Exception {
Map<String, Object> map = new LinkedHashMap<>();
String skn = paramMap.get("skn");
if (StringUtils.isEmpty(skn)) {
map.put("code", "400");
map.put("message", "skn is null");
return map;
}
String keyword = paramMap.get("query");
long start = System.currentTimeMillis();
logger.info("Begin to show skn tokens. skn={}, keyword={}.", skn, keyword);
List<SearchFieldResult> resultList = new ArrayList<>();
// 1. 获取document
Map<String, Object> document = getDocumentBySkn(skn);
logger.info("Get the document for the product. document: \n{}", document);
// 2. 解析mapping文件获取字段元数据
Map<String, FieldDesc> fieldDescMap = parseMapping();
logger.info("Get the field description by mapping. fieldDescMap: \n{}", fieldDescMap);
// 3. 处理多字段和copy_to的字段并赋值
processMultiFields(fieldDescMap, document);
processCopiedFields(fieldDescMap, document);
logger.info("Set the value of copied field succeeded.");
// 4. 分析每个multi-match的每个字段
List<String> sortedSearchFields = getSortedSearchFields();
logger.info("Get the search fields. fields: {}", sortedSearchFields);
// TODO: 这里先只取search_analyzer是ik_complex的 对于productSkn_ansj进行忽略处理
FieldDesc maxBoostField = fieldDescMap.get(sortedSearchFields.get(0));
Assert.notNull(maxBoostField.search_analyzer, "for " + sortedSearchFields.get(0));
for (FieldWithBoost fieldWithBoost : this.localMutilFieldWithBoostList) {
FieldDesc fieldDesc = fieldDescMap.get(fieldWithBoost.fieldName);
Assert.notNull(fieldDesc, "for " + fieldWithBoost.fieldName);
if (maxBoostField.search_analyzer.equals(fieldDesc.search_analyzer)) {
resultList.add(showSingleField(fieldWithBoost, document.get(fieldWithBoost.fieldName), fieldDesc));
}
}
// 5. 对query的关键字进行分词
List<String> searchTokens = StringUtils.isNotEmpty(keyword) ? getKeyWordAnalyzerResult(maxBoostField, keyword) : null;
// 6. 处理匹配结果。匹配结果暂时只考虑是cross_fields的 并且所有字段的search_analyzer都是ik_complex
boolean queryMatchResult = true;
if (CollectionUtils.isNotEmpty(searchTokens)) {
for (String searchToken : searchTokens) {
boolean isTokenMatched = false;
for (SearchFieldResult searchFieldResult : resultList) {
if (searchFieldResult.getTokens().contains(searchToken)) {
searchFieldResult.addMatchToken(searchToken);
searchFieldResult.setMatchResult(true);
isTokenMatched = true;
break;
}
}
// 只有所有token都匹配成功才返回true
queryMatchResult = queryMatchResult && isTokenMatched;
}
}
for (SearchFieldResult searchFieldResult : resultList) {
searchFieldResult.setTokens(Arrays.asList(StringUtils.join(searchFieldResult.getTokens(), ";")));
}
map.put("code", "200");
map.put("message", "show skn tokens");
map.put("data", resultList);
map.put("searchTokens", searchTokens);
map.put("queryMatchResult", queryMatchResult);
logger.info("End to show skn tokens. skn={}, keyword={}, cost={}.", skn, keyword, System.currentTimeMillis() - start);
return map;
}
private SearchFieldResult showSingleField(FieldWithBoost fieldWithBoost, Object fieldValue, FieldDesc fieldDesc) throws Exception {
SearchFieldResult searchFieldResult = new SearchFieldResult();
searchFieldResult.setFieldName(fieldWithBoost.fieldName);
searchFieldResult.setBoost(fieldWithBoost.boost);
searchFieldResult.setIndexAnalyzer(fieldDesc.analyzer);
searchFieldResult.setSearchAnalyzer(fieldDesc.search_analyzer);
String fieldValueStr = fieldValue != null ? fieldValue.toString() : null;
List<String> tokens = getAnalyzerResult(fieldDesc, fieldValueStr);
searchFieldResult.setFieldValue(fieldValueStr);
searchFieldResult.setTokens(tokens);
return searchFieldResult;
}
public Map<String, Object> explain(Map<String, String> paramMap) throws Exception {
long start = System.currentTimeMillis();
logger.info("Begin to explain search. start: {}, paramMap: {}.", start, paramMap);
String skn = paramMap.get("skn");
if (StringUtils.isEmpty(skn)) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("code", "400");
map.put("message", "skn is null");
return map;
}
String returnType = paramMap.get("returnType");
if (returnType == null) {
returnType = RETURN_TYPE_MATCH_ONLY;
}
String multiSearchType = paramMap.get("multiSearchType");
if (multiSearchType == null) {
multiSearchType = searchServiceConfiger.getSearchMultiMatchQueryType();
}
String keyword = paramMap.get("query");
logger.info("Begin to explain search keyword [{}] for product skn {}.", keyword, skn);
// 1. 根据skn获取es中的数据
Map<String, Object> document = getDocumentBySkn(skn);
logger.info("Get the document for the product. document: \n{}", document);
// 2.分析过滤的参数
Map<String, Object> explainResult = explainFilter(paramMap, document);
if ((Boolean) explainResult.get("filter_match") && StringUtils.isNotEmpty(keyword)) {
List<SearchMatch> queryMatchList = explainMultiMatchQuery(document, keyword, returnType, multiSearchType);
explainResult.put("query_match", queryMatchList.stream().anyMatch(SearchMatch::isMatch));
explainResult.put("query_list", queryMatchList);
}
logger.info("End to explain search keyword [{}] for product skn {}. cost: {}", keyword, skn, System.currentTimeMillis() - start);
return explainResult;
}
private Map<String, Object> explainFilter(Map<String, String> paramMap, Map<String, Object> document) {
Map<String, Object> map = new LinkedHashMap<>();
List<String> warns = new ArrayList<>();
boolean isFilterMatch = true;
List<FilterMatch> filterMatchList = new ArrayList<>();
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
String key = entry.getKey();
if (key.equals("query") || key.equals("skn") || key.equals("returnType")) {
continue;
}
if (!fieldMap.containsKey(key)) {
warns.add("parameter " + key + " is invalid\n");
continue;
}
Object value = document.get(fieldConvert(key));
if (value != null) {
String paraValue = value.toString();
EsField esField = fieldMap.get(key);
// 将入参和字段值都转化为字符数组
String[] paraArray = entry.getValue().split(",");
String[] indexArray = paraValue.split(",");
Set<String> indexSet = new HashSet<String>(indexArray.length);
for (String i : indexArray) {
indexSet.add(i);
}
// 字符匹配比较
if (MATCH_TYPE_STRING.equals(esField.getFieldType())) {
for (String p : paraArray) {
if (!indexSet.contains(p)) {
filterMatchList.add(new FilterMatch(key, entry.getValue(), paraValue, esField.getFieldType(), false));
isFilterMatch = false;
}
}
}
// 取值范围比较
if (MATCH_TYPE_RANGE.equals(esField.getFieldType())) {
if (key.equals("stocknumber")) {
if (Integer.valueOf(value.toString()) < 1) {
filterMatchList.add(new FilterMatch(key, entry.getValue(), paraValue, esField.getFieldType(), false));
isFilterMatch = false;
}
} else if (paraArray.length == 2 && indexArray.length == 1) {
Integer paraRangeStart = Integer.valueOf(paraArray[0]);
Integer paraRangeEnd = Integer.valueOf(paraArray[1]);
Integer indexValue = Integer.valueOf(indexArray[0]);
if (indexValue < paraRangeStart || indexValue > paraRangeEnd) {
filterMatchList.add(new FilterMatch(key, entry.getValue(), paraValue, esField.getFieldType(), false));
isFilterMatch = false;
}
}
}
}
}
map.put("code", "200");
map.put("message", "search explainer");
if (CollectionUtils.isNotEmpty(warns)) {
map.put("warn", warns);
}
map.put("filter_match", isFilterMatch);
map.put("filter_list", filterMatchList);
return map;
}
private List<SearchMatch> explainMultiMatchQuery(Map<String, Object> document, String keyword, String returnType, String multiSearchType) throws Exception {
// 3. 解析mapping文件获取字段元数据
Map<String, FieldDesc> fieldDescMap = parseMapping();
logger.info("Get the field description by mapping. fieldDescMap: \n{}", fieldDescMap);
// 4. 处理多字段和copy_to的字段并赋值
processMultiFields(fieldDescMap, document);
processCopiedFields(fieldDescMap, document);
logger.info("Set the value of copied field succeeded.");
// 5. 分析每个multi-match的每个字段 根据分词结果判定是否匹配
List<String> sortedSearchFields = getSortedSearchFields();
logger.info("Get the search fields. fields: {}", sortedSearchFields);
if ("cross_fields".equalsIgnoreCase(multiSearchType)) {
return explainCrossFieldsQuery(document, keyword, returnType, fieldDescMap, sortedSearchFields);
} else {
return explainBestFieldsQuery(document, keyword, returnType, fieldDescMap, sortedSearchFields);
}
}
private List<SearchMatch> explainBestFieldsQuery(Map<String, Object> document, String keyword, String returnType, Map<String, FieldDesc> fieldDescMap,
List<String> sortedSearchFields) throws Exception {
boolean isQueryMatch = false;
boolean isFieldQueryMatch = false;
List<SearchMatch> queryMatchList = new ArrayList<>();
for (String field : sortedSearchFields) {
String value = document.get(field) != null ? document.get(field).toString() : null;
FieldDesc fieldDesc = fieldDescMap.get(field);
Assert.notNull(fieldDesc, "no field " + field + " defined.");
List<String> analyzerResult = getAnalyzerResult(fieldDesc, value);
List<String> keywordAnalyzerResult = getKeyWordAnalyzerResult(fieldDesc, keyword);
isFieldQueryMatch = judgeMatch(analyzerResult, keywordAnalyzerResult);
isQueryMatch = isFieldQueryMatch || isQueryMatch;
// 如果是匹配模式则只返回匹配的字段,如果是ALL模式则返回索引字段
if (isFieldQueryMatch || RETURN_TYPE_ALL.equals(returnType)) {
queryMatchList.add(new FieldMatch(field, value, fieldDesc.analyzer, analyzerResult, fieldDesc.search_analyzer, keywordAnalyzerResult, isFieldQueryMatch));
}
if (isFieldQueryMatch && RETURN_TYPE_MATCH_ONLY.equals(returnType)) {
// 找到匹配的就返回
break;
}
}
return queryMatchList;
}
private List<SearchMatch> explainCrossFieldsQuery(Map<String, Object> document, String keyword, String returnType, Map<String, FieldDesc> fieldDescMap,
List<String> sortedSearchFields) throws Exception {
List<SearchMatch> groupMatchList = new ArrayList<>();
List<FieldDesc> searchFieldDescList = new ArrayList<>();
fieldDescMap.forEach((field, fieldDesc) -> {
if (sortedSearchFields.contains(field)) {
if (StringUtils.isEmpty(fieldDesc.analyzer)) {
fieldDesc.analyzer = DEFAULT_ANALYZER;
}
if (StringUtils.isEmpty(fieldDesc.search_analyzer)) {
fieldDesc.search_analyzer = fieldDesc.analyzer;
}
searchFieldDescList.add(fieldDesc);
}
});
// 根据search_analyzer进行分组
Map<String, List<FieldDesc>> groupMap = searchFieldDescList.stream().collect(Collectors.groupingBy(fieldDesc -> fieldDesc.search_analyzer));
for (Map.Entry<String, List<FieldDesc>> groupEntry : groupMap.entrySet()) {
// 对于cross_field来说 必须所有输入的token在同一组内
String searchAnalyzer = groupEntry.getValue().get(0).search_analyzer;
List<String> keywordAnalyzerResult = getAnalyzerTokens(keyword, searchAnalyzer);
List<String> sortedFieldDescNames = groupEntry.getValue().stream().map(fieldDesc -> fieldDesc.field).collect(Collectors.toList());
boolean isMatch = true;
List<FieldMatch> fieldMatchList = new ArrayList<>();
for (String token : keywordAnalyzerResult) {
boolean isTokenMatch = false;
for (String field : sortedSearchFields) {
// 检查field是否匹配token
if (!sortedFieldDescNames.contains(field)) {
continue;
}
String value = document.get(field) != null ? document.get(field).toString() : null;
FieldDesc fieldDesc = fieldDescMap.get(field);
Assert.notNull(fieldDesc, "for " + field);
List<String> analyzerResult = getAnalyzerResult(fieldDesc, value);
boolean isTokenMatchInThisField = analyzerResult.contains(token);
isTokenMatch = isTokenMatch || isTokenMatchInThisField;
if (isTokenMatchInThisField || RETURN_TYPE_ALL.equals(returnType)) {
fieldMatchList.add(new FieldMatch(field, value, fieldDesc.analyzer, analyzerResult, fieldDesc.search_analyzer, Arrays.asList(token),
isTokenMatchInThisField));
}
if (isTokenMatchInThisField && RETURN_TYPE_MATCH_ONLY.equals(returnType)) {
// 找到匹配的就返回
break;
}
}
isMatch = isMatch && isTokenMatch;
}
if (isMatch || RETURN_TYPE_ALL.equals(returnType)) {
groupMatchList.add(new GroupMatch("", searchAnalyzer, keywordAnalyzerResult, fieldMatchList, isMatch));
}
}
return groupMatchList;
}
public Map<String, Object> getTokens(String skn) throws Exception {
long start = System.currentTimeMillis();
logger.info("Begin to get tokens for product skn {}.", skn);
// 1. 根据skn获取es中的数据
Map<String, Object> document = getDocumentBySkn(skn);
logger.info("Get the document for the product. document: \n{}", document);
// 2. 解析mapping文件获取字段元数据
Map<String, FieldDesc> fieldDescMap = parseMapping();
logger.info("Get the field description by mapping. fieldDescMap: \n{}", fieldDescMap);
// 3. 处理多字段和copy_to的字段并赋值
processMultiFields(fieldDescMap, document);
processCopiedFields(fieldDescMap, document);
logger.info("Set the value of copied field succeeded.");
// 4. 分析每个multi-match的每个字段 根据权重优先级展示token列表
List<String> sortedSearchFields = getSortedSearchFields();
logger.info("Get the search fields. fields: {}", sortedSearchFields);
// boolean isMatch = false;
List<String> tokens = new ArrayList<>();
for (String field : sortedSearchFields) {
String value = document.get(field) != null ? document.get(field).toString() : null;
FieldDesc fieldDesc = fieldDescMap.get(field);
Assert.notNull(fieldDesc, "no field " + field + " defined.");
List<String> analyzerResult = getAnalyzerResult(fieldDesc, value);
if (CollectionUtils.isNotEmpty(analyzerResult)) {
analyzerResult.removeAll(tokens);
tokens.addAll(analyzerResult);
}
}
Map<String, Object> map = new HashMap<>();
map.put("code", "200");
map.put("message", "product tokens.");
map.put("data", tokens);
logger.info("End to get tokens for product skn {}. cost: {}", skn, System.currentTimeMillis() - start);
return map;
}
private boolean judgeMatch(List<String> analyzerResult, List<String> keywordAnalyzerResult) {
// 对于best_fields必须包含所有token
return analyzerResult.containsAll(keywordAnalyzerResult);
}
private List<String> getKeyWordAnalyzerResult(FieldDesc fieldDesc, String keyword) throws ExecutionException {
if (fieldDesc.noNeedAnalyzer()) {
return Arrays.asList(keyword);
}
String searchAnalyzer = fieldDesc.search_analyzer;
if (StringUtils.isEmpty(searchAnalyzer)) {
searchAnalyzer = fieldDesc.analyzer;
}
if (StringUtils.isEmpty(searchAnalyzer)) {
searchAnalyzer = DEFAULT_ANALYZER;
}
return getAnalyzerTokens(keyword, searchAnalyzer);
}
private List<String> getAnalyzerResult(FieldDesc fieldDesc, String value) throws ExecutionException {
if (StringUtils.isEmpty(value)) {
return new ArrayList<>();
}
if (fieldDesc.noNeedAnalyzer()) {
return Arrays.asList(value);
}
String analyzer = fieldDesc.analyzer;
if (StringUtils.isEmpty(analyzer)) {
analyzer = DEFAULT_ANALYZER;
}
return getAnalyzerTokens(value, analyzer);
}
private List<String> getAnalyzerTokens(String text, String analyzer) throws ExecutionException {
// 使用guava的本地缓存
return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new CacheLoader<String, List<String>>() {
public List<String> load(String key) {
// String tempAnalyzer = key.split("@")[0];
String tempText = key.split("@")[1];
if ("keyword".equals(analyzer)) {
// keyword 分词就不去调用ES
return Arrays.asList(tempText);
}
IElasticsearchClient client = esClientMgr.getClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
List<AnalyzeResponse.AnalyzeToken> list = client.getAnalyzeResponse(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, text, analyzer).getTokens();
return list.stream().map(item -> item.getTerm()).collect(Collectors.toList());
}
}).get(analyzer + "@" + text);
}
private Map<String, Object> getDocumentBySkn(String skn) throws Exception {
IElasticsearchClient client = esClientMgr.getClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
if (!client.indexExists(ISearchConstants.INDEX_NAME_PRODUCT_INDEX)) {
throw new RuntimeException("index not exist");
}
SearchParam searchParam = new SearchParam();
searchParam.setQuery(QueryBuilders.termQuery("productSkn", skn));
searchParam.setSize(1);
SearchResult searchResult = client.search(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) {
throw new RuntimeException("product skn not exist");
}
return searchResult.getResultList().get(0);
}
@SuppressWarnings("unchecked")
private Map<String, FieldDesc> parseMapping() throws Exception {
if (localFieldDescMap != null) {
return localFieldDescMap;
}
synchronized (this) {
if (localFieldDescMap != null) {
// double check
return localFieldDescMap;
}
// 从ES获取mapping
IElasticsearchClient client = esClientMgr.getClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
GetMappingsResponse response = client.getMapping(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
Assert.notNull(response, ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
ObjectLookupContainer<String> keys = response.getMappings().keys();
if (keys.isEmpty()) {
throw new RuntimeException("Cannot get field mapping!");
}
String realIndexName = Arrays.asList(keys.toArray(String.class)).get(0);
MappingMetaData mappingMetaData = response.getMappings().get(realIndexName).get(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
if (mappingMetaData == null) {
throw new RuntimeException("Cannot get field mapping!");
}
Map<String, FieldDesc> fieldDescMap = new HashMap<>();
try {
Map<String, Object> sourceAsMap = mappingMetaData.getSourceAsMap();
Map<String, Object> properties = (Map<String, Object>) sourceAsMap.get("properties");
for (Map.Entry<String, Object> p : properties.entrySet()) {
String field = p.getKey();
Map<String, Object> fieldPropMap = (Map<String, Object>) p.getValue();
parseToFieldDesc(fieldDescMap, field, fieldPropMap);
}
localFieldDescMap = fieldDescMap;
return localFieldDescMap;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@SuppressWarnings("unchecked")
private void parseToFieldDesc(Map<String, FieldDesc> fieldDescMap, String field, Map<String, Object> fieldPropMap) {
if (fieldPropMap.containsKey("fields")) {
// 多字段类型
Map<String, Object> innerFieldsMap = (Map<String, Object>) fieldPropMap.get("fields");
for (String innerField : innerFieldsMap.keySet()) {
String realFieldName = innerField;
// if (!innerField.equals(field)) {
realFieldName = field + "." + innerField;
// }
parseSingle(fieldDescMap, realFieldName, (Map<String, Object>) innerFieldsMap.get(innerField));
}
if (!innerFieldsMap.containsKey(field)) {
// ES的mapping是平铺的 所以相同名字的不在fields里面
parseSingle(fieldDescMap, field, fieldPropMap);
}
} else if (fieldPropMap.containsKey("properties")) {
// nested object类型 搜索暂不支持查询内嵌对象的字段
} else {
parseSingle(fieldDescMap, field, fieldPropMap);
}
}
@SuppressWarnings("unchecked")
private void parseSingle(Map<String, FieldDesc> fieldDescMap, String field, Map<String, Object> fieldPropMap) {
FieldDesc fieldDesc = new FieldDesc();
fieldDesc.field = field;
fieldDesc.type = (String) fieldPropMap.get("type");
fieldDesc.index = (Boolean) fieldPropMap.get("index");
fieldDesc.analyzer = (String) fieldPropMap.get("analyzer");
fieldDesc.search_analyzer = (String) fieldPropMap.get("search_analyzer");
if (fieldDesc.search_analyzer == null && fieldDesc.analyzer != null) {
fieldDesc.search_analyzer = fieldDesc.analyzer;
}
Object copyTo = fieldPropMap.get("copy_to");
if (copyTo instanceof List) {
List<String> copyToFields = (List<String>) copyTo;
for (int i = 0; i < copyToFields.size(); i++) {
fieldDesc.addCopyTo(copyToFields.get(i));
}
} else if (copyTo instanceof String) {
fieldDesc.addCopyTo((String) copyTo);
}
fieldDescMap.put(field, fieldDesc);
}
private void processMultiFields(Map<String, FieldDesc> fieldDescMap, Map<String, Object> document) {
for (String field : fieldDescMap.keySet()) {
String[] parts = field.split("\\.");
if (parts != null && parts.length == 2) {
document.put(field, document.get(parts[0]));
}
}
}
private void processCopiedFields(Map<String, FieldDesc> fieldDescMap, Map<String, Object> document) {
Map<String, List<String>> copiedFieldMap = getCopiedFieldMap(fieldDescMap);
for (Map.Entry<String, List<String>> entry : copiedFieldMap.entrySet()) {
String copiedField = entry.getKey();
StringBuilder sb = new StringBuilder();
for (String field : entry.getValue()) {
if (document.get(field) != null) {
sb.append(document.get(field)).append(" ");
}
}
document.put(copiedField, sb.toString());
}
}
private Map<String, List<String>> getCopiedFieldMap(Map<String, FieldDesc> fieldDescMap) {
if (localCopiedFieldMap != null) {
return localCopiedFieldMap;
}
synchronized (this) {
if (localCopiedFieldMap != null) {
// double check
return localCopiedFieldMap;
}
Map<String, List<String>> copiedFieldMap = new HashMap<>();
for (Map.Entry<String, FieldDesc> entry : fieldDescMap.entrySet()) {
for (String dest : entry.getValue().copy_to) {
List<String> list = copiedFieldMap.get(dest);
if (list == null) {
list = new ArrayList<>();
copiedFieldMap.put(dest, list);
}
list.add(entry.getKey());
}
}
localCopiedFieldMap = copiedFieldMap;
return localCopiedFieldMap;
}
}
private List<String> getSortedSearchFields() {
if (localMutilFields != null) {
return localMutilFields;
}
synchronized (this) {
if (localMutilFields != null) {
// double check
return localMutilFields;
}
List<FieldWithBoost> list = new ArrayList<>();
List<SearchField> searchFields = SearchFieldUtils.getFuzzySearchFields();
for (SearchField searchField : searchFields) {
list.add(new FieldWithBoost(searchField.getEsField(), (int)searchField.getBoost()));
}
Collections.sort(list);
localMutilFieldWithBoostList = list;
List<String> result = new ArrayList<>();
for (FieldWithBoost item : localMutilFieldWithBoostList) {
result.add(item.fieldName);
}
localMutilFields = result;
return localMutilFields;
}
}
public static class FieldWithBoost implements Comparable<FieldWithBoost> {
int boost;
String fieldName;
public FieldWithBoost(String fieldName, int boost) {
this.boost = boost;
this.fieldName = fieldName;
}
@Override
public int compareTo(FieldWithBoost o) {
return o.boost - boost;
}
}
static class FieldDesc {
String field;
String type;
Boolean index;
String analyzer;
String search_analyzer;
Set<String> copy_to = new HashSet<>();
public void addCopyTo(String item) {
copy_to.add(item);
}
public boolean noNeedAnalyzer() {
if (index == null) {
return false;
}
if (index == Boolean.TRUE) {
return false;
}
if ("text".equals(type)) {
return false;
}
return true;
}
@Override
public String toString() {
return "FieldDesc{" + "type='" + type + '\'' + ", index='" + index + '\'' + ", analyzer='" + analyzer + '\'' + ", search_analyzer='" + search_analyzer + '\'' + '}';
}
}
public Map<String, Object> multiFieldAnalyzeList(Map<String, String> paramMap) throws Exception {
Map<String, Object> map = new LinkedHashMap<>();
String skn = paramMap.get("skn");
if (StringUtils.isEmpty(skn)) {
map.put("code", "400");
map.put("message", "skn is null");
return map;
}
String keyword = paramMap.get("query");
long start = System.currentTimeMillis();
logger.info("Begin to show skn tokens. skn={}, keyword={}.", skn, keyword);
// 1. 获取document
Map<String, Object> document = getDocumentBySkn(skn);
logger.info("Get the document for the product. document: \n{}", document);
// 2. 解析mapping文件获取字段元数据
Map<String, FieldDesc> fieldDescMap = parseMapping();
logger.info("Get the field description by mapping. fieldDescMap: \n{}", fieldDescMap);
// 3. 处理多字段和copy_to的字段并赋值
processMultiFields(fieldDescMap, document);
processCopiedFields(fieldDescMap, document);
logger.info("Set the value of copied field succeeded.");
// 4. 分析每个multi-match的每个字段
List<String> sortedSearchFields = getSortedSearchFields();
logger.info("Get the search fields. fields: {}", sortedSearchFields);
Map<String, FieldDesc> multiMatchFieldMap = new HashMap<>();
sortedSearchFields.forEach(e -> {
if (document.get(e) != null && StringUtils.isNotBlank(document.get(e).toString())) {
multiMatchFieldMap.put(e, fieldDescMap.get(e));
}
});
List<String> multiMatchFieldSearchAnalyzes = multiMatchFieldMap.values().stream().map(e -> e.search_analyzer).distinct().collect(Collectors.toList());
Map<String, List<FieldDesc>> multiMatchFieldAnalyzeFieldsMap = multiMatchFieldMap.values().stream().collect(Collectors.groupingBy(e -> e.analyzer));
List<SearchFieldResult> resultList = getSearchFieldResult(keyword, multiMatchFieldSearchAnalyzes, multiMatchFieldAnalyzeFieldsMap, document);
map.put("code", "200");
map.put("message", "show skn tokens");
map.put("data", resultList);
logger.info("End to show skn tokens. skn={}, keyword={}, cost={}.", skn, keyword, System.currentTimeMillis() - start);
return map;
}
private List<SearchFieldResult> getSearchFieldResult(String keyword, List<String> multiMatchFieldSearchAnalyzes, Map<String, List<FieldDesc>> multiMatchFieldAnalyzeFieldsMap, Map<String, Object> document) throws Exception {
Map<String, List<AnalyzeResponse.AnalyzeToken>> keywordSearchAnalyzesMap = new HashMap<>();
multiMatchFieldSearchAnalyzes.forEach(e -> keywordSearchAnalyzesMap.put(e, getAnalyzerResultWithMultiValues(e, keyword)));
List<SearchFieldResult> searchFieldResultList = new ArrayList<>();
Map<String, FieldWithBoost> localMutilFieldWithBoostMap = localMutilFieldWithBoostList.stream().collect(Collectors.toMap((e -> e.fieldName), p -> p));
multiMatchFieldAnalyzeFieldsMap.entrySet().forEach(e -> {
List<FieldDesc> analyzeFields = e.getValue();
List<String> fieldValueList = analyzeFields.stream().map(f -> document.get(f.field).toString()).collect(Collectors.toList());
List<AnalyzeResponse.AnalyzeToken> analyzeResponse = getAnalyzerResultWithMultiValues(e.getKey(), fieldValueList.toArray(new String[fieldValueList.size()]));
List<List<String>> tokensGroupList = new ArrayList<>();
int position = 0;
int analyzeResponseIndex = 0;
for (int i = 0; i < analyzeFields.size(); i++) {
List<String> tokenList = new ArrayList<>();
tokensGroupList.add(tokenList);
for (int j = analyzeResponseIndex; j < analyzeResponse.size(); j++) {
if (analyzeResponse.get(j).getPosition() - position < 100) {
tokenList.add(analyzeResponse.get(j).getTerm());
position = analyzeResponse.get(j).getPosition();
} else {
analyzeResponseIndex = j;
position = analyzeResponse.get(j).getPosition();
break;
}
}
}
for (int i = 0; i < analyzeFields.size(); i++) {
SearchFieldResult searchFieldResult = new SearchFieldResult();
searchFieldResult.setFieldName(analyzeFields.get(i).field);
searchFieldResult.setBoost(localMutilFieldWithBoostMap.get(analyzeFields.get(i).field).boost);
searchFieldResult.setIndexAnalyzer(analyzeFields.get(i).analyzer);
searchFieldResult.setSearchAnalyzer(analyzeFields.get(i).search_analyzer);
searchFieldResult.setFieldValue(document.get(analyzeFields.get(i).field).toString());
List<String> tokens = tokensGroupList.get(i);
searchFieldResult.setTokens(Arrays.asList(StringUtils.join(tokens, ";")));
List<String> matchTokens = keywordSearchAnalyzesMap.get(analyzeFields.get(i).search_analyzer).stream().filter(kt -> tokens.contains(kt)).map(AnalyzeResponse.AnalyzeToken::getTerm).collect(Collectors.toList());
searchFieldResult.addAllMatchToken(matchTokens);
searchFieldResult.setMatchResult(CollectionUtils.isNotEmpty(matchTokens));
searchFieldResultList.add(searchFieldResult);
}
});
return searchFieldResultList;
}
private List<AnalyzeResponse.AnalyzeToken> getAnalyzerResultWithMultiValues(String analyzer, String... value) {
IElasticsearchClient client = esClientMgr.getClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
List<AnalyzeResponse.AnalyzeToken> analyzeResponse = client.getAnalyzeResponseWithMultiValues(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, analyzer, value).getTokens();
return analyzeResponse;
}
}