...
|
...
|
@@ -448,49 +448,7 @@ public class ElasticsearchClientImpl implements IElasticsearchClient { |
|
|
return search(indexNameList, type, searchParam);
|
|
|
}
|
|
|
|
|
|
private SearchResult getSearchResultFromSearchResponse(SearchParam searchParam, SearchResponse response) {
|
|
|
// 构造返回结果
|
|
|
SearchResult searchResult = new SearchResult();
|
|
|
SearchHits hits = response.getHits();
|
|
|
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
Map<String, Object> map;
|
|
|
if (hit.getSource() != null) {
|
|
|
map = hit.getSource();
|
|
|
} else {
|
|
|
map = new HashMap<String, Object>();
|
|
|
}
|
|
|
if (hit.getFields() != null && hit.getFields().size() > 0) {
|
|
|
// 返回指定字段
|
|
|
for (SearchHitField shf : hit.fields().values()) {
|
|
|
map.put(shf.name(), shf.value());
|
|
|
}
|
|
|
}
|
|
|
if (searchParam.isHighlight()) {
|
|
|
// 添加高亮字段信息
|
|
|
Map<String, Object> highlightMap = new HashMap<String, Object>();
|
|
|
// highlightMap.putAll(map);
|
|
|
for (HighlightField hf : hit.highlightFields().values()) {
|
|
|
highlightMap.put(hf.name(), hf.fragments()[0].toString());
|
|
|
}
|
|
|
map.put("_highlight", highlightMap);
|
|
|
}
|
|
|
resultList.add(map);
|
|
|
}
|
|
|
searchResult.setResultList(resultList);
|
|
|
searchResult.setTotal(hits.getTotalHits());
|
|
|
searchResult.setPage(searchParam.getPage());
|
|
|
if (searchResult.getTotal() % searchParam.getSize() == 0) {
|
|
|
searchResult.setTotalPage(searchResult.getTotal() / searchParam.getSize());
|
|
|
} else {
|
|
|
searchResult.setTotalPage(searchResult.getTotal() / searchParam.getSize() + 1);
|
|
|
}
|
|
|
if (response.getAggregations() != null) {
|
|
|
Map<String, Aggregation> aggregations = response.getAggregations().asMap();
|
|
|
searchResult.setAggMaps(aggregations);
|
|
|
}
|
|
|
return searchResult;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public SearchResult search(final List<String> aliasIndexNameList, final String type, final SearchParam searchParam) {
|
...
|
...
|
@@ -562,4 +520,54 @@ public class ElasticsearchClientImpl implements IElasticsearchClient { |
|
|
logger.debug("[model=EsIndexClient][method=search][aliasIndexNameList={}][cost={}ms]", aliasIndexNameList, System.currentTimeMillis() - begin);
|
|
|
return searchResults;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 拼装返回数据
|
|
|
* @param searchParam
|
|
|
* @param response
|
|
|
* @return
|
|
|
*/
|
|
|
private SearchResult getSearchResultFromSearchResponse(SearchParam searchParam, SearchResponse response) {
|
|
|
// 构造返回结果
|
|
|
SearchResult searchResult = new SearchResult();
|
|
|
SearchHits hits = response.getHits();
|
|
|
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
Map<String, Object> map;
|
|
|
if (hit.getSource() != null) {
|
|
|
map = hit.getSource();
|
|
|
} else {
|
|
|
map = new HashMap<String, Object>();
|
|
|
}
|
|
|
if (hit.getFields() != null && hit.getFields().size() > 0) {
|
|
|
// 返回指定字段
|
|
|
for (SearchHitField shf : hit.fields().values()) {
|
|
|
map.put(shf.name(), shf.value());
|
|
|
}
|
|
|
}
|
|
|
if (searchParam.isHighlight()) {
|
|
|
// 添加高亮字段信息
|
|
|
Map<String, Object> highlightMap = new HashMap<String, Object>();
|
|
|
// highlightMap.putAll(map);
|
|
|
for (HighlightField hf : hit.highlightFields().values()) {
|
|
|
highlightMap.put(hf.name(), hf.fragments()[0].toString());
|
|
|
}
|
|
|
map.put("_highlight", highlightMap);
|
|
|
}
|
|
|
resultList.add(map);
|
|
|
}
|
|
|
searchResult.setResultList(resultList);
|
|
|
searchResult.setTotal(hits.getTotalHits());
|
|
|
searchResult.setPage(searchParam.getPage());
|
|
|
if (searchResult.getTotal() % searchParam.getSize() == 0) {
|
|
|
searchResult.setTotalPage(searchResult.getTotal() / searchParam.getSize());
|
|
|
} else {
|
|
|
searchResult.setTotalPage(searchResult.getTotal() / searchParam.getSize() + 1);
|
|
|
}
|
|
|
if (response.getAggregations() != null) {
|
|
|
Map<String, Aggregation> aggregations = response.getAggregations().asMap();
|
|
|
searchResult.setAggMaps(aggregations);
|
|
|
}
|
|
|
return searchResult;
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|