Riya
Riya

Reputation: 70

Is there a way to query solr "leader" directly using solrj?

I'm having a single shard and 1 leader & 1 replica architecture. When using "CloudSolrClient", queries are being distributed to both leader and replica. But is there a way to point it only to leader(using zookeeper) other than finding the leader manually and building the query?

Upvotes: 0

Views: 529

Answers (1)

AR1
AR1

Reputation: 5023

It's possible to get the Shards leader in SolrJ and there are several scenarios where this is useful, like for instance when you need to perform a backup programmatically (see example in Solr in Action book). Here is the relevant code I use:

private final String COLLECTION_NAME = "myCollection";
private final String ZOOKEPER_CLIENT_TIMEOUT_MS = "1000000"

private Map<String, String> getShardLeaders(CloudSolrServer cloudSolrServer) throws InterruptedException, KeeperException {
        Map<String, String> shardleaders = new TreeMap<String, String>();
        ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
        for (Slice slice : zkStateReader.getClusterState().getSlices(COLLECTION_NAME)) {
            shardleaders.put(slice.getName(), zkStateReader.getLeaderUrl(COLLECTION_NAME, slice.getName(), ZOOKEPER_CLIENT_TIMEOUT_MS));
        }
        return shardleaders;
    }

Upvotes: 0

Related Questions