|
@@ -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,12 +33,20 @@ import com.yoho.search.core.es.model.SearchResult; |
|
@@ -28,12 +33,20 @@ 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;
|
|
|
44
|
+ private PerformanceMonitor performanceMonitor;
|
|
|
45
|
+
|
|
|
46
|
+ @PostConstruct
|
|
|
47
|
+ void init(){
|
|
|
48
|
+ performanceMonitor = new PerformanceMonitor("ES_PERFORMANCE",ES_PERFORMANCE, 10);
|
|
|
49
|
+ }
|
37
|
|
50
|
|
38
|
@Override
|
51
|
@Override
|
39
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
52
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
@@ -65,10 +78,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -65,10 +78,18 @@ 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) {
|
|
|
81
|
+ long begin = System.currentTimeMillis();
|
|
|
82
|
+ performanceMonitor.addVisitCount();
|
|
|
83
|
+ try {
|
68
|
IElasticsearchClient client = esClientMgr.getClient(indexName);
|
84
|
IElasticsearchClient client = esClientMgr.getClient(indexName);
|
69
|
SearchResult searchResult = client.search(indexName, indexName, searchParam);
|
85
|
SearchResult searchResult = client.search(indexName, indexName, searchParam);
|
70
|
this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
86
|
this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
71
|
return 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
|
/**
|
|
@@ -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) {
|
|
|
103
|
+ long begin = System.currentTimeMillis();
|
|
|
104
|
+ performanceMonitor.addVisitCount();
|
|
|
105
|
+ try {
|
82
|
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
|
106
|
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
|
83
|
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
|
107
|
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
|
84
|
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
|
108
|
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
|
85
|
return 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,6 +122,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -93,6 +122,9 @@ 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) {
|
|
|
125
|
+ long begin = System.currentTimeMillis();
|
|
|
126
|
+ performanceMonitor.addVisitCount(searchParams==null?0:searchParams.size());
|
|
|
127
|
+ try {
|
96
|
if (searchParams == null || searchParams.isEmpty()) {
|
128
|
if (searchParams == null || searchParams.isEmpty()) {
|
97
|
return new ArrayList<SearchResult>();
|
129
|
return new ArrayList<SearchResult>();
|
98
|
}
|
130
|
}
|
|
@@ -104,6 +136,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -104,6 +136,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
104
|
this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
136
|
this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
105
|
}
|
137
|
}
|
106
|
return results;
|
138
|
return results;
|
|
|
139
|
+ } catch (Exception e) {
|
|
|
140
|
+ throw e;
|
|
|
141
|
+ }finally{
|
|
|
142
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
143
|
+ }
|
107
|
}
|
144
|
}
|
108
|
|
145
|
|
109
|
/**
|
146
|
/**
|
|
@@ -114,6 +151,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -114,6 +151,9 @@ 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) {
|
|
|
154
|
+ long begin = System.currentTimeMillis();
|
|
|
155
|
+ performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
|
|
|
156
|
+ try {
|
117
|
if (StringUtils.isBlank(id)) {
|
157
|
if (StringUtils.isBlank(id)) {
|
118
|
return null;
|
158
|
return null;
|
119
|
}
|
159
|
}
|
|
@@ -128,6 +168,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -128,6 +168,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
128
|
return null;
|
168
|
return null;
|
129
|
}
|
169
|
}
|
130
|
return response.getSourceAsMap();
|
170
|
return response.getSourceAsMap();
|
|
|
171
|
+ } catch (Exception e) {
|
|
|
172
|
+ throw e;
|
|
|
173
|
+ }finally{
|
|
|
174
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
175
|
+ }
|
131
|
}
|
176
|
}
|
132
|
|
177
|
|
133
|
/**
|
178
|
/**
|
|
@@ -137,6 +182,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -137,6 +182,9 @@ 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 {
|
|
|
185
|
+ long begin = System.currentTimeMillis();
|
|
|
186
|
+ performanceMonitor.addVisitCount(idList==null||idList.isEmpty()?0:1);
|
|
|
187
|
+ try {
|
140
|
if (idList == null || idList.isEmpty()) {
|
188
|
if (idList == null || idList.isEmpty()) {
|
141
|
return new ArrayList<Map<String, Object>>();
|
189
|
return new ArrayList<Map<String, Object>>();
|
142
|
}
|
190
|
}
|
|
@@ -157,6 +205,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
@@ -157,6 +205,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
157
|
}
|
205
|
}
|
158
|
}
|
206
|
}
|
159
|
return results;
|
207
|
return results;
|
|
|
208
|
+ } catch (Exception e) {
|
|
|
209
|
+ throw e;
|
|
|
210
|
+ }finally{
|
|
|
211
|
+ performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
212
|
+ }
|
160
|
}
|
213
|
}
|
161
|
|
214
|
|
162
|
} |
215
|
} |