19

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.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
benhsu
  • 5,346
  • 8
  • 39
  • 47

3 Answers3

31

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

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 2
    First, this isn't SolrJ. Also, being a text response the best you can get is the **name** of every core, not instances of `SolrCore`. – tar Sep 21 '15 at 19:09
  • 1
    In question is "...using SolrJ" not using REST API – librucha Nov 16 '16 at 21:45
22

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));
}
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • Not really, the best you can get is the **name** of every core, not instances of `SolrCore`. – tar Sep 21 '15 at 19:09
3

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");    
    }       
}
Lefty G Balogh
  • 1,771
  • 3
  • 26
  • 40