开发者

How can I get a list of all the cores in a solr server using SolrJ

开发者 https://www.devze.com 2023-03-19 13:39 出处:网络
We are using Solr for our searches, and sharding the data across several cores. We have one core per week of data, so we are dynamically creating and deleting cores each week.

We are using Solr for our searches, and sharding the data across several cores. We have one core per week of data, so we are dynamically creating and deleting cores each week.

How can I query a solr server for a list of all its cores? The JavaDoc says I can use coreAdminHandler.getCoreContainer().getCoreNames(), bu开发者_StackOverflow中文版t I'm not sure how to build a coreAdminHandler object.


A request to http://localhost:8983/solr/admin/cores?action=STATUS (replace your own host/port of course) will return all cores.


Using SolrJ as you asked, here is how I did:

// Solr server instance
CommonsHttpSolrServer solrServer = ...;

// Request core list
CoreAdminRequest request = new CoreAdminRequest();
request.setAction(CoreAdminAction.STATUS);
CoreAdminResponse cores = request.process(solrServer);

// List of the cores
List<String> coreList = new ArrayList<String>();
for (int i = 0; i < cores.getCoreStatus().size(); i++) {
    coreList.add(cores.getCoreStatus().getName(i));
}


Just adding an update to the code sample above as several bits have been deprecated since Solr 4. The following code works on Solr 6.1.0.

package <...>.<...>.<...>;    

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;    

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;    

public class GetCores {
    static String SOLR_URL = "http://...:8983/solr/";    

    public static void getCores() {
        System.out.println("Building Solr server instance");
        HttpSolrClient solrClient=new HttpSolrClient.Builder(SOLR_URL).build();    

        System.out.println("Requesting core list"); 
        CoreAdminRequest request = new CoreAdminRequest();
        request.setAction(CoreAdminAction.STATUS);
        CoreAdminResponse cores=null;

        try {
            cores = request.process(solrClient);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(" Listing cores");
        List<String> coreList = new ArrayList<String>();
        for (int i = 0; i < cores.getCoreStatus().size(); i++) {
            coreList.add(cores.getCoreStatus().getName(i));
        }

        System.out.println(coreList.get(0)+" is the first core");    
    }       
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号