Authored by wangnan9279

ufo

... ... @@ -10,6 +10,7 @@ import com.yoho.search.core.es.utils.SearchFieldUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
... ... @@ -42,6 +43,10 @@ public class UfoSearchQueryHelper extends BaseService {
//价格
orderValues.add("price:asc");
orderValues.add("price:desc");
//价格
orderValues.add("pools.id:asc");
orderValues.add("pools.id:desc");
}
public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
... ... @@ -71,6 +76,16 @@ public class UfoSearchQueryHelper extends BaseService {
return boolFilter;
}
public BoolQueryBuilder getPoolIdTermsBuilder(Map<String, String> paramMap) {
// 增加商品池相关的过滤条件
if (paramMap.containsKey("pool") && StringUtils.isNotBlank("pool")) {
BoolQueryBuilder nestedBoolFilter = QueryBuilders.boolQuery();
nestedBoolFilter.must(QueryBuilders.termQuery("pools.pool_id", paramMap.get("pool")));
return nestedBoolFilter;
}
return null;
}
public QueryBuilder constructQueryBuilder(Map<String, String> paramMap) {
// 0、处理查询关键字
String query = SearchKeyWordUtils.getUfoParamKeyword(paramMap, SearchRequestParams.UFOPRODUCTINDEX_SEARCH_QUERY);
... ... @@ -123,7 +138,7 @@ public class UfoSearchQueryHelper extends BaseService {
String[] sortParts = sortType.split(ISearchConstants.SPLIT_CHAR_COLON);
String fieldName = sortParts[0];
SortOrder sortOrder = SortOrder.ASC.toString().equals(sortParts[1]) ? SortOrder.ASC : SortOrder.DESC;
this.addSortBuildSorts(sortBuilders, filteredFieldNames, fieldName, sortOrder);
this.addSortBuildSorts(sortBuilders, filteredFieldNames, paramMap, fieldName, sortOrder);
}
return sortBuilders;
... ... @@ -159,13 +174,21 @@ public class UfoSearchQueryHelper extends BaseService {
return realSortField + ":" + sortParts[1];
}
private void addSortBuildSorts(List<SortBuilder<?>> sortBuilders, List<String> filteredFieldNames, String fieldName, SortOrder sortOrder) {
private void addSortBuildSorts(List<SortBuilder<?>> sortBuilders, List<String> filteredFieldNames, Map<String, String> paramMap, String fieldName, SortOrder sortOrder) {
if (filteredFieldNames.contains(fieldName)) {
return;
}
filteredFieldNames.add(fieldName);
if (fieldName.equalsIgnoreCase("_score")) {
sortBuilders.add(SortBuilders.scoreSort().order(sortOrder));
} else if (fieldName.contains("pools")) {
BoolQueryBuilder poolsTermsBuilder = this.getPoolIdTermsBuilder(paramMap);
String flag = sortOrder.toString().equals("desc") ? "_last" : "_first";
FieldSortBuilder poolsFieldSortBuilder = SortBuilders.fieldSort(fieldName).order(sortOrder).setNestedPath("pools").missing(flag);
if (poolsFieldSortBuilder != null) {
poolsFieldSortBuilder.setNestedFilter(poolsTermsBuilder);
}
sortBuilders.add(poolsFieldSortBuilder);
} else {
sortBuilders.add(SortBuilders.fieldSort(fieldName).order(sortOrder));
}
... ...