org.apache.solr.client.solrj.impl

## Class CloudSolrClient

- java.lang.Object
- - org.apache.solr.client.solrj.SolrClient
  - - org.apache.solr.client.solrj.impl.CloudSolrClient

All Implemented Interfaces:
  Serializable

Direct Known Subclasses:
  CloudSolrServer

    public class CloudSolrClient
    extends SolrClient

SolrJ client class to communicate with SolrCloud. Instances of this class communicate with Zookeeper to discover Solr endpoints for SolrCloud collections, and then use the `LBHttpSolrClient` to issue requests. This class assumes the id field for your documents is called 'id' - if this is not the case, you must set the right name with `setIdField(String)`.

See Also:
  Serialized Form

### Nested Class Summary

Nested Classes
`static class` `CloudSolrClient.RouteException`
`static class` `CloudSolrClient.RouteResponse`

### Field Summary

Fields
`protected Map<String,org.apache.solr.client.solrj.impl.CloudSolrClient.ExpiringCachedDocCollection>` `collectionStateCache`
`protected static org.slf4j.Logger` `log`
`static String` `STATE_VERSION`

### Constructor Summary

Constructors
Constructor and Description
`CloudSolrClient(Collection<String> zkHosts, String chroot)` Create a new client object using multiple string values in a Collection instead of a standard zkHost connection string.
`CloudSolrClient(Collection<String> zkHosts, String chroot, org.apache.http.client.HttpClient httpClient)` Create a new client object using multiple string values in a Collection instead of a standard zkHost connection string.
`CloudSolrClient(String zkHost)` Create a new client object that connects to Zookeeper and is always aware of the SolrCloud state.
`CloudSolrClient(String zkHost, boolean updatesToLeaders)`
`CloudSolrClient(String zkHost, boolean updatesToLeaders, org.apache.http.client.HttpClient httpClient)`
`CloudSolrClient(String zkHost, org.apache.http.client.HttpClient httpClient)` Create a new client object that connects to Zookeeper and is always aware of the SolrCloud state.
`CloudSolrClient(String zkHost, LBHttpSolrClient lbClient)`
`CloudSolrClient(String zkHost, LBHttpSolrClient lbClient, boolean updatesToLeaders)`

### Method Summary

Methods
`CloudSolrClient.RouteResponse` `condenseResponse(NamedList response, long timeMillis)`
`void` `connect()` Connect to the zookeeper ensemble.
`String` `getDefaultCollection()` Gets the default collection for request
`protected DocCollection` `getDocCollection(ClusterState clusterState, String collection)`
`String` `getIdField()`
`LBHttpSolrClient` `getLbClient()`
`int` `getMinAchievedReplicationFactor(String collection, NamedList resp)` Useful for determining the minimum achieved replication factor across all shards involved in processing an update request, typically useful for gauging the replication factor of a batch.
`ResponseParser` `getParser()`
`RequestWriter` `getRequestWriter()`
`Map<String,Integer>` `getShardReplicationFactor(String collection, NamedList resp)` Walks the NamedList response after performing an update request looking for the replication factor that was achieved in each shard involved in the request.
`String` `getZkHost()`
`ZkStateReader` `getZkStateReader()`
`boolean` `isUpdatesToLeaders()`
`NamedList<Object>` `request(SolrRequest request)` SolrServer implementations need to implement how a request is actually processed
`protected NamedList<Object>` `requestWithRetryOnStaleState(SolrRequest request, int retryCount, String collection)` As this class doesn't watch external collections on the client side, there's a chance that the request will fail due to cached stale state, which means the state must be refreshed from ZK and retried.
`protected NamedList<Object>` `sendRequest(SolrRequest request)`
`void` `setCollectionCacheTTl(int seconds)` Sets the cache ttl for DocCollection Objects cached .
`void` `setDefaultCollection(String collection)` Sets the default collection for request
`void` `setIdField(String idField)`
`void` `setParallelCacheRefreshes(int n)` If caches are expired they are refreshed after acquiring a lock.
`void` `setParallelUpdates(boolean parallelUpdates)`
`void` `setParser(ResponseParser processor)` Note: This setter method is not thread-safe.
`void` `setRequestWriter(RequestWriter requestWriter)`
`void` `setZkClientTimeout(int zkClientTimeout)` Set the timeout to the zookeeper ensemble in ms
`void` `setZkConnectTimeout(int zkConnectTimeout)` Set the connect timeout to the zookeeper ensemble in ms
`void` `shutdown()` Release allocated resources.

### Methods inherited from class org.apache.solr.client.solrj.SolrClient

`add, add, add, add, addBean, addBean, addBeans, addBeans, commit, commit, commit, deleteById, deleteById, deleteById, deleteById, deleteByQuery, deleteByQuery, getBinder, optimize, optimize, optimize, ping, query, query, queryAndStreamResponse, rollback`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### log

    protected static final org.slf4j.Logger log

#### STATE_VERSION

    public static final String STATE_VERSION

See Also:
  Constant Field Values

#### collectionStateCache

    protected final Map<String,org.apache.solr.client.solrj.impl.CloudSolrClient.ExpiringCachedDocCollection> collectionStateCache

### Constructor Detail

#### CloudSolrClient

    public CloudSolrClient(String zkHost)

Create a new client object that connects to Zookeeper and is always aware of the SolrCloud state. If there is a fully redundant Zookeeper quorum and SolrCloud has enough replicas for every shard in a collection, there is no single point of failure. Updates will be sent to shard leaders by default.

Parameters:
  `zkHost` - The client endpoint of the zookeeper quorum containing the cloud state. The full specification for this string is one or more comma separated HOST:PORT values, followed by an optional chroot value that starts with a forward slash. Using a chroot allows multiple applications to coexist in one ensemble. For full details, see the Zookeeper documentation. Some examples:

  "host1:2181"

  "host1:2181,host2:2181,host3:2181/mysolrchroot"

  "zoo1.example.com:2181,zoo2.example.com:2181,zoo3.example.com:2181"

#### CloudSolrClient

    public CloudSolrClient(String zkHost,
                   org.apache.http.client.HttpClient httpClient)

Create a new client object that connects to Zookeeper and is always aware of the SolrCloud state. If there is a fully redundant Zookeeper quorum and SolrCloud has enough replicas for every shard in a collection, there is no single point of failure. Updates will be sent to shard leaders by default.

Parameters:
  `zkHost` - The client endpoint of the zookeeper quorum containing the cloud state. The full specification for this string is one or more comma separated HOST:PORT values, followed by an optional chroot value that starts with a forward slash. Using a chroot allows multiple applications to coexist in one ensemble. For full details, see the Zookeeper documentation. Some examples:

  "host1:2181"

  "host1:2181,host2:2181,host3:2181/mysolrchroot"

  "zoo1.example.com:2181,zoo2.example.com:2181,zoo3.example.com:2181"
  `httpClient` - the `HttpClient` instance to be used for all requests. The provided httpClient should use a multi-threaded connection manager.

#### CloudSolrClient

    public CloudSolrClient(Collection<String> zkHosts,
                   String chroot)

Create a new client object using multiple string values in a Collection instead of a standard zkHost connection string. Note that this method will not be used if there is only one String argument - that will use `CloudSolrClient(String)` instead.

Parameters:
  `zkHosts` - A Java Collection (List, Set, etc) of HOST:PORT strings, one for each host in the zookeeper ensemble. Note that with certain Collection types like HashSet, the order of hosts in the final connect string may not be in the same order you added them.
  `chroot` - A chroot value for zookeeper, starting with a forward slash. If no chroot is required, use null.
Throws:
  `IllegalArgumentException` - if the chroot value does not start with a forward slash.
See Also:
  `CloudSolrClient(String)`

#### CloudSolrClient

    public CloudSolrClient(Collection<String> zkHosts,
                   String chroot,
                   org.apache.http.client.HttpClient httpClient)

Create a new client object using multiple string values in a Collection instead of a standard zkHost connection string. Note that this method will not be used if there is only one String argument - that will use `CloudSolrClient(String)` instead.

Parameters:
  `zkHosts` - A Java Collection (List, Set, etc) of HOST:PORT strings, one for each host in the zookeeper ensemble. Note that with certain Collection types like HashSet, the order of hosts in the final connect string may not be in the same order you added them.
  `chroot` - A chroot value for zookeeper, starting with a forward slash. If no chroot is required, use null.
  `httpClient` - the `HttpClient` instance to be used for all requests. The provided httpClient should use a multi-threaded connection manager.
Throws:
  `IllegalArgumentException` - if the chroot value does not start with a forward slash.
See Also:
  `CloudSolrClient(String)`

#### CloudSolrClient

    public CloudSolrClient(String zkHost,
                   boolean updatesToLeaders)

Parameters:
  `zkHost` - A zookeeper client endpoint.
  `updatesToLeaders` - If true, sends updates only to shard leaders.
See Also:
  `for full description and details on zkHost`

#### CloudSolrClient

    public CloudSolrClient(String zkHost,
                   boolean updatesToLeaders,
                   org.apache.http.client.HttpClient httpClient)

Parameters:
  `zkHost` - A zookeeper client endpoint.
  `updatesToLeaders` - If true, sends updates only to shard leaders.
  `httpClient` - the `HttpClient` instance to be used for all requests. The provided httpClient should use a multi-threaded connection manager.
