Class CloudSolrClient
- java.lang.Object
- 
- org.apache.solr.client.solrj.SolrClient
- 
- org.apache.solr.client.solrj.impl.CloudSolrClient
 
 
- 
- All Implemented Interfaces:
- Closeable,- Serializable,- AutoCloseable
 
 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 theLBHttpSolrClientto 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 withsetIdField(String).- See Also:
- Serialized Form
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classCloudSolrClient.BuilderConstructsCloudSolrClientinstances from provided configuration.static classCloudSolrClient.RouteExceptionstatic classCloudSolrClient.RouteResponse
 - 
Field SummaryFields Modifier and Type Field Description protected org.apache.solr.client.solrj.impl.CloudSolrClient.StateCachecollectionStateCachestatic StringSTATE_VERSION
 - 
Constructor SummaryConstructors Modifier Constructor Description protectedCloudSolrClient(CloudSolrClient.Builder builder)Create a new client object that connects to Zookeeper and is always aware of the SolrCloud state.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()CloudSolrClient.RouteResponsecondenseResponse(NamedList response, int timeMillis)voidconnect()Connect to the zookeeper ensemble.voidconnect(long duration, TimeUnit timeUnit)Connect to a cluster.ClusterStateProvidergetClusterStateProvider()StringgetDefaultCollection()Gets the default collection for requestprotected DocCollectiongetDocCollection(String collection, Integer expectedVersion)org.apache.http.client.HttpClientgetHttpClient()StringgetIdField()LBHttpSolrClientgetLbClient()intgetMinAchievedReplicationFactor(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.ResponseParsergetParser()RequestWritergetRequestWriter()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.StringgetZkHost()ZkStateReadergetZkStateReader()booleanisDirectUpdatesToLeadersOnly()booleanisParallelUpdates()Gets whether direct updates are sent in parallelbooleanisUpdatesToLeaders()voidregisterCollectionStateWatcher(String collection, CollectionStateWatcher watcher)Register a CollectionStateWatcher to be called when the cluster state for a collection changes Note that the watcher is unregistered after it has been called once.NamedList<Object>request(SolrRequest request, String collection)Execute a request against a Solr server for a given collectionprotected NamedList<Object>requestWithRetryOnStaleState(SolrRequest request, int retryCount, List<String> inputCollections)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, List<String> inputCollections)voidsetCollectionCacheTTl(int seconds)Sets the cache ttl for DocCollection Objects cached .voidsetConnectionTimeout(int timeout)Deprecated.since 7.0 UseCloudSolrClient.Buildermethods instead.voidsetDefaultCollection(String collection)Sets the default collection for requestvoidsetIdField(String idField)voidsetParallelCacheRefreshes(int n)If caches are expired they are refreshed after acquiring a lock.voidsetParallelUpdates(boolean parallelUpdates)Deprecated.since 7.2 UseCloudSolrClient.Buildermethods instead.voidsetParser(ResponseParser processor)Note: This setter method is not thread-safe.voidsetRequestWriter(RequestWriter requestWriter)voidsetRetryExpiryTime(int secs)This is the time to wait to refetch the state after getting the same state version from ZKvoidsetSoTimeout(int timeout)Deprecated.since 7.0 UseCloudSolrClient.Buildermethods instead.voidsetZkClientTimeout(int zkClientTimeout)Set the timeout to the zookeeper ensemble in msvoidsetZkConnectTimeout(int zkConnectTimeout)Set the connect timeout to the zookeeper ensemble in msvoidwaitForState(String collection, long wait, TimeUnit unit, CollectionStatePredicate predicate)Block until a collection state matches a predicate, or a timeout Note that the predicate may be called again even after it has returned true, so implementors should avoid changing state within the predicate call itself.- 
Methods inherited from class org.apache.solr.client.solrj.SolrClientadd, add, add, add, add, add, add, add, add, add, addBean, addBean, addBean, addBean, addBeans, addBeans, addBeans, addBeans, addBeans, addBeans, commit, commit, commit, commit, commit, commit, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteByQuery, deleteByQuery, deleteByQuery, deleteByQuery, getBinder, getById, getById, getById, getById, getById, getById, getById, getById, optimize, optimize, optimize, optimize, optimize, optimize, ping, query, query, query, query, queryAndStreamResponse, queryAndStreamResponse, request, rollback, rollback
 
- 
 
- 
- 
- 
Field Detail- 
STATE_VERSIONpublic static final String STATE_VERSION - See Also:
- Constant Field Values
 
 - 
collectionStateCacheprotected final org.apache.solr.client.solrj.impl.CloudSolrClient.StateCache collectionStateCache 
 
- 
 - 
Constructor Detail- 
CloudSolrClientprotected CloudSolrClient(CloudSolrClient.Builder builder) 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:
- builder- a- CloudSolrClient.Builderwith the options used to create the client.
 
 
- 
 - 
Method Detail- 
setRetryExpiryTimepublic void setRetryExpiryTime(int secs) This is the time to wait to refetch the state after getting the same state version from ZKsecs 
 - 
setSoTimeout@Deprecated public void setSoTimeout(int timeout) Deprecated.since 7.0 UseCloudSolrClient.Buildermethods instead.
 - 
