solr的shard

首先shard是这个意思,比如我有5千万数据,都放在一个core太大了。那么,可以分成5个core,每个core是整体数据的1/5,就可以把1/5放到单独的服务器。这5个单独的服务就是5个shard。

这是solr cloud未出来前的做法。

那查询数据要从所有的服务查数据怎么办?

server还是连一个,其它的服务(shard)做为一个参数放到请求参数里,就可以查其它shard了。如:

package com.dhgate.solrj;

import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.Group;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.QueryResponse;

public class ShardSearch {
    public static void main(String args[]) {
        String solr="http://xx.xx.xx.22:9100/search/pro";
        HttpSolrServer solrServer = new HttpSolrServer(solr);
        solrServer.setConnectionTimeout(100);
        solrServer.setDefaultMaxConnectionsPerHost(100);
        solrServer.setMaxTotalConnections(100);
        
        
        
        QueryResponse rsp=query(solrServer);
        /*
        if(rsp.getResponse()!=null&&rsp.getResponse().size()>0) {
            System.out.println(solr+":"+rsp.getResponse());
            
        }
        */
        List<GroupCommand> gcList=rsp.getGroupResponse().getValues();
        for(GroupCommand gc:gcList) {
            System.out.println(gc.getName()+" "+gc.getValues());
            for(Group g:gc.getValues()) {
                System.out.println(g.getGroupValue()+" "+g.getResult().size());
            }
            //System.out.println(JSONObject.toJSON(gc));
        }
        
            
        
    }
    public static QueryResponse  query(HttpSolrServer server) {
        try {
            SolrQuery query = new SolrQuery();
            query.setStart(0);
            query.setRows(10);
            query.setQuery("productname:shoe");
            //query.setQuery("itemcode:539846529");
            
            query.set("group", "true");
            query.set("group.field", "supplierid");
            
            query.set("group.offset", "0");
            query.set("group.limit", "10");
            query.set("group.main", "false");
            query.set("group.ngroups", "true");
            
            //query.set("sort", "score desc,recentlysold desc");
            
          
            
            query.set("shards", "xx.xx.xx.22:9100/search/pro,xx.xx.xx.22:9200/search/pro,xx.xx.xx.24:9200/search/pro,"+
            "xx.xx.xx.24:9100/search/pro,xx.xx.xx.25:9200/search/pro,xx.xx.xx.25:9100/search/pro,"+
                    "xx.xx.50.86:9200/search/pro,xx.xx.50.86:9100/search/pro");
            
            
            
            QueryResponse rsp = server.query(query);
        
            return rsp;
            
        } catch (Exception e) {
            //log.error(e.getMessage(),e);
        }
        
    
        
        return null;
    }

}

文/程忠 浏览次数:0次   2020-10-22 10:14:01

相关阅读


评论: