ElasticSearch实战-JavaAPI

目录

请求构建

查询的请求

构造器

封装查询DSL语句

query语句

对全局匹配的关键字进行查询

品牌和类目的过滤

是否有库存

价格范围查找

按照属性过滤(内聚) 

排序

分页查询

高亮显示

聚合操作

完成请求构建,发送请求

页面再次分析

结果封装

获取结果

 商品信息赋值

获取查询到结果的品牌集合

获取查询到结果的类目集合

获取查询到结果的属性集合

分页操作

高亮显示


请求构建

查询的请求

SearchRequest searchRequest = new SearchRequest();

构造器

ElasticSearch实战-JavaAPI_第1张图片

封装查询DSL语句

 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

query语句

我们看到query里面,先进性bool查询

/**
* 关键字查询、根据属性、分类、品牌、价格区间、是否有库存等进行过滤、分页、高亮、以及聚合统计品牌分类属性
*/
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

对全局匹配的关键字进行查询

当时那个页面上有关于手机关键字的查询,这个是对name进行查询,还有可能其他人对其他信息进行搜索,我们需要进行判断这个keyword到底有没有传值

/**
* 页面传递过来的全文匹配关键字
*/
private String keyword;

单个字段查询,和多个字段查询 

//1、查询关键字
if (!StringUtils.isEmpty(param.getKeyword())) {
    //单字段查询
    //boolQueryBuilder.must(QueryBuilders.matchQuery("name", param.getKeyword()));
    //多字段查询                   
    boolQueryBuilder.must(QueryBuilders.multiMatchQuery(param.getKeyword(),"name","keywords","subTitle"));
}

 API

ElasticSearch实战-JavaAPI_第2张图片

ElasticSearch实战-JavaAPI_第3张图片

品牌和类目的过滤

//2、根据类目ID进行过滤
if (null != param.getCategoryId()) {
    boolQueryBuilder.filter(QueryBuilders.termQuery("categoryId", param.getCategoryId()));
}

//3、根据品牌ID进行过滤
if (null != param.getBrandId() && param.getBrandId().size() > 0) {
    boolQueryBuilder.filter(QueryBuilders.termsQuery("brandId", param.getBrandId()));
}

是否有库存

if (null != param.getHasStock()) {
    boolQueryBuilder.filter(QueryBuilders.termQuery("hasStock", param.getHasStock() == 1));
}

价格范围查找

if (!StringUtils.isEmpty(param.getPrice())) {
            //价格的输入形式为:10_100(起始价格和最终价格)或_100(不指定起始价格)或10_(不限制最终价格)
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price");
            String[] price = param.getPrice().split("_");
            if (price.length == 2) {
                    //price: _5000
                    if (param.getPrice().startsWith("_")) {
                        rangeQueryBuilder.lte(price[1]);
                    }
                else{
                        //price: 1_5000
                        rangeQueryBuilder.gte(price[0]).lte(price[1]);
                }

            } else if (price.length == 1) {
                 //price: 1_
                if (param.getPrice().endsWith("_")) {
                    rangeQueryBuilder.gte(price[0]);
                }
                //price: _5000
                if (param.getPrice().startsWith("_")) {
                    rangeQueryBuilder.lte(price[0]);
                }
            }
            boolQueryBuilder.filter(rangeQueryBuilder);
        }

按照属性过滤(内聚) 

看一下postman的请求参数,里面会携带两个参数,第一个参数代表具体查询哪个属性,第二个参数就是查询这个属性具体哪个规格 

if (param.getAttrs() != null && param.getAttrs().size() > 0) {

            param.getAttrs().forEach(item -> {
                //attrs:[1_白色,2_4核]
                BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

                //attrs=1_64G
                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);
                boolQueryBuilder.filter(nestedQueryBuilder);
            });

        }

注意:所有bool查询结束

排序

//页面传入的参数值形式 sort=price_asc/desc
        if (!StringUtils.isEmpty(param.getSort())) {
            String sort = param.getSort();
            String[] sortFileds = sort.split("_");

            if(!StringUtils.isEmpty(sortFileds[0])){
                SortOrder sortOrder = "asc".equalsIgnoreCase(sortFileds[1]) ? SortOrder.ASC : SortOrder.DESC;
                searchSourceBuilder.sort(sortFileds[0], sortOrder);
            }
        }

分页查询

//分页查询
        searchSourceBuilder.from((param.getPageNum() - 1) * SearchConstant.PAGE_SIZE);
        searchSourceBuilder.size(SearchConstant.PAGE_SIZE);

高亮显示

//高亮显示
        if (!StringUtils.isEmpty(param.getKeyword())) {

            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.field("name");
            highlightBuilder.preTags("");
            highlightBuilder.postTags("");

            searchSourceBuilder.highlighter(highlightBuilder);
        }

聚合操作

