Lightweight Python client for Apache Solr
—
Core administration capabilities for managing Solr cores including creation, status monitoring, reloading, renaming, swapping, and unloading operations.
Administrative client for managing Solr cores through the Core Admin API.
class SolrCoreAdmin:
def __init__(self, url, *args, **kwargs):
"""
Initialize a Solr Core Admin client.
Parameters:
- url (str): Full URL to Solr admin cores endpoint (e.g., 'http://localhost:8983/solr/admin/cores')
- *args, **kwargs: Additional arguments passed to parent class
"""Usage:
import pysolr
# Create admin client
admin = pysolr.SolrCoreAdmin('http://localhost:8983/solr/admin/cores')Get status information for one or all Solr cores including configuration, statistics, and health information.
def status(self, core=None):
"""
Get core status information.
Parameters:
- core (str, optional): Specific core name to get status for. If None, returns status for all cores
Returns:
str: XML response containing core status information
Raises:
SolrError: If status request fails
"""Usage:
# Get status for all cores
all_status = admin.status()
print("All cores status:", all_status)
# Get status for specific core
core_status = admin.status(core='my_core')
print("Core status:", core_status)Create new Solr cores with specified configuration and schema files.
def create(self, name, instance_dir=None, config="solrconfig.xml", schema="schema.xml"):
"""
Create a new Solr core.
Parameters:
- name (str): Name for the new core
- instance_dir (str, optional): Instance directory path. Defaults to core name if not specified
- config (str): Configuration file name (default: "solrconfig.xml")
- schema (str): Schema file name (default: "schema.xml")
Returns:
str: XML response from core creation
Raises:
SolrError: If core creation fails
"""Usage:
# Create core with default settings
response = admin.create('new_core')
print("Core created:", response)
# Create core with custom configuration
response = admin.create(
name='custom_core',
instance_dir='/path/to/core/instance',
config='custom-solrconfig.xml',
schema='custom-schema.xml'
)
print("Custom core created:", response)Reload Solr cores to apply configuration changes without restarting the server.
def reload(self, core):
"""
Reload a Solr core to apply configuration changes.
Parameters:
- core (str): Name of the core to reload
Returns:
str: XML response from core reload
Raises:
SolrError: If core reload fails
"""Usage:
# Reload a core after configuration changes
response = admin.reload('my_core')
print("Core reloaded:", response)Rename existing Solr cores to new names.
def rename(self, core, other):
"""
Rename a Solr core.
Parameters:
- core (str): Current name of the core
- other (str): New name for the core
Returns:
str: XML response from core rename
Raises:
SolrError: If core rename fails
"""Usage:
# Rename a core
response = admin.rename('old_core_name', 'new_core_name')
print("Core renamed:", response)Swap the names of two existing Solr cores, effectively switching their identities.
def swap(self, core, other):
"""
Swap two Solr cores (exchange their names).
Parameters:
- core (str): Name of the first core
- other (str): Name of the second core
Returns:
str: XML response from core swap
Raises:
SolrError: If core swap fails
"""Usage:
# Swap two cores (useful for blue-green deployments)
response = admin.swap('core_blue', 'core_green')
print("Cores swapped:", response)Unload Solr cores from memory while preserving their data and configuration on disk.
def unload(self, core):
"""
Unload a Solr core from memory.
Parameters:
- core (str): Name of the core to unload
Returns:
str: XML response from core unload
Raises:
SolrError: If core unload fails
"""Usage:
# Unload a core from memory
response = admin.unload('unused_core')
print("Core unloaded:", response)Some operations are not available in certain Solr versions.
def load(self, core):
"""
Load a Solr core (not implemented).
Parameters:
- core (str): Name of the core to load
Raises:
NotImplementedError: Always raised as this operation is not supported in Solr 1.4 and below
"""import pysolr
# Initialize admin client
admin = pysolr.SolrCoreAdmin('http://localhost:8983/solr/admin/cores')
try:
# Check current status
print("Checking current cores...")
status = admin.status()
print("Current status:", status)
# Create a new core
print("Creating new core...")
create_response = admin.create(
name='test_core',
config='solrconfig.xml',
schema='managed-schema'
)
print("Core created successfully")
# Verify core was created
core_status = admin.status(core='test_core')
print("New core status:", core_status)
# Reload core (e.g., after configuration changes)
print("Reloading core...")
reload_response = admin.reload('test_core')
print("Core reloaded successfully")
# Create another core for swapping demo
admin.create('test_core_2')
# Swap cores
print("Swapping cores...")
swap_response = admin.swap('test_core', 'test_core_2')
print("Cores swapped successfully")
# Unload cores when done
print("Cleaning up...")
admin.unload('test_core')
admin.unload('test_core_2')
print("Cores unloaded successfully")
except pysolr.SolrError as e:
print(f"Admin operation failed: {e}")Administrative operations can fail for various reasons and should include proper error handling:
import pysolr
admin = pysolr.SolrCoreAdmin('http://localhost:8983/solr/admin/cores')
try:
# Attempt to create a core
admin.create('my_core')
except pysolr.SolrError as e:
if 'already exists' in str(e).lower():
print("Core already exists, skipping creation")
else:
print(f"Failed to create core: {e}")
raise
try:
# Attempt to reload a core
admin.reload('nonexistent_core')
except pysolr.SolrError as e:
print(f"Core reload failed: {e}")
# Handle missing core or configuration errors
try:
# Attempt unsupported operation
admin.load('some_core')
except NotImplementedError as e:
print(f"Operation not supported: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-pysolr