setCollectionCacheTTlpublic 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
 
 - 
getParserpublic ResponseParser getParser() 
 - 
setParserpublic 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()
 
 - 
getRequestWriterpublic RequestWriter getRequestWriter() 
 - 
setRequestWriterpublic void setRequestWriter(RequestWriter requestWriter) 
 - 
getZkHostpublic String getZkHost() - Returns:
- the zkHost value used to connect to zookeeper.
 
 - 
getZkStateReaderpublic ZkStateReader getZkStateReader() 
 - 
setIdFieldpublic void setIdField(String idField) - Parameters:
- idField- the field to route documents on.
 
 - 
getIdFieldpublic String getIdField() - Returns:
- the field that updates are routed on.
 
 - 
setDefaultCollectionpublic void setDefaultCollection(String collection) Sets the default collection for request
 - 
getDefaultCollectionpublic String getDefaultCollection() Gets the default collection for request
 - 
setZkConnectTimeoutpublic void setZkConnectTimeout(int zkConnectTimeout) Set the connect timeout to the zookeeper ensemble in ms
 - 
setZkClientTimeoutpublic void setZkClientTimeout(int zkClientTimeout) Set the timeout to the zookeeper ensemble in ms
 - 
isParallelUpdatespublic boolean isParallelUpdates() Gets whether direct updates are sent in parallel
 - 
setParallelUpdates@Deprecated public void setParallelUpdates(boolean parallelUpdates) Deprecated.since 7.2 UseCloudSolrClient.Buildermethods instead.
 - 
connectpublic 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.
 - 
connectpublic void connect(long duration, TimeUnit timeUnit) throws TimeoutException, InterruptedExceptionConnect to a cluster. If the cluster is not ready, retry connection up to a given timeout.- Parameters:
- duration- the timeout
- timeUnit- the units of the timeout
- Throws:
- TimeoutException- if the cluster is not ready after the timeout
- InterruptedException- if the wait is interrupted
 
 - 
waitForStatepublic void waitForState(String collection, long wait, TimeUnit unit, CollectionStatePredicate predicate) throws InterruptedException, TimeoutException Block until a collection state matches a predicate, or a timeout Note that the predicate may be called again even after it has returned true, so implementors should avoid changing state within the predicate call itself.- Parameters:
- collection- the collection to watch
- wait- how long to wait
- unit- the units of the wait parameter
- predicate- a- CollectionStatePredicateto check the collection state
- Throws:
- InterruptedException- on interrupt
- TimeoutException- on timeout
 
 - 
registerCollectionStateWatcherpublic void registerCollectionStateWatcher(String collection, CollectionStateWatcher watcher) Register a CollectionStateWatcher to be called when the cluster state for a collection changes Note that the watcher is unregistered after it has been called once. To make a watcher persistent, it should re-register itself in itsCollectionStateWatcher.onStateChanged(Set, DocCollection)call- Parameters:
- collection- the collection to watch
- watcher- a watcher that will be called when the state changes
 
 - 
condenseResponsepublic CloudSolrClient.RouteResponse condenseResponse(NamedList response, int timeMillis) 
 - 
requestpublic NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException Description copied from class:SolrClientExecute a request against a Solr server for a given collection- Specified by:
- requestin class- SolrClient
- Parameters:
- request- the request to execute
- collection- the collection to execute the request against
- Returns:
- a NamedListcontaining the response from the server
- Throws:
- SolrServerException- if there is an error on the server
- IOException- If there is a low-level I/O error.
 
 - 
requestWithRetryOnStaleStateprotected NamedList<Object> requestWithRetryOnStaleState(SolrRequest request, int retryCount, List<String> inputCollections) 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
 
 - 
sendRequestprotected NamedList<Object> sendRequest(SolrRequest request, List<String> inputCollections) throws SolrServerException, IOException - Throws:
- SolrServerException
- IOException
 
 - 
closepublic void close() throws IOException- Throws:
- IOException
 
 - 
getLbClientpublic LBHttpSolrClient getLbClient() 
 - 
getHttpClientpublic org.apache.http.client.HttpClient getHttpClient() 
 - 
isUpdatesToLeaderspublic boolean isUpdatesToLeaders() 
 - 
isDirectUpdatesToLeadersOnlypublic boolean isDirectUpdatesToLeadersOnly() - Returns:
- true if direct updates are sent to shard leaders only
 
 - 
setParallelCacheRefreshespublic void setParallelCacheRefreshes(int n) If caches are expired they are refreshed after acquiring a lock. use this to set the number of locks
 - 
getDocCollectionprotected DocCollection getDocCollection(String collection, Integer expectedVersion) throws SolrException - Throws:
- SolrException
 
 - 
getMinAchievedReplicationFactorpublic 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.
 - 
getShardReplicationFactorpublic 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.
 - 
setConnectionTimeout@Deprecated public void setConnectionTimeout(int timeout) Deprecated.since 7.0 UseCloudSolrClient.Buildermethods instead.
 - 
getClusterStateProviderpublic ClusterStateProvider getClusterStateProvider() 
 
- 
 
-