zoukankan      html  css  js  c++  java
  • Solr的核心操作案例

    @Service
    public class SearchServiceImpl implements SearchService {
    
        @Autowired
        private SolrTemplate solrTemplate;
    
        @Autowired
        private TbItemMapper itemMapper;
    
        @Override
        public Map search(Map searchMap) {
    
            String keywords = (String) searchMap.get("keywords");
            System.out.println("查询关键字是:"+keywords);
    
            //通过solrTemplate进行索引库的查询
    
            //直接创建高亮查询
            SimpleHighlightQuery query = new SimpleHighlightQuery(); //不需要指定*:*
    
            //封装搜索条件
            Criteria criteria = new Criteria("item_keywords");  //要查询的域
            criteria = criteria.contains(keywords);  //要查询的条件
            query.addCriteria(criteria);
    
            //设置高亮效果选项
            HighlightOptions options = new HighlightOptions();
            options.addField("item_title");  //设置哪个域加高亮效果
            options.setSimplePrefix("<em style='color:red'>"); // 高亮头部
            options.setSimplePostfix("</em>"); // 高亮尾部
            query.setHighlightOptions(options);
    
            //增加过滤查询条件--商品分类
            String categoryStr = (String) searchMap.get("category");
            if(categoryStr!=null && categoryStr.length() > 0){
    
                FilterQuery filterQuery = new SimpleFilterQuery();
                Criteria filterCriteria = new Criteria("item_category"); //设置Criteria
                filterCriteria = filterCriteria.contains(categoryStr); //进行查询比较
                filterQuery.addCriteria(filterCriteria);
    
                //增加过滤查询条件
                query.addFilterQuery(filterQuery);
            }
    
            //增加过滤查询条件--商品品牌
            String brandStr = (String) searchMap.get("brand");
            if(brandStr!= null && brandStr.length() > 0){
    
                SimpleFilterQuery filterQuery = new SimpleFilterQuery();
                Criteria fileterCriteria = new Criteria("item_brand");
                fileterCriteria = fileterCriteria.contains(brandStr);  //进行品牌搜索条件封装
                filterQuery.addCriteria(fileterCriteria);
    
                query.addFilterQuery(filterQuery);
            }
    
            //增加过滤查询条件--规格
            Map<String,String> specMap = (Map) searchMap.get("spec");
            if(specMap!=null){
    
                for (String key : specMap.keySet()) {
                    SimpleFilterQuery filterQuery = new SimpleFilterQuery();
                    Criteria filterCriteria = new Criteria("item_spec_"+key);
                    filterCriteria = filterCriteria.contains(specMap.get(key));  //设置搜索的规格内容
                    filterQuery.addCriteria(filterCriteria);
                    query.addFilterQuery(filterQuery);
                }
            }
    
            //增加过滤查询条件--价格
            String priceStr = (String) searchMap.get("price");
            if(priceStr != null && priceStr.length() > 0){
                String[] prices = priceStr.split("-");
    
                //获取起始值作为价格初始条件
                SimpleFilterQuery filterQuery1 = new SimpleFilterQuery();
                Criteria filterCriteria1 = new Criteria("item_price");
                filterCriteria1 = filterCriteria1.greaterThanEqual(prices[0]);  //price>= 价格条件查询
                filterQuery1.addCriteria(filterCriteria1);
                query.addFilterQuery(filterQuery1);
    
                if(!"*".equals(prices[1])){  //当结束价格不定*才会有结束价格范围,否则3000-*,就是起始是3000,没有结束价格
                    //获取结束值作为价格结束条件
                    SimpleFilterQuery filterQuery2 = new SimpleFilterQuery();
                    Criteria filterCriteria2 = new Criteria("item_price");
                    filterCriteria2 = filterCriteria2.lessThanEqual(prices[1]);  //price<= 价格条件查询
                    filterQuery2.addCriteria(filterCriteria2);
                    query.addFilterQuery(filterQuery2);
                }
            }
    
            //设置价格升降序查询
            String sortStr = (String) searchMap.get("sort");
    
            Sort sort;
            if("ASC".equals(sortStr)){
                //设置价格升序查询
                sort = new Sort(Sort.Direction.ASC, "item_price");        //参数一是设置升降序  参数二设置升降序的域
            }else{
                //设置价格降序查询
                sort = new Sort(Sort.Direction.DESC, "item_price");
            }
            query.addSort(sort);
    
            Integer pageNo = (Integer) searchMap.get("pageNo");
            Integer pageSize = (Integer) searchMap.get("pageSize");
    
            //设置分页相关参数
            query.setRows(pageSize);  //pageSize
            query.setOffset((pageNo-1)*pageSize);//offset = (pageNo - 1)*pageSize,起始的索引数
    
            //直接采用solr的高亮查询
            HighlightPage<TbItem> page = solrTemplate.queryForHighlightPage(query, TbItem.class);
    
            List<TbItem> content = page.getContent();
    
            for (TbItem item : content) {
                List<HighlightEntry.Highlight> highlights = page.getHighlights(item);
    
                if(highlights.size() > 0){ //防止出现的结果item_title中没有高亮关键字
                    List<String> snipplets = highlights.get(0).getSnipplets();
                    if(snipplets.size() > 0){
                        item.setTitle(snipplets.get(0));  //设置高亮后的效果
                    }
                }
            }
    
            //封装前端分页插件的需要的属性,rows,total(模仿PageResult)
            Map returnMap = new HashMap<>();
            returnMap.put("rows", content);
            returnMap.put("total",page.getTotalElements());  //TotalElements总记录数
                                                            //page.getTotalPages()总页数
            return returnMap;
        }
    
        @Override
        public void importData(Long[] ids) {
            //根据导入的goods的id,查询出本次需要导入的item对象,保存到solr索引库
            TbItemExample example = new TbItemExample();
            example.createCriteria().andGoodsIdIn(Arrays.asList(ids));
            List<TbItem> items = itemMapper.selectByExample(example);
    
            for (TbItem item : items) {
                System.out.println("导入了:"+item.getTitle());
            }
            solrTemplate.saveBeans(items);
            solrTemplate.commit(); //提交一下
        }
    
        @Override
        public void removeData(Long[] ids) {
            TbItemExample example = new TbItemExample();
            example.createCriteria().andGoodsIdIn(Arrays.asList(ids));
            List<TbItem> items = itemMapper.selectByExample(example);
    
            for (TbItem item : items) {
                solrTemplate.deleteById(item.getId() + "");
    
                System.out.println("删除了:"+item.getTitle());
    
            }
    
            solrTemplate.commit();
        }
    }
    
    
  • 相关阅读:
    Java基础--(一)hello world
    性能测试--jmeter安装与配置
    性能测试--(四)函数
    性能测试--(三)jmeter参数化
    (一)Monkey使用场景及常用命令
    (二)logcat/trace.txt日志文件的分析
    (一)adb部署及使用
    SOAP UI破解及安装
    性能测试常用指标
    shll 基础讲解
  • 原文地址:https://www.cnblogs.com/kitor/p/11310717.html
Copyright © 2011-2022 走看看