...
|
...
|
@@ -31,6 +31,28 @@ public class AggCommonHelper { |
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AggCommonHelper.class);
|
|
|
|
|
|
/**
|
|
|
* 从TopHits中获取_source
|
|
|
*
|
|
|
* @param topHits
|
|
|
* @return
|
|
|
*/
|
|
|
public static List<Map<String, Object>> getTopHitResultsWithScore(TopHits topHits) {
|
|
|
List<Map<String, Object>> topHitList = new ArrayList<>();
|
|
|
if (topHits == null) {
|
|
|
return topHitList;
|
|
|
}
|
|
|
SearchHits hits = topHits.getHits();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
Map<String, Object> source = hit.getSource();
|
|
|
float _score = hit.getScore();
|
|
|
source.put("_score", _score);
|
|
|
topHitList.add(source);
|
|
|
}
|
|
|
return topHitList;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static List<Integer> getIdsFromAggMaps(Map<String, Aggregation> aggMaps, String aggName) {
|
|
|
if (!aggMaps.containsKey(aggName)) {
|
|
|
return new ArrayList<>();
|
...
|
...
|
@@ -53,7 +75,7 @@ public class AggCommonHelper { |
|
|
*
|
|
|
* @param simpleFieldAggs
|
|
|
* @param topHitOrder
|
|
|
* @param topHitFieldCount
|
|
|
* @param topHitCount
|
|
|
* @return
|
|
|
*/
|
|
|
public static AbstractAggregationBuilder<?> getTopHitAggregation(List<SimpleFieldAgg> simpleFieldAggs, String topHitOrder, int topHitCount) {
|
...
|
...
|
@@ -85,12 +107,6 @@ public class AggCommonHelper { |
|
|
|
|
|
/**
|
|
|
* 获取聚合出来的商品列表,并按特定顺序截取
|
|
|
*
|
|
|
* @param aggregation
|
|
|
* @param viewNum
|
|
|
* @param sortField
|
|
|
* @param sortOrder
|
|
|
* @return
|
|
|
*/
|
|
|
public static List<Map<String, Object>> getTopHitList(Map<String, Aggregation> aggMaps, List<SimpleFieldAgg> simpleFieldAggs, String topHitOrder, int totalCount) {
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
...
|
...
|
@@ -99,6 +115,7 @@ public class AggCommonHelper { |
|
|
return results;
|
|
|
}
|
|
|
|
|
|
|
|
|
private static void getTopHitResults(List<Map<String, Object>> results, Map<String, Aggregation> aggMaps, List<SimpleFieldAgg> simpleFieldAggs, int index) {
|
|
|
try {
|
|
|
String aggName = simpleFieldAggs.get(index++).getAggName();
|
...
|
...
|
@@ -113,15 +130,8 @@ public class AggCommonHelper { |
|
|
Map<String, Aggregation> aggMap = bucket.getAggregations().getAsMap();
|
|
|
if (aggMap.containsKey("topHit")) {
|
|
|
TopHits topHits = bucket.getAggregations().get("topHit");
|
|
|
if (topHits != null) {
|
|
|
SearchHits hits = topHits.getHits();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
Map<String, Object> source = hit.getSource();
|
|
|
float _score = hit.getScore();
|
|
|
source.put("_score", _score);
|
|
|
results.add(source);
|
|
|
}
|
|
|
}
|
|
|
List<Map<String, Object>> topHitList = getTopHitResultsWithScore(topHits);
|
|
|
results.addAll(topHitList);
|
|
|
} else {
|
|
|
getTopHitResults(results, aggMap, simpleFieldAggs, index);
|
|
|
}
|
...
|
...
|
@@ -133,7 +143,7 @@ public class AggCommonHelper { |
|
|
|
|
|
private static List<Map<String, Object>> sortListBySortField(List<Map<String, Object>> productList, String topHitOrder, int viewNum) {
|
|
|
if (productList == null || productList.isEmpty()) {
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
// 再按照某个字段对商品排序
|
|
|
FieldSortOrder fieldSortOrder = new FieldSortOrder(topHitOrder);
|
...
|
...
|
@@ -173,38 +183,27 @@ public class AggCommonHelper { |
|
|
/**
|
|
|
* 按聚合的key获取聚合出来的商品列表[目前只支持一个field,多重field的话,key不好处理]
|
|
|
*
|
|
|
* @param aggregation
|
|
|
* @param viewNum
|
|
|
* @param sortField
|
|
|
* @param sortOrder
|
|
|
* @return
|
|
|
*/
|
|
|
public static List<KeyTopHitModel> getTopHitListGroupByKey(Map<String, Aggregation> aggMaps, SimpleFieldAgg simpleFieldAgg) {
|
|
|
String aggName = simpleFieldAgg.getAggName();
|
|
|
if (!aggMaps.containsKey(aggName)) {
|
|
|
return new ArrayList<KeyTopHitModel>();
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<? extends Bucket> bucketList = ((MultiBucketsAggregation) aggMaps.get(aggName)).getBuckets();
|
|
|
if (bucketList.isEmpty()) {
|
|
|
return new ArrayList<KeyTopHitModel>();
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<KeyTopHitModel> results = new ArrayList<KeyTopHitModel>();
|
|
|
for (Bucket bucket : bucketList) {
|
|
|
String key = bucket.getKeyAsString();
|
|
|
long count = bucket.getDocCount();
|
|
|
List<Map<String, Object>> topHitList = new ArrayList<Map<String, Object>>();
|
|
|
List<Map<String, Object>> topHitList = new ArrayList<>();
|
|
|
Map<String, Aggregation> aggMap = bucket.getAggregations().getAsMap();
|
|
|
if (aggMap.containsKey("topHit")) {
|
|
|
TopHits topHits = bucket.getAggregations().get("topHit");
|
|
|
if (topHits != null) {
|
|
|
SearchHits hits = topHits.getHits();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
Map<String, Object> source = hit.getSource();
|
|
|
float _score = hit.getScore();
|
|
|
source.put("_score", _score);
|
|
|
topHitList.add(source);
|
|
|
}
|
|
|
}
|
|
|
List<Map<String, Object>> tempTopHitList = getTopHitResultsWithScore(topHits);
|
|
|
topHitList.addAll(tempTopHitList);
|
|
|
}
|
|
|
KeyTopHitModel keyTopHitModel = new KeyTopHitModel(key, count, topHitList);
|
|
|
results.add(keyTopHitModel);
|
...
|
...
|
|