benhsu
benhsu

Reputation: 5526

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

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(), but I'm not sure how to build a coreAdminHandler object.

Upvotes: 19

Views: 24840

Answers (3)

Lefty G Balogh
Lefty G Balogh

Reputation: 1888

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");    
    }       
}

Upvotes: 4

Matthieu Napoli
Matthieu Napoli

Reputation: 49553

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));
}

Upvotes: 22

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99720

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

Upvotes: 33

Related Questions