solr7.2版本在项目中的使用

一.数据库单表有百万条数据,走数据库查询速度比较慢,决定搭一台搜索服务器,数据放到搜索服务器上,接口从搜索引擎中去查询。项目中采用的是solr 搜索引擎。

Solr的优缺点

优点

  1. Solr有一个更大、更成熟的用户、开发和贡献者社区。
  2. 支持添加多种格式的索引,如:HTML、PDF、微软 Office 系列软件格式以及 JSON、XML、CSV 等纯文本格式。
  3. Solr比较成熟、稳定。
  4. 不考虑建索引的同时进行搜索,速度更快。

缺点

  1. 建立索引时,搜索效率下降,实时索引搜索效率不高。

二.项目中采用的是solr7.2版本,maven管理项目,直接导入solr的依赖即可。

    


三. 项目中的实际使用

 SolrQuery query = new SolrQuery();
        // 设置参数
        query.setQuery(queryParam);   // 设置查询参数
        query.set("group","true");   // 可以分组
        query.set("group.field","mallId"); // 分组字段为 mallId
        query.set("group.ngroups","true"); // 分组后   返回分组后的总条数
        query.addFacetField("mallId");   // 
        query.set("sort","mallName asc");  // 设置排序
        query.setStart(req.getLimitStart());  // 设置搜索的起始顺序
        query.setRows(req.getPageSize());  // 设置搜索的数量
      
        Map<String, Object> searchMap = req.getSearchMap();
        StringBuilder builder = new StringBuilder("");
        // 封装搜索参数
        for(String param : searchMap.keySet()) {
            Object value = searchMap.get(param);
            builder.append(" AND ");
            if("floorName".equals(param)){
                builder.append("floor").append(":").append("\"" + value + "\"");
                continue;
            }
            builder.append(param).append(":").append("\"" + value + "\"");
        } // 如果是多参数请求,  封装请求参数   中间用 " AND "隔开
        String queryParam = builder.length()> 0?builder.substring(" AND ".length(),builder.length()):"*:*";

创建连接,封装工具类

public class SolrUtil {
    // url 在properties配置
    private final static String solrUrl = SystemConstants.getSolrUrl();

    public static HttpSolrClient getSolrClient(){
        return new HttpSolrClient.Builder(solrUrl)
                .withConnectionTimeout(10000)
                .withSocketTimeout(60000)
                .build();
    }

}
创建连接,并赋值代码如下
try{
            //创建连接
            HttpSolrClient server = SolrUtil.getSolrClient();
            rsp = server.query("yunpu",query); // yunpu 是指solr中的collection名称
        } catch(Exception e){
            e.printStackTrace();
        }
        GroupResponse groupResponse = rsp.getGroupResponse();
        if(groupResponse != null){
            List<GroupCommand> commands  = groupResponse.getValues();
            if(commands != null) {
                for(GroupCommand command: commands){
                    // 返回总记录数
                    pageResp.setTotal(command.getNGroups());
                    List<Group> groups = command.getValues();
                    if(groups !=null){
                        for(Group group:groups){
                            SolrDocumentList solrDocument = group.getResult();
                            Iterator<SolrDocument> documentList = solrDocument.iterator();
                            while(documentList.hasNext()){
                                PopupDpMallInfo mallInfo = new PopupDpMallInfo();
                                // 如果mallId为 null跳出循环
                                SolrDocument document=documentList.next();
                                if(document.get("mallId") == null){
                                    continue;
                                }
                                mallInfo.setMallId((Integer) document.get("mallId"));
                                mallInfo.setMallName((String) document.get("mallName"));
                                mallInfo.setAddress((String) document.get("address"));
                                mallInfo.setProvinceName((String) document.get("provinceName"));
                                mallInfo.setCityName((String) document.get("cityName"));
                                mallInfo.setDistrictName((String) document.get("districtName"));
                                mallInfo.setDpUrl((String) document.get("dpUrl"));

                                // 查询一个商业体下所有的商铺
                                SolrQuery queryStore = new SolrQuery();
                                queryStore.setQuery("mallId:"+mallInfo.getMallId());
                                queryStore.set("start", 0);
                                queryStore.set("rows",Integer.MAX_VALUE);
                                queryStore.set("sort","floor asc");
                                QueryResponse storeRsp = null;
                                try {
                                    storeRsp = SolrUtil.getSolrClient().query("yunpu",queryStore);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                SolrDocumentList storeDocumentList =storeRsp.getResults();
                                List<PopupDpShopInfoVo> shopInfoVoList = getShopInfoVoList(storeDocumentList);
                                mallInfo.setStoreNum(storeDocumentList.size());
                                mallInfo.setShopInfoVoList(shopInfoVoList);
                                dpMallInfoList.add(mallInfo);
                            }
                        }
                    }
                }
            }
中间摸索了一阵子,基本实现了需求。先记录一下子



猜你喜欢

转载自blog.csdn.net/anshengsuiyeu/article/details/80931753
今日推荐