1. 程式人生 > >solr-geohsah 按照距離搜索分組

solr-geohsah 按照距離搜索分組

shm system aso http tlist conf ans 情況 quest

通過solr的domain-import,將mysql的數據通過查詢,導入到solr中。java通過使用solrj,鏈接solr,調用domaininport,並將分頁參數設置到domain-import中(防止查詢所有數據),通過線程sleep,查詢solr導入情況,如果導入成功,則繼續導入下一頁,直到所有數據導入完成。數據導入完成之後,客戶端通過地圖的縮放等級,發送當前位置的精度緯度,以及當前地圖顯示的最大距離,將數據發送值服務器,服務器通過查詢solr中數據,將對應數據所在的經緯度、統計等信息返回給客戶端。

solr chema配置

<fieldtype name="geohash" class="solr.GeoHashField"/>
從數據導入solr配置
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.211.226:3306/adwl_dev_v2?useUnicode=true&amp;characterEncoding=utf8" user="root" password="111111" /> <document> <entity name="solr_docking_insert" transformer="DateFormatTransformer" query=" sql查詢*** limit ${dataimporter.request.startpage},${dataimporter.request.endpage}"> </entity> </document> </dataConfig>
//動態調用SolrDomainImport
public static String importData(Long start,String url){
        System.out.println("貨源導入數據開始:"+start+" "+perPage);
        Map<String , String> map=new HashMap<String, String>();
        if(isclear){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            isclear=false;
            map.put("clean", "true");
        }else{
            map.put("clean", "false");
        }
        map.put("command", "full-import");
        map.put("commit", "true");
        map.put("debug", "false");
        map.put("endpage", perPage+"");
        map.put("indent", "true");
        map.put("optimize", "false");
        map.put("startpage", start+"");
        map.put("verbose", "false");
        map.put("wt", "json");
        //向移動交互服務發送httppost請求
        String str= HttpPostUtil.doPost(url, map,null);
        return str;
    }
    public SimpleResult groupCargo(LbsSearchRequestDto lbsSearchRequestDto){
        try{
        if(lbsSearchRequestDto==null)
            return new SimpleResult("body不能為空", StateCode.VALIDATOR_FAIL, null, false);
        LbsSearchBodyDto lbsBdoy=lbsSearchRequestDto.getBodyDto();
        if(lbsBdoy.getLevel()==null)
            return new SimpleResult("Level不能為空", StateCode.VALIDATOR_FAIL, null, false);
        List<String> fl=new ArrayList<String>();
        //level為省級按照省分組
        if(lbsBdoy.getLevel()==1){
            fl.add("rciShipperCity:notfind");
            fl.add("rciShipperArea:notfind");
        //level為市級按照市分組
        }else if(lbsBdoy.getLevel()==2){
            fl.add("rciShipperArea:notfind");
            fl.add("-rciShipperCity:notfind");
        //level為區縣級按照區縣分組
        }else if(lbsBdoy.getLevel()==3){
            fl.add("-rciShipperArea:notfind");
        }
        if(lbsBdoy.getPageSize()==null)lbsBdoy.setPageSize(0);
        if(lbsBdoy.getPageNumber()==null)lbsBdoy.setPageNumber(50);
        //按照經緯度和距離查詢
        PageData data=solrCargoLLSearch.queryLL(lbsBdoy.getPageSize(), lbsBdoy.getPageNumber(), lbsBdoy.getD(), "geohash", lbsBdoy.getLat(),lbsBdoy.getLon(), fl);
        
        LbsSearchGroupResponseDto lsgrb=new LbsSearchGroupResponseDto();
        LbsSearchGroupResponseBodyDto lsgrbd=new LbsSearchGroupResponseBodyDto();
        lsgrb.setBodyDto(lsgrbd);
        List<LbsGroupMsg> lbsGroupMsgs=new ArrayList<LbsGroupMsg>();
        String solrMsg=data.getData();
        JSONArray array=JSONArray.parseArray(solrMsg);
        for(int i=0;i<array.size();i++){
            //TODO數據整合
        }
        lsgrbd.setLbsGroupMsgs(lbsGroupMsgs);
        return new SimpleResult("成功", 200, lsgrbd, true);
        }catch(Exception e){
            e.printStackTrace();
            return new SimpleResult("服務器錯誤", 500, null, true);
        }
        
    }
/**
     * 
     * @param orderField 排序字段 必須為index字段
     * @param start 分頁開始值
     * @param rows 分頁結束值
     * @param filters 條件
     * @return
     */
    public  PageData  queryDataArrays(String orderField,Integer start,Integer rows,Map<String, String>latAndlon,List<String>filters){
        PageData pageData=null;
        //HttpSolrServer server = SolrServerFactory.getCargoServer();
        SolrQuery query =  new SolrQuery(q);
        query.setStart(start);
        query.setRows(rows);
        if(null!=latAndlon){
            query.addFilterQuery("{!geofilt}");
            Set<String>set=latAndlon.keySet();
            for(String key:set){
                System.out.println(key+" : "+latAndlon.get(key));
                query.set(key, latAndlon.get(key));
            }
        }
        if(filters!=null)
            for(String filter:filters)
                query.addFilterQuery(filter);
        
        if(orderField!=null)
            query.addSort(orderField, ORDER.desc);
        try {
            QueryResponse rsp = getServer().query(query);
            SolrDocumentList docs = rsp.getResults();
            pageData=new PageData();
            pageData.setSize(docs.getNumFound());
            pageData.setData(getJsons(docs));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
         return pageData;
    }

solr-geohsah 按照距離搜索分組