...
|
...
|
@@ -8,17 +8,22 @@ import java.util.List; |
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
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 com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.monitor.PerformanceMonitor;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.core.es.IElasticsearchClient;
|
|
|
import com.yoho.search.core.es.impl.YohoIndexHelper;
|
...
|
...
|
@@ -28,12 +33,20 @@ import com.yoho.search.core.es.model.SearchResult; |
|
|
@Service
|
|
|
public class SearchCommonService implements ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger ES_PERFORMANCE = LoggerFactory.getLogger("ES_PERFORMANCE");
|
|
|
|
|
|
@Autowired
|
|
|
private ESClientMgr esClientMgr;
|
|
|
@Autowired
|
|
|
private YohoIndexHelper yohoIndexHelper;
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
private PerformanceMonitor performanceMonitor;
|
|
|
|
|
|
@PostConstruct
|
|
|
void init(){
|
|
|
performanceMonitor = new PerformanceMonitor("ES_PERFORMANCE",ES_PERFORMANCE, 10);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
...
|
...
|
@@ -65,10 +78,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -79,10 +100,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @return
|
|
|
*/
|
|
|
public SearchResult doSearch(final List<String> indexNames, final SearchParam searchParam) {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
performanceMonitor.addVisitCount();
|
|
|
try {
|
|
|
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
|
|
|
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
|
|
|
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
|
|
|
return searchResult;
|
|
|
} catch (Exception e) {
|
|
|
throw e;
|
|
|
}finally{
|
|
|
performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -93,6 +122,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @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>();
|
|
|
}
|
...
|
...
|
@@ -104,6 +136,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
|
|
}
|
|
|
return results;
|
|
|
} catch (Exception e) {
|
|
|
throw e;
|
|
|
}finally{
|
|
|
performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -114,6 +151,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @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;
|
|
|
}
|
...
|
...
|
@@ -128,6 +168,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
return null;
|
|
|
}
|
|
|
return response.getSourceAsMap();
|
|
|
} catch (Exception e) {
|
|
|
throw e;
|
|
|
}finally{
|
|
|
performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -137,6 +182,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @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>>();
|
|
|
}
|
...
|
...
|
@@ -157,6 +205,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
}
|
|
|
}
|
|
|
return results;
|
|
|
} catch (Exception e) {
|
|
|
throw e;
|
|
|
}finally{
|
|
|
performanceMonitor.addCost(System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|