How to Add or Delete Replicas in Solr Collection

In this article, We will understand about Solr Search and How to add or delete a replica in the Solr collection?
As a whole, Adding a replica to an existing collection can be done via Solr API call with “action=ADDREPLICA” and Deleting a replica via “action=DELETEREPLICA” followed by the node name where the replica is available or wanted to be created
What is Apache Solr?
Apache Solr is a search engine works based on the apache Lucene library, Solr takes input as structured, semi-structured, and unstructured data and stores and indexes it, Which will be available for the user in near-real time
The document is the basic unit of Solr and it is stored as a collection, Each Solr collection would be divided into multiple shards and stored in different hosts
Example to create a Solr collection
solrctl instancedir --generate learnshare
solrctl instancedir --create learnshare learnshare/
solrctl collection --create learnshare -a -s 1 -r 1
“collections”:{
“learnshare”:{
“pullReplicas”:”0″,
“replicationFactor”:”1″,
“shards”:{“shard1”:{
“range”:”80000000-7fffffff”,
“state”:”active”,
“replicas”:{“core_node2”:{
“dataDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node2/data/”,
“base_url”:”http://<Solr server hostname>:8983/solr”,
“node_name”:”<Solr server hostname>:8983_solr”,
“type”:”NRT”,
“force_set_state”:”false”,
“ulogDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node2/data/tlog”,
“core”:”learnshare_shard1_replica_n1″,
“shared_storage”:”true”,
“state”:”active”,
“leader”:”true”}}}},
“router”:{“name”:”compositeId”},
From the above example, Below are the explanation of the terminology
Collection
In the above example, the collection name is Learnshare and it has 2 shards with 2 replicas each
Shard
The collection will be divided into multiple shards for better distribution of the load
Replica
A replica is a copy of shared data in a different host. One of these copies will act as a leader, Where all the requests will be diverted and another copy will act as a backup. If the leader replica goes down another copy will be elected as a leader
In the above example collection: We can easily understand if the replica is active or down using the below status in the clusterstate.json file (Check here for more understanding on clusterstate.json file)
"state":"active",
In this article, We will be learning how to add and delete a replica from a shard (collection)
Below are some of the scenarios, Where we need to add and delete a replica
– Due to hardware failure, there are chances the replica might go down, In this case, We can add a new replica to a different host and delete the down replica
– Migration: If you want to migrate a replica to a different host, In this case, We can simply add and delete a replica
How do you add replicas in Solr?
To add replicas in Solr, Action ADDREPLICA is used to create one or more replicas to a shard in a Solr collection, While creating the replica, We can specify the node name, Where the replica has to be created
Command to add a new replica
$ curl -k --negotiate -u : "http://$(hostname -f):8983/solr/admin/collections?action=ADDREPLICA&collection=learnshare&shard=shard1&node=<Solr Hostname>:8983_solr"
{
"responseHeader":{
"status":0,
"QTime":1282},
"success":{
"<Solr Hostname>:8983_solr":{
"responseHeader":{
"status":0,
"QTime":888},
"core":"learnshare_shard1_replica_n3"}}}
Output
“collections”:{
“learnshare”:{
“pullReplicas”:”0″,
“replicationFactor”:”1″,
“shards”:{“shard1”:{
“range”:”80000000-7fffffff”,
“state”:”active”,
“replicas”:{
“core_node2”:{
“dataDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node2/data/”,
“base_url”:”http://<Solr Hostname>e:8983/solr”,
“node_name”:”<Solr Hostname>:8983_solr”,
“type”:”NRT”,
“force_set_state”:”false”,
“ulogDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node2/data/tlog”,
“core”:”learnshare_shard1_replica_n1″,
“shared_storage”:”true”,
“state”:”active”,
“leader”:”true”},
“core_node4”:{
“dataDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node4/data/”,
“base_url”:”http://<Solr Hostname>:8983/solr”,
“node_name”:”<Solr Hostname>:8983_solr”,
“type”:”NRT”,
“force_set_state”:”false”,
“ulogDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node4/data/tlog”,
“core”:”learnshare_shard1_replica_n3″,
“shared_storage”:”true”,
“state”:”active”}}}},
The above command will add a new replica to the collection: Learnshare & Shard1
How do you delete replicas in Solr?
To delete replicas in Solr, Action DELETEREPLICA is used to delete one or more replicas in a Solr collection using a Solr API call.
Command to delete a replica
$ curl -k --negotiate -u : "http://$(hostname -f):8983/solr/admin/collections?action=DELETEREPLICA&collection=learnshare&shard=shard1&replica=core_node2"
{
"responseHeader":{
"status":0,
"QTime":85},
"success":{
"<Solr Hostname>:8983_solr":{
"responseHeader":{
"status":0,
"QTime":69}}}}
Above command will delete a replica from the collection: Learnshare & shard1& replica:2
Output
“collections”:{
“learnshare”:{
“pullReplicas”:”0″,
“replicationFactor”:”1″,
“shards”:{“shard1”:{
“range”:”80000000-7fffffff”,
“state”:”active”,
“replicas”:{“core_node4”:{
“dataDir”:”hdfs:///<Namenode Hostname>:8020/solr/learnshare/core_node4/data/”,
“base_url”:”http://<Solr Hostname>:8983/solr”,
“node_name”:”<Solr Hostname>:8983_solr”,
“type”:”NRT”,
“force_set_state”:”false”,
“ulogDir”:”hdfs://<Namenode Hostname>:8020/solr/learnshare/core_node4/data/tlog”,
“core”:”learnshare_shard1_replica_n3″,
“shared_storage”:”true”,
“state”:”active”,
“leader”:”true”}}}},
Validation
To validate, We can get the latest clusterstate.json file with the below command to validate if all the replicas are active
curl -ik --negotiate -u : "http://$(hostname -f):8983/solr/admin/collections?action=clusterstatus&wt=json&indent=true"
We have learned to add and delete a replica to a Solr collection, Good Luck with your Learning !!