Elastic搜索的使用方法

发布时间:2021-07-10 10:32:03 作者:chen
来源:亿速云 阅读:118

本篇内容主要讲解“Elastic搜索的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Elastic搜索的使用方法”吧!

1、前端

Elastic搜索的使用方法

	"specList": {
		"机身颜色": "金色",
        "内存": "3GB"
	},

1.2、前端实现

Elastic搜索的使用方法

Elastic搜索的使用方法

specSearch(specName, optionName, e) {
   //参数1:specName 规格名称
   //参数2:optionName 规格选项的值
   //1) 处理条件:选择值,如果存在添加条件,如果不存在删除条件
    if (optionName){
      //存在
      this.searchMap.specList[specName] = optionName;
    } else {
      //不存在
      delete this.searchMap.specList[specName];
    }
      
      //重新查询
      this.searchList();
    
  //方式1:jquery代码 处理回显
  //获得事件源 a 标签,再获得父标签<dd>,再添加用时
  $(e.target).parent().addClass("cur");
  
  //获得 a 标签父元素的所有,将样式移除
  $(e.target).parent().siblings().removeClass("cur");
    
},

1.2.1、品牌查询

需要使用修改后的品牌进行查询

   brandSearch(bid) {
      //切换品牌
      this.searchMap.brandId = bid;
      //查询
      this.searchList();
    },

1.2.2、关键字查询

Elastic搜索的使用方法

Elastic搜索的使用方法

Elastic搜索的使用方法

1.2.3、排序

Elastic搜索的使用方法

1.2.4、价格区间

Elastic搜索的使用方法

<input type="text"  v-model="searchMap.minPrice" name="" id=""> -
<input type="text"  v-model="searchMap.maxPrice" name="" id="">
<input type="button" @click.prevent="searchList" value="搜索">

1.2.5、分页

Elastic搜索的使用方法

2、后端

Controller

package com.czxy.controller;

import com.czxy.service.SkuSearchService;
import com.czxy.vo.BaseResult;
import com.czxy.vo.SearchVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 庭前云落
 * @Date 2020/4/15 21:39
 * @description
 */
@RestController
@RequestMapping("/sku")
public class SkuSearchController {

    @Resource
    private SkuSearchService skuSearchService;

    @PostMapping("/search")
    public BaseResult findSkus(@RequestBody SearchVo searchVo) {
        if (searchVo.getCatId() == null) {
            return BaseResult.error("分类不能为空");
        }
        Map search = skuSearchService.search(searchVo);
        System.out.println(search);
        return BaseResult.ok("查询成功", search);
    }
}

Service

package com.czxy.service.impl;

import com.czxy.repository.SkuRepository;
import com.czxy.service.SkuSearchService;
import com.czxy.vo.ReturnSku;
import com.czxy.vo.SearchSku;
import com.czxy.vo.SearchVo;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 庭前云落
 * @Date 2020/4/15 21:40
 * @description
 */
@Service
public class SkuSearchServiceImpl implements SkuSearchService {
    @Resource
    private SkuRepository skuRepository;

    public Map search(SearchVo searchVo){
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //分类查询
        boolQueryBuilder.must(QueryBuilders.termQuery("catId",searchVo.getCatId()));
        //关键字查询
        if(StringUtils.isNotBlank(searchVo.getKeyword())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("skuName",searchVo.getKeyword()));
        }
        //品牌查询
        if(searchVo.getBrandId()!=null){
           boolQueryBuilder.must(QueryBuilders.termQuery("brandId",searchVo.getBrandId()));
        }
        //规格查询
        Map<String, String> specList = searchVo.getSpecList();
        if(specList!=null){
            for (Map.Entry<String, String> entry : specList.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                boolQueryBuilder.must(QueryBuilders.termQuery("spces."+key+".keyword",value));
            }
        }
        if(searchVo.getMaxPrice()!=null&&searchVo.getMaxPrice()!=null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").gte(searchVo.getMinPrice()).lt(searchVo.getMaxPrice()));
        }
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.withQuery(boolQueryBuilder);


        if (searchVo.getSortBy()!=null){
            if(searchVo.getSortBy().equals("xl")&&searchVo.getSortWay().equals("asc")){
                //销量升序
                queryBuilder.withSort(SortBuilders.fieldSort("sellerCount").order(SortOrder.ASC));
            }else  if(searchVo.getSortBy().equals("xl")&&searchVo.getSortWay().equals("desc")) {
                // 销量降序
                queryBuilder.withSort(SortBuilders.fieldSort("sellerCount").order(SortOrder.DESC));
            }else if(searchVo.getSortBy().equals("jg")&&searchVo.getSortWay().equals("asc")){
                // 价格升序
                queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
            }else  if(searchVo.getSortBy().equals("jg")&&searchVo.getSortWay().equals("desc")) {
                // 价格降序
                queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
            }else if(searchVo.getSortBy().equals("pl")&&searchVo.getSortWay().equals("asc")){
                // 评论升序
                queryBuilder.withSort(SortBuilders.fieldSort("commentCount").order(SortOrder.ASC));
            }else  if(searchVo.getSortBy().equals("pl")&&searchVo.getSortWay().equals("desc")) {
                // 评论降序
                queryBuilder.withSort(SortBuilders.fieldSort("commentCount").order(SortOrder.DESC));
            }else if(searchVo.getSortBy().equals("sj")&&searchVo.getSortWay().equals("asc")){
                // 上架时间
                queryBuilder.withSort(SortBuilders.fieldSort("onSaleTime").order(SortOrder.ASC));
            }else  if(searchVo.getSortBy().equals("sj")&&searchVo.getSortWay().equals("desc")) {
                // 上架时间
                queryBuilder.withSort(SortBuilders.fieldSort("onSaleTime").order(SortOrder.DESC));
            }
        }

        // 2.2 分页
        queryBuilder.withPageable(PageRequest.of(searchVo.getPageNum() - 1 ,searchVo.getPageSize()));


        //3 查询,获取结果
        // 3.1 查询
        Page<SearchSku> pageInfo = this.skuRepository.search(queryBuilder.build());

        // 2.2 总条数
        long total = pageInfo.getTotalElements();

        // 3.3 获取返回结果 ,组装返回数据(SearchSku-->Return)
        List<SearchSku> list = pageInfo.getContent();

        List<ReturnSku> returnList = new ArrayList<>();

        for(SearchSku sku:list){
            //创建 ReturnSku对象
            ReturnSku rs = new ReturnSku();
            //依次填充数据
            rs.setId(sku.getId().intValue());
            rs.setGoodsName(sku.getSkuName());
            rs.setMidlogo(sku.getLogo());
            rs.setCommentCount(sku.getCommentCount());
            rs.setPrice(sku.getPrice());

            returnList.add(rs);
        }

        // 3.4 封装
        Map result = new HashMap();
        result.put("count" , total);
        result.put("list" ,returnList);

        return result;
    }
}

到此,相信大家对“Elastic搜索的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. elastic ik中文分词测试
  2. Elastic search performance testing finding

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elastic

上一篇:如何在IDEA中解决Git冲突

下一篇:java如何实现显示目录文件列表和删除目录功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》