...
|
...
|
@@ -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,13 +33,21 @@ 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) {
|
|
|
this.publisher = applicationEventPublisher;
|
...
|
...
|
@@ -65,12 +78,20 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @return
|
|
|
*/
|
|
|
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
|
|
|
IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
SearchResult searchResult = client.search(indexName, indexName, searchParam);
|
|
|
this.publishSearchResultEvent(indexName, searchParam, searchResult);
|
|
|
return searchResult;
|
|
|
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) {
|
|
|
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
|
|
|
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
|
|
|
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
|
|
|
return searchResult;
|
|
|
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,17 +122,25 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @return
|
|
|
*/
|
|
|
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
|
|
|
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);
|
|
|
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);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -114,20 +151,28 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @return
|
|
|
*/
|
|
|
public Map<String, Object> doGetCommon(final String indexName, final String id) {
|
|
|
if (StringUtils.isBlank(id)) {
|
|
|
return null;
|
|
|
}
|
|
|
IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return null;
|
|
|
}
|
|
|
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
|
|
|
// 判断是否为空
|
|
|
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
|
|
|
return null;
|
|
|
long begin = System.currentTimeMillis();
|
|
|
performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
|
|
|
try {
|
|
|
if (StringUtils.isBlank(id)) {
|
|
|
return null;
|
|
|
}
|
|
|
IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return null;
|
|
|
}
|
|
|
GetResponse response = client.get(realIndexNames.get(0), 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);
|
|
|
}
|
|
|
return response.getSourceAsMap();
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -137,26 +182,34 @@ public class SearchCommonService implements ApplicationEventPublisherAware { |
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
|
|
|
if (idList == null || idList.isEmpty()) {
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
}
|
|
|
IElasticsearchClient client = esClientMgr.getClient(indexName);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
}
|
|
|
Set<String> idSet = new HashSet<String>();
|
|
|
for (Object id : idList) {
|
|
|
idSet.add(id.toString());
|
|
|
}
|
|
|
MultiGetResponse response = client.multiGet(realIndexNames.get(0), 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());
|
|
|
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);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
}
|
|
|
Set<String> idSet = new HashSet<String>();
|
|
|
for (Object id : idList) {
|
|
|
idSet.add(id.toString());
|
|
|
}
|
|
|
MultiGetResponse response = client.multiGet(realIndexNames.get(0), 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);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|