SearchCommonService.java
5.91 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
package com.yoho.search.common;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.monitor.PerformanceMonitor;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class SearchCommonService implements ApplicationEventPublisherAware {
private static final Logger ES_PERFORMANCE = LoggerFactory.getLogger("ES_PERFORMANCE");
@Autowired
private ESClientMgr esClientMgr;
private ApplicationEventPublisher publisher;
private PerformanceMonitor performanceMonitor;
@PostConstruct
void init(){
performanceMonitor = new PerformanceMonitor("ES_PERFORMANCE",ES_PERFORMANCE, 10);
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
private void publishSearchResultEvent(final String indexName, SearchParam searchParam, SearchResult searchResult) {
if (searchParam == null || searchResult == null) {
return;
}
if (searchResult.getTotal() > 0 || searchParam.getFiter() == null) {
return;
}
QueryBuilder filter = searchParam.getFiter();
String filterString = (filter == null ? "" : filter.toString());
if (!filterString.contains(ProductIndexEsField.poolIds)) {
return;
}
publisher.publishEvent(new SearchEvent(indexName, EventReportEnum.SEARCHCOMMONSERVICE_DOSEARCH.getFunctionName(), EventReportEnum.SEARCHCOMMONSERVICE_DOSEARCH
.getMoudleName(), "empty_result", null, "[" + indexName + "][" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()) + "]["
+ filterString + "]"));
}
/**
* 通用的查询接口
*
* @param indexName
* @param searchParam
* @return
*/
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount();
try {
IElasticsearchClient client = esClientMgr.getClient(indexName);
SearchResult searchResult = client.search(indexName, indexName, searchParam);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
return searchResult;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
* 通用的查询接口
*
* @param indexName
* @param
* @return
*/
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(searchParams==null?0:searchParams.size());
try {
if (searchParams == null || searchParams.isEmpty()) {
return new ArrayList<SearchResult>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
for (int i = 0; i < searchParams.size(); i++) {
SearchResult searchResult = results.get(i);
SearchParam searchParam = searchParams.get(i);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
* 通过id获取内容
*
* @param indexName
* @param id
* @return
*/
public Map<String, Object> doGetCommon(final String indexName, final String id) {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
try {
if (StringUtils.isBlank(id)) {
return null;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
GetResponse response = client.get(indexName, indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
return null;
}
return response.getSourceAsMap();
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
* 通过id获取内容
*
* @param indexName
* @return
*/
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(idList==null||idList.isEmpty()?0:1);
try {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
Set<String> idSet = new HashSet<String>();
for (Object id : idList) {
idSet.add(id.toString());
}
MultiGetResponse response = client.multiGet(indexName, indexName, idSet, null);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
results.add(item.getResponse().getSource());
}
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
}