本文主要是介绍181、商城业务-检索服务-SearchRequest构建-聚合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 准备检索请求* 模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存),排序,分页,高亮,聚合分析* @return*/private SearchRequest buildSearchRequest(SearchParam param) {SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //构建 DSL 语句的/*** 模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存)*///1. 构建bool-queryBoolQueryBuilder boolQuery = new BoolQueryBuilder();//1.1 bool-must 模糊匹配if(!StringUtils.isEmpty(param.getKeyword())){boolQuery.must(QueryBuilders.matchQuery("skuTitle",param.getKeyword()));}//1.2 bool-fiter//1.2.1 catelogId -- 按照三级分类idif(null != param.getCatalog3Id()){boolQuery.filter(QueryBuilders.termQuery("catalogId",param.getCatalog3Id()));}//1.2.2 brandId 品牌if(null != param.getBrandId() && param.getBrandId().size() >0){boolQuery.filter(QueryBuilders.termsQuery("brandId",param.getBrandId()));}//1.2.3 attrs 属性if(param.getAttrs() != null && param.getAttrs().size() > 0){for(String attrStr : param.getAttrs()) {//attrs=1_5寸:8寸&2_16G:8GBoolQueryBuilder nestedboolQuery = QueryBuilders.boolQuery();//attrs=1_5寸:8寸String[] s = attrStr.split("_");String attrId=s[0];String[] attrValues = s[1].split(":");//这个属性检索用的值nestedboolQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId));nestedboolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues));NestedQueryBuilder nestedQuery = QueryBuilders.nestedQuery("attrs",nestedboolQuery, ScoreMode.None);boolQuery.filter(nestedQuery);}// param.getAttrs().forEach(item -> {
// //attrs=1_5寸:8寸&2_16G:8G
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//
//
// //attrs=1_5寸:8寸
// String[] s = item.split("_");
// String attrId=s[0];
// String[] attrValues = s[1].split(":");//这个属性检索用的值
// boolQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId));
// boolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues));
//
// NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("attrs",boolQuery, ScoreMode.None);
// queryBuilder.filter(nestedQueryBuilder);
// });}//1.2.4 hasStock 是否有库存if(null != param.getHasStock()){boolQuery.filter(QueryBuilders.termQuery("hasStock",param.getHasStock() == 1));}//1.2.5 skuPrice 价格区间if(!StringUtils.isEmpty(param.getSkuPrice())){//skuPrice形式为:1_500或_500或500_RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("skuPrice");String[] s = param.getSkuPrice().split("_");if(s.length==2){rangeQuery.gte(s[0]).lte(s[1]);}else if(s.length == 1){if(param.getSkuPrice().startsWith("_")){rangeQuery.lte(s[1]);}if(param.getSkuPrice().endsWith("_")){rangeQuery.gte(s[0]);}}boolQuery.filter(rangeQuery);}//封装所有的查询条件sourceBuilder.query(boolQuery);/*** 排序,分页,高亮*///排序//形式为sort=hotScore_asc/descif(!StringUtils.isEmpty(param.getSort())){String sort = param.getSort();String[] s = sort.split("_");SortOrder sortOrder="asc".equalsIgnoreCase(s[1])?SortOrder.ASC:SortOrder.DESC;sourceBuilder.sort(s[0],sortOrder);}//分页sourceBuilder.from((param.getPageNum()-1)* EsConstant.PRODUCT_PAGESIZE);sourceBuilder.size(EsConstant.PRODUCT_PAGESIZE);//高亮if(!StringUtils.isEmpty(param.getKeyword())){HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("skuTitle");highlightBuilder.preTags("<b style='color:red'>");highlightBuilder.postTags("</b>");sourceBuilder.highlighter(highlightBuilder);}/*** 聚合分析*///1. 按照品牌进行聚合TermsAggregationBuilder brand_agg = AggregationBuilders.terms("brand_agg");brand_agg.field("brandId").size(50);//1.1 品牌的子聚合-品牌名聚合brand_agg.subAggregation(AggregationBuilders.terms("brand_Name_agg").field("brandName").size(1));//1.2 品牌的子聚合-品牌图片聚合brand_agg.subAggregation(AggregationBuilders.terms("brand_img_agg").field("brandImg").size(1));// TODO 1、聚合 brandsourceBuilder.aggregation(brand_agg);//2. 按照分类信息进行聚合TermsAggregationBuilder catalog_agg = AggregationBuilders.terms("catalog_agg");catalog_agg.field("catalogId").size(20);catalog_agg.subAggregation(AggregationBuilders.terms("catalog_name_agg").field("catalogName").size(1));// TODO 2、聚合 catalogsourceBuilder.aggregation(catalog_agg);//2. 按照属性信息进行聚合NestedAggregationBuilder attr_agg = AggregationBuilders.nested("attr_agg", "attrs");//2.1 按照属性ID进行聚合TermsAggregationBuilder attr_id_agg = AggregationBuilders.terms("attr_id_agg").field("attrs.attrId");//2.1.1 在每个属性ID下,按照属性名进行聚合attr_id_agg.subAggregation(AggregationBuilders.terms("attr_name_agg").field("attrs.attrName").size(1));//2.1.1 在每个属性ID下,按照属性值进行聚合attr_id_agg.subAggregation(AggregationBuilders.terms("attr_value_agg").field("attrs.attrValue").size(50));attr_agg.subAggregation(attr_id_agg);// TODO 2、聚合 attrsourceBuilder.aggregation(attr_agg);// log.debug("构建的DSL语句 {}",searchSourceBuilder.toString());String s = sourceBuilder.toString();System.out.println("构建的DSL======" + s);SearchRequest searchRequest = new SearchRequest(new String[]{EsConstant.PRODUCT_INDEX}, sourceBuilder);return searchRequest;}
127.0.0.1:15000/list.html?keyword=华为&catalog3Id=225&attrs=9_海思:A13&attrs=10_HUAWEI Kirin 980&skuPrice=6000_
GET gulimall_product/_search
{"from": 0,"size": 2,"query": {"bool": {"must": [{"match": {"skuTitle": {"query": "华为","operator": "OR","prefix_length": 0,"max_expansions": 50,"fuzzy_transpositions": true,"lenient": false,"zero_terms_query": "NONE","auto_generate_synonyms_phrase_query": true,"boost": 1}}}],"filter": [{"term": {"catalogId": {"value": 225,"boost": 1}}},{"nested": {"query": {"bool": {"must": [{"term": {"attrs.attrId": {"value": "9","boost": 1}}},{"terms": {"attrs.attrValue": ["海思","A13"],"boost": 1}}],"adjust_pure_negative": true,"boost": 1}},"path": "attrs","ignore_unmapped": false,"score_mode": "none","boost": 1}},{"nested": {"query": {"bool": {"must": [{"term": {"attrs.attrId": {"value": "10","boost": 1}}},{"terms": {"attrs.attrValue": ["HUAWEI Kirin 980"],"boost": 1}}],"adjust_pure_negative": true,"boost": 1}},"path": "attrs","ignore_unmapped": false,"score_mode": "none","boost": 1}},{"term": {"hasStock": {"value": true,"boost": 1}}},{"range": {"skuPrice": {"from": "6000","to": null,"include_lower": true,"include_upper": true,"boost": 1}}}],"adjust_pure_negative": true,"boost": 1}},"aggregations": {"brand_agg": {"terms": {"field": "brandId","size": 50,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]},"aggregations": {"brand_Name_agg": {"terms": {"field": "brandName","size": 1,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]}},"brand_img_agg": {"terms": {"field": "brandImg","size": 1,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]}}}},"catalog_agg": {"terms": {"field": "catalogId","size": 20,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]},"aggregations": {"catalog_name_agg": {"terms": {"field": "catalogName","size": 1,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]}}}},"attr_agg": {"nested": {"path": "attrs"},"aggregations": {"attr_id_agg": {"terms": {"field": "attrs.attrId","size": 10,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]},"aggregations": {"attr_name_agg": {"terms": {"field": "attrs.attrName","size": 1,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]}},"attr_value_agg": {"terms": {"field": "attrs.attrValue","size": 50,"min_doc_count": 1,"shard_min_doc_count": 0,"show_term_doc_count_error": false,"order": [{"_count": "desc"},{"_key": "asc"}]}}}}}}},"highlight": {"pre_tags": ["<b style='color:red'>"],"post_tags": ["</b>"],"fields": {"skuTitle": {}}}
}
这篇关于181、商城业务-检索服务-SearchRequest构建-聚合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!