See Also:
  `for full description and details on zkHost`

#### CloudSolrClient

    public CloudSolrClient(String zkHost,
                   LBHttpSolrClient lbClient)

Parameters:
  `zkHost` - A zookeeper client endpoint.
  `lbClient` - LBHttpSolrServer instance for requests.
See Also:
  `for full description and details on zkHost`

#### CloudSolrClient

    public CloudSolrClient(String zkHost,
                   LBHttpSolrClient lbClient,
                   boolean updatesToLeaders)

Parameters:
  `zkHost` - A zookeeper client endpoint.
  `lbClient` - LBHttpSolrServer instance for requests.
  `updatesToLeaders` - If true, sends updates only to shard leaders.
See Also:
  `for full description and details on zkHost`

### Method Detail

#### setCollectionCacheTTl

    public void setCollectionCacheTTl(int seconds)

Sets the cache ttl for DocCollection Objects cached . This is only applicable for collections which are persisted outside of clusterstate.json

Parameters:
  `seconds` - ttl value in seconds

#### getParser

    public ResponseParser getParser()

#### setParser

    public void setParser(ResponseParser processor)

Note: This setter method is not thread-safe.

Parameters:
  `processor` - Default Response Parser chosen to parse the response if the parser were not specified as part of the request.
See Also:
  `SolrRequest.getResponseParser()`

#### getRequestWriter

    public RequestWriter getRequestWriter()

#### setRequestWriter

    public void setRequestWriter(RequestWriter requestWriter)

#### getZkHost

    public String getZkHost()

Returns:
  the zkHost value used to connect to zookeeper.

#### getZkStateReader

    public ZkStateReader getZkStateReader()

#### setIdField

    public void setIdField(String idField)

Parameters:
  `idField` - the field to route documents on.

#### getIdField

    public String getIdField()

Returns:
  the field that updates are routed on.

#### setDefaultCollection

    public void setDefaultCollection(String collection)

Sets the default collection for request

#### getDefaultCollection

    public String getDefaultCollection()

Gets the default collection for request

#### setZkConnectTimeout

    public void setZkConnectTimeout(int zkConnectTimeout)

Set the connect timeout to the zookeeper ensemble in ms

#### setZkClientTimeout

    public void setZkClientTimeout(int zkClientTimeout)

Set the timeout to the zookeeper ensemble in ms

#### connect

    public void connect()

Connect to the zookeeper ensemble. This is an optional method that may be used to force a connect before any other requests are sent.

#### setParallelUpdates

    public void setParallelUpdates(boolean parallelUpdates)

#### condenseResponse

    public CloudSolrClient.RouteResponse condenseResponse(NamedList response,
                                                 long timeMillis)

#### request

    public NamedList<Object> request(SolrRequest request)
                              throws SolrServerException,
                                     IOException

Description copied from class: `SolrClient`
SolrServer implementations need to implement how a request is actually processed

Specified by:
  `request` in class `SolrClient`
Throws:
  `SolrServerException`
  `IOException`

#### requestWithRetryOnStaleState

    protected NamedList<Object> requestWithRetryOnStaleState(SolrRequest request,
                                                 int retryCount,
                                                 String collection)
                                                      throws SolrServerException,
                                                             IOException

As this class doesn't watch external collections on the client side, there's a chance that the request will fail due to cached stale state, which means the state must be refreshed from ZK and retried.

Throws:
  `SolrServerException`
  `IOException`

#### sendRequest

    protected NamedList<Object> sendRequest(SolrRequest request)
                                     throws SolrServerException,
                                            IOException

Throws:
  `SolrServerException`
  `IOException`

#### shutdown

    public void shutdown()

Description copied from class: `SolrClient`
Release allocated resources.

Specified by:
  `shutdown` in class `SolrClient`

#### getLbClient

    public LBHttpSolrClient getLbClient()

#### isUpdatesToLeaders

    public boolean isUpdatesToLeaders()

#### setParallelCacheRefreshes

    public void setParallelCacheRefreshes(int n)

If caches are expired they are refreshed after acquiring a lock. use this to set the number of locks

#### getDocCollection

    protected DocCollection getDocCollection(ClusterState clusterState,
                                 String collection)
                                      throws SolrException

Throws:
  `SolrException`

#### getMinAchievedReplicationFactor

    public int getMinAchievedReplicationFactor(String collection,
                                      NamedList resp)

Useful for determining the minimum achieved replication factor across all shards involved in processing an update request, typically useful for gauging the replication factor of a batch.

#### getShardReplicationFactor

    public Map<String,Integer> getShardReplicationFactor(String collection,
                                                NamedList resp)

Walks the NamedList response after performing an update request looking for the replication factor that was achieved in each shard involved in the request. For single doc updates, there will be only one shard in the return value.