package com.alatus.search.service.impl; import com.alatus.search.config.MallElasticSearchConfig; import com.alatus.search.constant.EsConstant; import com.alatus.search.service.MallSearchService; import com.alatus.search.vo.SearchParam; import com.alatus.search.vo.SearchResult; import org.apache.commons.lang.StringUtils; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; @Service public class MallSearchServiceImpl implements MallSearchService { @Autowired private RestHighLevelClient client; @Override public SearchResult search(SearchParam searchParam) { // 动态构建出查询所需要的DSL语句 SearchResult searchResult = null; // 准备检索请求 SearchRequest searchRequest = buildSearchRequest(searchParam); try { // 执行查询检索请求 SearchResponse response = client.search(searchRequest, MallElasticSearchConfig.COMMON_OPTIONS); // 分析响应数据并封装成返回的数据 searchResult = buildSearchResult(); } catch (IOException e) { throw new RuntimeException(e); } return null; } private SearchResult buildSearchResult() { return null; } //准备检索请求 private SearchRequest buildSearchRequest(SearchParam searchParam) { // 构建DSL语句 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询条件,模糊查询,过滤,按照属性,分类品牌,价格区间,库存 // 构建了bool-query BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); // must if(!StringUtils.isEmpty(searchParam.getKeyword())){ boolQuery.must(QueryBuilders.matchQuery("skuTitle",searchParam.getKeyword())); } // filter按照三级分类ID查询 if(searchParam.getCatalog3Id()!=null){ boolQuery.filter(QueryBuilders.termQuery("catalogId",searchParam.getCatalog3Id())); } // filter按照品牌ID查询 if(searchParam.getBrandId()!=null && !searchParam.getBrandId().isEmpty()){ boolQuery.filter(QueryBuilders.termsQuery("brandId",searchParam.getBrandId())); } // filter按照属性ID查询 if(searchParam.getAttrs()!=null && !searchParam.getAttrs().isEmpty()){ for (String attrStr : searchParam.getAttrs()) { BoolQueryBuilder nestedBoolQuery = QueryBuilders.boolQuery(); String[] attr = attrStr.split("_"); String attrId = attr[0];//属性ID String[] attrValues = attr[1].split(":");//属性值 nestedBoolQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId)); nestedBoolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues)); // 每一个都必须有一个嵌入式的查询 NestedQueryBuilder attrs = QueryBuilders.nestedQuery("attrs", nestedBoolQuery, ScoreMode.None); boolQuery.filter(attrs); } } // 按照库存进行查询 boolQuery.filter(QueryBuilders.termQuery("hasStock", searchParam.getHasStock()==1)); // 按照价格区间 if(!StringUtils.isEmpty(searchParam.getSkuPrice())){ RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("skuPrice"); String[] price = searchParam.getSkuPrice().split("_"); if(price.length == 2){ rangeQuery.gte(price[0]).lte(price[1]); } else if (price.length == 1) { if(searchParam.getSkuPrice().startsWith("_")){ rangeQuery.lte(price[0]); } else if(searchParam.getSkuPrice().endsWith("_")){ rangeQuery.gte(price[0]); } } boolQuery.filter(rangeQuery); } // 把所有条件全部封装 sourceBuilder.query(boolQuery); return new SearchRequest(new String[]{EsConstant.PRODUCT_INDEX},sourceBuilder); } }
package com.alatus.search.service.impl; import com.alatus.search.config.MallElasticSearchConfig; import com.alatus.search.constant.EsConstant; import com.alatus.search.service.MallSearchService; import com.alatus.search.vo.SearchParam; import com.alatus.search.vo.SearchResult; import org.apache.commons.lang.StringUtils; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; @Service public class MallSearchServiceImpl implements MallSearchService { @Autowired private RestHighLevelClient client; @Override public SearchResult search(SearchParam searchParam) { // 动态构建出查询所需要的DSL语句 SearchResult searchResult = null; // 准备检索请求 SearchRequest searchRequest = buildSearchRequest(searchParam); try { // 执行查询检索请求 SearchResponse response = client.search(searchRequest, MallElasticSearchConfig.COMMON_OPTIONS); // 分析响应数据并封装成返回的数据 searchResult = buildSearchResult(); } catch (IOException e) { throw new RuntimeException(e); } return null; } private SearchResult buildSearchResult() { return null; } //准备检索请求 private SearchRequest buildSearchRequest(SearchParam searchParam) { // 构建DSL语句 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询条件,模糊查询,过滤,按照属性,分类品牌,价格区间,库存 // 构建了bool-query BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); // must if(!StringUtils.isEmpty(searchParam.getKeyword())){ boolQuery.must(QueryBuilders.matchQuery("skuTitle",searchParam.getKeyword())); } // filter按照三级分类ID查询 if(searchParam.getCatalog3Id()!=null){ boolQuery.filter(QueryBuilders.termQuery("catalogId",searchParam.getCatalog3Id())); } // filter按照品牌ID查询 if(searchParam.getBrandId()!=null && !searchParam.getBrandId().isEmpty()){ boolQuery.filter(QueryBuilders.termsQuery("brandId",searchParam.getBrandId())); } // filter按照属性ID查询 if(searchParam.getAttrs()!=null && !searchParam.getAttrs().isEmpty()){ for (String attrStr : searchParam.getAttrs()) { BoolQueryBuilder nestedBoolQuery = QueryBuilders.boolQuery(); String[] attr = attrStr.split("_"); String attrId = attr[0];//属性ID String[] attrValues = attr[1].split(":");//属性值 nestedBoolQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId)); nestedBoolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues)); // 每一个都必须有一个嵌入式的查询 NestedQueryBuilder attrs = QueryBuilders.nestedQuery("attrs", nestedBoolQuery, ScoreMode.None); boolQuery.filter(attrs); } } // 按照库存进行查询 boolQuery.filter(QueryBuilders.termQuery("hasStock", searchParam.getHasStock()==1)); // 按照价格区间 if(!StringUtils.isEmpty(searchParam.getSkuPrice())){ RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("skuPrice"); String[] price = searchParam.getSkuPrice().split("_"); if(price.length == 2){ rangeQuery.gte(price[0]).lte(price[1]); } else if (price.length == 1) { if(searchParam.getSkuPrice().startsWith("_")){ rangeQuery.lte(price[0]); } else if(searchParam.getSkuPrice().endsWith("_")){ rangeQuery.gte(price[0]); } } boolQuery.filter(rangeQuery); } // 把所有条件全部封装 sourceBuilder.query(boolQuery); return new SearchRequest(new String[]{EsConstant.PRODUCT_INDEX},sourceBuilder); } }
06-27
426
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)