TermsAggregationBuilder category_agg = AggregationBuilders.terms("category_agg");
        category_agg.field("categoryId").size(50);

        category_agg.subAggregation(AggregationBuilders.terms("category_name_agg").field("categoryName").size(1));

        searchSourceBuilder.aggregation(category_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");
        attr_agg.subAggregation(attr_id_agg);
        //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));
        searchSourceBuilder.aggregation(attr_agg);

完成请求构建,发送请求

SearchRequest searchRequest = new SearchRequest(new String[]{SearchConstant.INDEX_NAME}, searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

页面再次分析

上面的都是聚合信息,都是通过聚合函数查询出来的结果,下面都是商品信息,再hits里面

ElasticSearch实战-JavaAPI_第4张图片

结果封装

获取结果

SearchHits hits = response.getHits();

 商品信息赋值

if (hits.getHits() != null && hits.getHits().length > 0) {
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                EsProduct esModel = JSON.parseObject(sourceAsString, EsProduct.class);

                    //2.1 判断是否按关键字检索,若是就显示高亮,否则不显示
                if (!StringUtils.isEmpty(param.getKeyword())) {
                    //2.2 拿到高亮信息显示标题
                    HighlightField name = hit.getHighlightFields().get("name");
                    //2.3 判断name中是否含有查询的关键字(因为是多字段查询,因此可能不包含指定的关键字,假设不包含则显示原始name字段的信息)
                    String nameValue = name!=null ? name.getFragments()[0].string() : esModel.getName();
                    esModel.setName(nameValue);
                }
                esModels.add(esModel);
            }
        }
        result.setProducts(esModels);

获取查询到结果的品牌集合

List brandVos = new ArrayList<>();
        //获取到品牌的聚合
        ParsedLongTerms brandAgg = response.getAggregations().get("brand_agg");
        for (Terms.Bucket bucket : brandAgg.getBuckets()) {
            ESResponseResult.BrandVo brandVo = new ESResponseResult.BrandVo();

            //获取品牌的id
            long brandId = bucket.getKeyAsNumber().longValue();
            brandVo.setBrandId(brandId);

            //获取品牌的名字
            ParsedStringTerms brandNameAgg = bucket.getAggregations().get("brand_name_agg");
            String brandName = brandNameAgg.getBuckets().get(0).getKeyAsString();
            brandVo.setBrandName(brandName);

            //获取品牌的LOGO
            ParsedStringTerms brandImgAgg = bucket.getAggregations().get("brand_img_agg");
            String brandImg = brandImgAgg.getBuckets().get(0).getKeyAsString();
            brandVo.setBrandImg(brandImg);
            System.out.println("brandId:"+brandId+"brandName:"+brandName+"brandImg");
            brandVos.add(brandVo);
        }
        result.setBrands(brandVos);

获取查询到结果的类目集合

List categoryVos = new ArrayList<>();

        ParsedLongTerms categoryAgg = response.getAggregations().get("category_agg");


        for (Terms.Bucket bucket : categoryAgg.getBuckets()) {
            ESResponseResult.categoryVo categoryVo = new ESResponseResult.categoryVo();
            //获取分类id
            String keyAsString = bucket.getKeyAsString();
            categoryVo.setCategoryId(Long.parseLong(keyAsString));

            //获取分类名
            ParsedStringTerms categoryNameAgg = bucket.getAggregations().get("category_name_agg");
            String categoryName = categoryNameAgg.getBuckets().get(0).getKeyAsString();
            categoryVo.setCategoryName(categoryName);
            categoryVos.add(categoryVo);
        }

        result.setCategorys(categoryVos);

获取查询到结果的属性集合

List attrVos = new ArrayList<>();
        //获取属性信息的聚合
        ParsedNested attrsAgg = response.getAggregations().get("attr_agg");
        ParsedLongTerms attrIdAgg = attrsAgg.getAggregations().get("attr_id_agg");
        for (Terms.Bucket bucket : attrIdAgg.getBuckets()) {
            ESResponseResult.AttrVo attrVo = new ESResponseResult.AttrVo();
            //获取属性ID值
            long attrId = bucket.getKeyAsNumber().longValue();
            attrVo.setAttrId(attrId);

            //获取属性的名字
            ParsedStringTerms attrNameAgg = bucket.getAggregations().get("attr_name_agg");
            String attrName = attrNameAgg.getBuckets().get(0).getKeyAsString();
            attrVo.setAttrName(attrName);

            //获取属性的值
            ParsedStringTerms attrValueAgg = bucket.getAggregations().get("attr_value_agg");
            System.out.println("===1==="+attrValueAgg.getBuckets());

            for (Terms.Bucket b : attrValueAgg.getBuckets()) {
                 String bb = b.getKeyAsString();
                System.out.println("bb:"+bb);
            }

            List attrValues = attrValueAgg.getBuckets().stream().map(item -> item.getKeyAsString()).collect(Collectors.toList());
            attrVo.setAttrValue(attrValues);
            System.out.println("===2==="+attrValues);
            attrVos.add(attrVo);
        }

        result.setAttrs(attrVos);

分页操作

//6、进行分页操作
        result.setPageNum(param.getPageNum());
        //获取总记录数
        long total = hits.getTotalHits().value;
        result.setTotal(total);

        //计算总页码
        int totalPages = (int) total % SearchConstant.PAGE_SIZE == 0 ?
                (int) total / SearchConstant.PAGE_SIZE : ((int) total / SearchConstant.PAGE_SIZE + 1);
        result.setTotalPages(totalPages);

        List pageNavs = new ArrayList<>();
        for (int i = 1; i <= totalPages; i++) {
            pageNavs.add(i);
        }
        result.setPageNavs(pageNavs);

高亮显示

在商品信息赋值里面

你可能感兴趣的:(分布式搜索引擎,elasticsearch,大数据,搜索引擎)