|
@@ -8,17 +8,22 @@ import java.util.List; |
|
@@ -8,17 +8,22 @@ import java.util.List; |
8
|
import java.util.Map;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
9
|
import java.util.Set;
|
10
|
|
10
|
|
|
|
11
|
+import javax.annotation.PostConstruct;
|
|
|
12
|
+
|
11
|
import org.apache.commons.lang.StringUtils;
|
13
|
import org.apache.commons.lang.StringUtils;
|
12
|
import org.elasticsearch.action.get.GetResponse;
|
14
|
import org.elasticsearch.action.get.GetResponse;
|
13
|
import org.elasticsearch.action.get.MultiGetItemResponse;
|
15
|
import org.elasticsearch.action.get.MultiGetItemResponse;
|
14
|
import org.elasticsearch.action.get.MultiGetResponse;
|
16
|
import org.elasticsearch.action.get.MultiGetResponse;
|
15
|
import org.elasticsearch.index.query.QueryBuilder;
|
17
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
18
|
+import org.slf4j.Logger;
|
|
|
19
|
+import org.slf4j.LoggerFactory;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.context.ApplicationEventPublisher;
|
21
|
import org.springframework.context.ApplicationEventPublisher;
|
18
|
import org.springframework.context.ApplicationEventPublisherAware;
|
22
|
import org.springframework.context.ApplicationEventPublisherAware;
|
19
|
import org.springframework.stereotype.Service;
|
23
|
import org.springframework.stereotype.Service;
|
20
|
|
24
|
|
21
|
import com.yoho.error.event.SearchEvent;
|
25
|
import com.yoho.error.event.SearchEvent;
|
|
|
26
|
+import com.yoho.search.base.monitor.PerformanceMonitor;
|
22
|
import com.yoho.search.base.utils.EventReportEnum;
|
27
|
import com.yoho.search.base.utils.EventReportEnum;
|
23
|
import com.yoho.search.core.es.IElasticsearchClient;
|
28
|
import com.yoho.search.core.es.IElasticsearchClient;
|
24
|
import com.yoho.search.core.es.impl.YohoIndexHelper;
|
29
|
import com.yoho.search.core.es.impl.YohoIndexHelper;
|
|
@@ -28,13 +33,21 @@ import com.yoho.search.core.es.model.SearchResult; |
|
@@ -28,13 +33,21 @@ import com.yoho.search.core.es.model.SearchResult; |
28
|
@Service
|
33
|
@Service
|
29
|
public class SearchCommonService implements ApplicationEventPublisherAware {
|
34
|
public class SearchCommonService implements ApplicationEventPublisherAware {
|
30
|
|
35
|
|
|
|
36
|
+ private static final Logger ES_PERFORMANCE = LoggerFactory.getLogger("ES_PERFORMANCE");
|
|
|
37
|
+
|
31
|
@Autowired
|
38
|
@Autowired
|
32
|
private ESClientMgr esClientMgr;
|
39
|
private ESClientMgr esClientMgr;
|
33
|
@Autowired
|
40
|
@Autowired
|
34
|
private YohoIndexHelper yohoIndexHelper;
|
41
|
private YohoIndexHelper yohoIndexHelper;
|
35
|
|
42
|
|
36
|
private ApplicationEventPublisher publisher;
|
43
|
private ApplicationEventPublisher publisher;
|
37
|
-
|
44
|
+ private PerformanceMonitor performanceMonitor;
|
|
|
45
|
+
|
|
|
46
|
+ @PostConstruct
|
|
|
47
|
+ void init(){
|
|
|
48
|
+ performanceMonitor = new PerformanceMonitor("ES_PERFORMANCE",ES_PERFORMANCE, 10);
|
|
|
49
|
+ }
|
|
|
50
|
+
|
38
|
@Override
|
51
|
@Override
|
39
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
52
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
40
|
this.publisher = applicationEventPublisher;
|
53
|
this.publisher = applicationEventPublisher;
|
|
@@ -65,12 +78,20 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -65,12 +78,20 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
65
|
* @return
|
78
|
* @return
|
66
|
*/
|
79
|
*/
|
67
|
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
|
80
|
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
|
68
|
- IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
69
|
- SearchResult searchResult = client.search(indexName, indexName, searchParam);
|
|
|
70
|
- this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
|
|
71
|
- return searchResult;
|
81
|
+ long begin = System.currentTimeMillis();
|
|
|
82
|
+ performanceMonitor.addVisitCount();
|
|
|
83
|
+ try {
|
|
|
84
|
+ IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
85
|
+ SearchResult searchResult = client.search(indexName, indexName, searchParam);
|
|
|
86
|
+ this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
|
|
87
|
+ return searchResult;
|
|
|
88
|
+ } catch (Exception e) {
|
|
|
89
|
+ throw e;
|
|
|
90
|
+ }finally{
|
|
|
91
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
92
|
+ }
|
72
|
}
|
93
|
}
|
73
|
-
|
94
|
+
|
74
|
/**
|
95
|
/**
|
75
|
* 根据多个索引查询
|
96
|
* 根据多个索引查询
|
76
|
*
|
97
|
*
|
|
@@ -79,10 +100,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -79,10 +100,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
79
|
* @return
|
100
|
* @return
|
80
|
*/
|
101
|
*/
|
81
|
public SearchResult doSearch(final List<String> indexNames, final SearchParam searchParam) {
|
102
|
public SearchResult doSearch(final List<String> indexNames, final SearchParam searchParam) {
|
82
|
- IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
|
|
|
83
|
- SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
|
|
|
84
|
- this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
|
|
|
85
|
- return searchResult;
|
103
|
+ long begin = System.currentTimeMillis();
|
|
|
104
|
+ performanceMonitor.addVisitCount();
|
|
|
105
|
+ try {
|
|
|
106
|
+ IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
|
|
|
107
|
+ SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
|
|
|
108
|
+ this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
|
|
|
109
|
+ return searchResult;
|
|
|
110
|
+ } catch (Exception e) {
|
|
|
111
|
+ throw e;
|
|
|
112
|
+ }finally{
|
|
|
113
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
114
|
+ }
|
86
|
}
|
115
|
}
|
87
|
|
116
|
|
88
|
/**
|
117
|
/**
|
|
@@ -93,17 +122,25 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -93,17 +122,25 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
93
|
* @return
|
122
|
* @return
|
94
|
*/
|
123
|
*/
|
95
|
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
|
124
|
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
|
96
|
- if (searchParams == null || searchParams.isEmpty()) {
|
|
|
97
|
- return new ArrayList<SearchResult>();
|
|
|
98
|
- }
|
|
|
99
|
- IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
100
|
- List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
|
|
|
101
|
- for (int i = 0; i < searchParams.size(); i++) {
|
|
|
102
|
- SearchResult searchResult = results.get(i);
|
|
|
103
|
- SearchParam searchParam = searchParams.get(i);
|
|
|
104
|
- this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
125
|
+ long begin = System.currentTimeMillis();
|
|
|
126
|
+ performanceMonitor.addVisitCount(searchParams==null?0:searchParams.size());
|
|
|
127
|
+ try {
|
|
|
128
|
+ if (searchParams == null || searchParams.isEmpty()) {
|
|
|
129
|
+ return new ArrayList<SearchResult>();
|
|
|
130
|
+ }
|
|
|
131
|
+ IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
132
|
+ List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
|
|
|
133
|
+ for (int i = 0; i < searchParams.size(); i++) {
|
|
|
134
|
+ SearchResult searchResult = results.get(i);
|
|
|
135
|
+ SearchParam searchParam = searchParams.get(i);
|
|
|
136
|
+ this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
|
|
137
|
+ }
|
|
|
138
|
+ return results;
|
|
|
139
|
+ } catch (Exception e) {
|
|
|
140
|
+ throw e;
|
|
|
141
|
+ }finally{
|
|
|
142
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
105
|
}
|
143
|
}
|
106
|
- return results;
|
|
|
107
|
}
|
144
|
}
|
108
|
|
145
|
|
109
|
/**
|
146
|
/**
|
|
@@ -114,20 +151,28 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -114,20 +151,28 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
114
|
* @return
|
151
|
* @return
|
115
|
*/
|
152
|
*/
|
116
|
public Map<String, Object> doGetCommon(final String indexName, final String id) {
|
153
|
public Map<String, Object> doGetCommon(final String indexName, final String id) {
|
117
|
- if (StringUtils.isBlank(id)) {
|
|
|
118
|
- return null;
|
|
|
119
|
- }
|
|
|
120
|
- IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
121
|
- List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
122
|
- if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
123
|
- return null;
|
|
|
124
|
- }
|
|
|
125
|
- GetResponse response = client.get(realIndexNames.get(0), indexName, id);
|
|
|
126
|
- // 判断是否为空
|
|
|
127
|
- if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
|
|
|
128
|
- return null;
|
154
|
+ long begin = System.currentTimeMillis();
|
|
|
155
|
+ performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
|
|
|
156
|
+ try {
|
|
|
157
|
+ if (StringUtils.isBlank(id)) {
|
|
|
158
|
+ return null;
|
|
|
159
|
+ }
|
|
|
160
|
+ IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
161
|
+ List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
162
|
+ if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
163
|
+ return null;
|
|
|
164
|
+ }
|
|
|
165
|
+ GetResponse response = client.get(realIndexNames.get(0), indexName, id);
|
|
|
166
|
+ // 判断是否为空
|
|
|
167
|
+ if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
|
|
|
168
|
+ return null;
|
|
|
169
|
+ }
|
|
|
170
|
+ return response.getSourceAsMap();
|
|
|
171
|
+ } catch (Exception e) {
|
|
|
172
|
+ throw e;
|
|
|
173
|
+ }finally{
|
|
|
174
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
129
|
}
|
175
|
}
|
130
|
- return response.getSourceAsMap();
|
|
|
131
|
}
|
176
|
}
|
132
|
|
177
|
|
133
|
/**
|
178
|
/**
|
|
@@ -137,26 +182,34 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -137,26 +182,34 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
137
|
* @return
|
182
|
* @return
|
138
|
*/
|
183
|
*/
|
139
|
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
|
184
|
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
|
140
|
- if (idList == null || idList.isEmpty()) {
|
|
|
141
|
- return new ArrayList<Map<String, Object>>();
|
|
|
142
|
- }
|
|
|
143
|
- IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
144
|
- List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
145
|
- if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
146
|
- return new ArrayList<Map<String, Object>>();
|
|
|
147
|
- }
|
|
|
148
|
- Set<String> idSet = new HashSet<String>();
|
|
|
149
|
- for (Object id : idList) {
|
|
|
150
|
- idSet.add(id.toString());
|
|
|
151
|
- }
|
|
|
152
|
- MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, null);
|
|
|
153
|
- List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
154
|
- for (MultiGetItemResponse item : response.getResponses()) {
|
|
|
155
|
- if (item.getResponse().isExists()) {
|
|
|
156
|
- results.add(item.getResponse().getSource());
|
185
|
+ long begin = System.currentTimeMillis();
|
|
|
186
|
+ performanceMonitor.addVisitCount(idList==null||idList.isEmpty()?0:1);
|
|
|
187
|
+ try {
|
|
|
188
|
+ if (idList == null || idList.isEmpty()) {
|
|
|
189
|
+ return new ArrayList<Map<String, Object>>();
|
|
|
190
|
+ }
|
|
|
191
|
+ IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
192
|
+ List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
193
|
+ if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
194
|
+ return new ArrayList<Map<String, Object>>();
|
|
|
195
|
+ }
|
|
|
196
|
+ Set<String> idSet = new HashSet<String>();
|
|
|
197
|
+ for (Object id : idList) {
|
|
|
198
|
+ idSet.add(id.toString());
|
|
|
199
|
+ }
|
|
|
200
|
+ MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, null);
|
|
|
201
|
+ List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
202
|
+ for (MultiGetItemResponse item : response.getResponses()) {
|
|
|
203
|
+ if (item.getResponse().isExists()) {
|
|
|
204
|
+ results.add(item.getResponse().getSource());
|
|
|
205
|
+ }
|
157
|
}
|
206
|
}
|
|
|
207
|
+ return results;
|
|
|
208
|
+ } catch (Exception e) {
|
|
|
209
|
+ throw e;
|
|
|
210
|
+ }finally{
|
|
|
211
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
158
|
}
|
212
|
}
|
159
|
- return results;
|
|
|
160
|
}
|
213
|
}
|
161
|
|
214
|
|
162
|
} |
215
|
} |