PySolr
PySolr is a lightweight Python client for Apache Solr, providing an interface for querying, indexing, and managing data in Solr. It is actively maintained with version 3.11.0 being the latest stable release, and typically sees 1-3 major releases per year, though the cadence can be irregular.
Warnings
- breaking PySolr has progressively dropped support for older Python versions. As of v3.9.0, Python 3.4 support was dropped. With v3.11.0, support for Python 3.9 and below has been explicitly removed. PySolr now strictly requires Python >=3.10.
- breaking The default commit behavior for add/delete operations changed significantly in v3.8.1. Previously, these operations would implicitly commit changes to Solr. From v3.8.1 onwards, `always_commit` defaults to `False` for performance. This means changes are buffered and not immediately searchable unless explicitly committed.
- gotcha PySolr v3.9.0 introduced a JSON interface which can offer significantly faster data loading (up to 15-20 times faster) compared to the older XML-based conversion. Users may inadvertently stick to less performant methods.
- gotcha When using custom Solr request handlers (often with `use_qt_param=True`), the handler name specified in the PySolr URL must precisely match the configuration in `solrconfig.xml`, including any leading slashes (e.g., `/update`). If `use_qt_param` is `False` (the default), leading/trailing slashes can be omitted.
- deprecated As part of the shift to Python 3, PySolr v3.11.0 explicitly removed the Python 2 tag from its distribution metadata. While Python 2 compatibility was waning in earlier 3.x releases, this action definitively signals the end of any remaining support. Attempting to use newer PySolr versions with Python 2 tooling or environments may lead to installation failures or runtime errors.
Install
-
pip install pysolr
Imports
- Solr
import pysolr solr = pysolr.Solr('http://localhost:8983/solr/')
Quickstart
import os
import pysolr
solr_url = os.environ.get('SOLR_URL', 'http://localhost:8983/solr/')
try:
# Connect to Solr with explicit commit behavior
solr = pysolr.Solr(solr_url, always_commit=True, timeout=10)
# Do a health check
solr.ping()
print(f"Successfully connected to Solr at {solr_url}")
# Index some data
docs_to_add = [
{"id": "doc_1", "title": "The Quick Brown Fox"},
{"id": "doc_2", "title": "Jumps Over The Lazy Dog"},
]
solr.add(docs_to_add)
print("Added documents.")
# Search for data
results = solr.search('fox')
print(f"Found {len(results)} result(s) for 'fox':")
for result in results:
print(f" - ID: {result['id']}, Title: {result['title']}")
# Delete a document
solr.delete(id='doc_1')
print("Deleted doc_1.")
# Search again to confirm deletion
results_after_delete = solr.search('fox')
print(f"Found {len(results_after_delete)} result(s) for 'fox' after deletion.")
except Exception as e:
print(f"An error occurred: {e}")