diff --git a/search_service/src/main/java/com/hp/search_service/service/GoodsSearchService.java b/search_service/src/main/java/com/hp/search_service/service/GoodsSearchService.java index eb8d0e9eb213ebb042d06a7c9333406a8317a8c0..7bcfab797d0748057404154472063bf78f50afb8 100644 --- a/search_service/src/main/java/com/hp/search_service/service/GoodsSearchService.java +++ b/search_service/src/main/java/com/hp/search_service/service/GoodsSearchService.java @@ -10,7 +10,6 @@ import com.hp.search_service.entity.SearchPageResult; import com.hp.search_service.feign.GoodsFeignClient; import com.hp.search_service.repository.GoodsRepository; import org.apache.commons.lang.StringUtils; -import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilders; @@ -55,11 +54,10 @@ public class GoodsSearchService { } //创建本地查询生成器 NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); -// //在all字段中查询关键字 设置And操作符,避免分词查询时,查询无关数据 -// MatchQueryBuilder query = QueryBuilders.matchQuery("all", search.getKeyword()).operator(Operator.AND); - BoolQueryBuilder queryByFilters = createQueryByFilters(search.getKeyword(), search.getSpecs()); + //在all字段中查询关键字 设置And操作符,避免分词查询时,查询无关数据 + MatchQueryBuilder query = QueryBuilders.matchQuery("all", search.getKeyword()).operator(Operator.AND); //设置查询 - builder.withQuery(queryByFilters); + builder.withQuery(query); //设置分页 builder.withPageable(PageRequest.of(search.getCurrent() - 1,search.getPageSize())); //设置排序字段 @@ -95,7 +93,7 @@ public class GoodsSearchService { //转换为long类型的聚合结果 LongTerms longTerms = (LongTerms) aggregation; //遍历每个聚合的桶 - for(Terms.Bucket bucket : longTerms.getBuckets()){ + for(Terms.Bucket b7ucket : longTerms.getBuckets()){ //获得分类的id long cid = bucket.getKeyAsNumber().longValue(); //调用商品服务查分类 @@ -141,37 +139,4 @@ public class GoodsSearchService { }); return specs; } - - /** - * 通过关键字和过滤条件创建BoolQuery - * @param keyword - * @param filters - * @return - */ - public BoolQueryBuilder createQueryByFilters(String keyword, Map filters){ - //创建布尔查询 - BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); - //进行关键字的全文检索,使用must - MatchQueryBuilder keywordQuery = QueryBuilders.matchQuery("all", keyword).operator(Operator.AND); - boolQueryBuilder.must(keywordQuery); - //创建filter布尔查询 - BoolQueryBuilder filterQuery = QueryBuilders.boolQuery(); - //遍历filters - for(Map.Entry entry : filters.entrySet()){ - String key = ""; - //品牌和分类查询的key需要特殊处理 - if("品牌".equals(entry.getKey())){ - key = "brandId"; - }else if("分类".equals(entry.getKey())){ - key = "cid3"; - }else{ - //其它的属性 - key = "specs." + entry.getKey() + ".keyword"; - } - filterQuery.must(QueryBuilders.termQuery(key,entry.getValue())); - } - //设置filter - boolQueryBuilder.filter(filterQuery); - return boolQueryBuilder; - } } diff --git a/shoppingmall/shoppingmall-web/search.html b/shoppingmall/shoppingmall-web/search.html index c53a152e8c8a0696cf78b18a07b6c57b32dc214b..09a2fb52b0ad1dd7a03f251eabc2dd62ebbd22af 100644 --- a/shoppingmall/shoppingmall-web/search.html +++ b/shoppingmall/shoppingmall-web/search.html @@ -1,4 +1,4 @@ - + @@ -83,10 +83,6 @@ width: 25px; height: 25px; } - .select-filter{ - background-color: #00b0ff; - border:1px #9d261d solid; - } @@ -130,8 +126,7 @@
{{filter.key}}
@@ -142,10 +137,9 @@
{{filter.key}}
@@ -340,8 +334,7 @@ search:{ //搜索参数 keyword:"", //关键字 current:1, - pageSize:20, - specs:{} //用户选择的过滤条件 + pageSize:20 }, filters:[], // 显示过滤选项的列表 {key:xx,options:[xxx,xxx]} total:0, @@ -366,25 +359,8 @@ goods.selected = sku; console.log(goods); }, - //点击过滤条件 - selectFilter(k,v){ - //分类和品牌传递的是id - if(k == "分类" || k == "品牌"){ - //如果之前的值就是当前赋值的值,就将数据设置为空(取消选中) - this.search.specs[k] = this.search.specs[k] == v.id ? undefined : v.id; - }else{ - this.search.specs[k] = this.search.specs[k] == v.name ? undefined : v.name; - } - console.log(JSON.stringify(this.search.specs)); - //再次进行搜索 - this.searchGoods(this.search.current,true); - }, - //给筛选值设置选中样式 - setSelected(k,v){ - return this.search.specs[k] == v ? 'select-filter' : ''; - }, - //搜索商品 current当前页 selectFilter 是否选择过滤条件 - searchGoods(current,selectFilter){ + //搜索商品 + searchGoods(current){ this.search.current = current; axios.post("http://api.eshop.com/search",this.search) .then(value => { @@ -398,23 +374,19 @@ //再绑定商品列表 this.goodsList = value.data.records; this.total = value.data.total; - //不是点击过滤条件,就加载过滤条件 - if(!selectFilter){ - this.filters = []; - //处理过滤的规格参数 - this.filters.push({key:"分类",options: value.data.categories}); - this.filters.push({key:"品牌",options: value.data.brands}); - //处理搜索规格 options:[{name:xxx},{..}] - value.data.specs.forEach(spec => { - let options = []; - //[xxxx,xxxx] => [{name:xxx},{..}] - spec.options.forEach(val => { - options.push({name:val}); - }); - spec.options = options; - this.filters.push(spec); + //处理过滤的规格参数 + this.filters.push({key:"分类",options: value.data.categories}); + this.filters.push({key:"品牌",options: value.data.brands}); + //处理搜索规格 options:[{name:xxx},{..}] + value.data.specs.forEach(spec => { + let options = []; + //[xxxx,xxxx] => [{name:xxx},{..}] + spec.options.forEach(val => { + options.push({name:val}); }); - } + spec.options = options; + this.filters.push(spec); + }); console.log(this.goodsList); }); }, @@ -427,7 +399,7 @@ //获得地址栏中的key参数 this.search.keyword = this.getUrlParam("key"); console.log(this.search.keyword); - this.searchGoods(1,false); + this.searchGoods(1); } });