@@ -1,5 +1,6 @@
package com.macro.mall.search.service.impl ;
import cn.hutool.core.util.StrUtil ;
import com.macro.mall.search.dao.EsProductDao ;
import com.macro.mall.search.domain.EsProduct ;
import com.macro.mall.search.domain.EsProductRelatedInfo ;
@@ -13,6 +14,7 @@ import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder ;
import org.elasticsearch.search.aggregations.Aggregation ;
import org.elasticsearch.search.aggregations.AggregationBuilders ;
import org.elasticsearch.search.aggregations.Aggregations ;
import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter ;
import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested ;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms ;
@@ -36,10 +38,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils ;
import org.springframework.util.StringUtils ;
import java.util.ArrayList ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Map ;
import java.util.* ;
import java.util.stream.Collectors ;
@@ -142,21 +141,21 @@ public class EsProductServiceImpl implements EsProductService {
//排序
if ( sort = = 1 ) {
//按新品从新到旧
nativeSearchQueryBuilder . withSort ( SortBuilders . fieldSort ( " id " ) . order ( SortOrder . DESC ) ) ;
nativeSearchQueryBuilder . withSorts ( SortBuilders . fieldSort ( " id " ) . order ( SortOrder . DESC ) ) ;
} else if ( sort = = 2 ) {
//按销量从高到低
nativeSearchQueryBuilder . withSort ( SortBuilders . fieldSort ( " sale " ) . order ( SortOrder . DESC ) ) ;
nativeSearchQueryBuilder . withSorts ( SortBuilders . fieldSort ( " sale " ) . order ( SortOrder . DESC ) ) ;
} else if ( sort = = 3 ) {
//按价格从低到高
nativeSearchQueryBuilder . withSort ( SortBuilders . fieldSort ( " price " ) . order ( SortOrder . ASC ) ) ;
nativeSearchQueryBuilder . withSorts ( SortBuilders . fieldSort ( " price " ) . order ( SortOrder . ASC ) ) ;
} else if ( sort = = 4 ) {
//按价格从高到低
nativeSearchQueryBuilder . withSort ( SortBuilders . fieldSort ( " price " ) . order ( SortOrder . DESC ) ) ;
nativeSearchQueryBuilder . withSorts ( SortBuilders . fieldSort ( " price " ) . order ( SortOrder . DESC ) ) ;
} else {
//按相关度
nativeSearchQueryBuilder . withSort ( SortBuilders . scoreSort ( ) . order ( SortOrder . DESC ) ) ;
nativeSearchQueryBuilder . withSorts ( SortBuilders . scoreSort ( ) . order ( SortOrder . DESC ) ) ;
}
nativeSearchQueryBuilder . withSort ( SortBuilders . scoreSort ( ) . order ( SortOrder . DESC ) ) ;
nativeSearchQueryBuilder . withSorts ( SortBuilders . scoreSort ( ) . order ( SortOrder . DESC ) ) ;
NativeSearchQuery searchQuery = nativeSearchQueryBuilder . build ( ) ;
LOGGER . info ( " DSL:{} " , searchQuery . getQuery ( ) . toString ( ) ) ;
SearchHits < EsProduct > searchHits = elasticsearchRestTemplate . search ( searchQuery , EsProduct . class ) ;
@@ -217,15 +216,15 @@ public class EsProductServiceImpl implements EsProductService {
public EsProductRelatedInfo searchRelatedInfo ( String keyword ) {
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder ( ) ;
//搜索条件
if ( String Utils . isEmpty ( keyword ) ) {
if ( StrUtil . isEmpty ( keyword ) ) {
builder . withQuery ( QueryBuilders . matchAllQuery ( ) ) ;
} else {
builder . withQuery ( QueryBuilders . multiMatchQuery ( keyword , " name " , " subTitle " , " keywords " ) ) ;
}
//聚合搜索品牌名称
builder . add Aggregation( AggregationBuilders . terms ( " brandNames " ) . field ( " brandName " ) ) ;
builder . with Aggregations ( AggregationBuilders . terms ( " brandNames " ) . field ( " brandName " ) ) ;
//集合搜索分类名称
builder . add Aggregation( AggregationBuilders . terms ( " productCategoryNames " ) . field ( " productCategoryName " ) ) ;
builder . with Aggregations ( AggregationBuilders . terms ( " productCategoryNames " ) . field ( " productCategoryName " ) ) ;
//聚合搜索商品属性, 去除type=1的属性
AbstractAggregationBuilder aggregationBuilder = AggregationBuilders . nested ( " allAttrValues " , " attrValueList " )
. subAggregation ( AggregationBuilders . filter ( " productAttrs " , QueryBuilders . termQuery ( " attrValueList.type " , 1 ) )
@@ -235,7 +234,7 @@ public class EsProductServiceImpl implements EsProductService {
. field ( " attrValueList.value " ) )
. subAggregation ( AggregationBuilders . terms ( " attrNames " )
. field ( " attrValueList.name " ) ) ) ) ;
builder . add Aggregation( aggregationBuilder ) ;
builder . with Aggregations ( aggregationBuilder ) ;
NativeSearchQuery searchQuery = builder . build ( ) ;
SearchHits < EsProduct > searchHits = elasticsearchRestTemplate . search ( searchQuery , EsProduct . class ) ;
return convertProductRelatedInfo ( searchHits ) ;
@@ -246,7 +245,7 @@ public class EsProductServiceImpl implements EsProductService {
*/
private EsProductRelatedInfo convertProductRelatedInfo ( SearchHits < EsProduct > response ) {
EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo ( ) ;
Map < String , Aggregation > aggregationMap = response . getAggregations ( ) . getA sMap( ) ;
Map < String , Aggregation > aggregationMap = ( ( Aggregations ) response . getAggregations ( ) . aggregations ( ) ) . a sMap( ) ;
//设置品牌
Aggregation brandNames = aggregationMap . get ( " brandNames " ) ;
List < String > brandNameList = new ArrayList < > ( ) ;