Code Documentation

Solr Client

Base and Exceptions

class parasolr.solr.base.ClientBase(session=None)[source]

Base object with common communication methods for talking to Solr API.

Parameters:session (Optional[Session]) – A python-requests requests.Session.
build_url(solr_url, collection, handler)[source]

Return a url to a handler based on core and base url.

Parameters:
  • solr_url (str) – Base url for Solr.
  • collection (str) – Collection or core name.
  • handler (str) – Handler URL for construction.
Return type:

str

Returns:

A full-qualified URL.

make_request(meth, url, headers=None, params=None, data=None, wrap=True, allowed_responses=None, **kwargs)[source]

Make an HTTP request to Solr. May optionally specify a list of allowed HTTP status codes for this request. Responses will be logged as errors if they are not in the list, but only responses with 200 OK status will be loaded as JSON.

Parameters:
  • meth (str) – HTTP method to use.
  • url (str) – URL to make request to.
  • headers (Optional[dict]) – HTTP headers.
  • params (Optional[dict]) – Params to use as form-fields or query-string params.
  • data (Optional[dict]) – Data for a POST request.
  • allowed_responses (Optional[list]) – HTTP status codes that are allowed for this request; if not set, defaults to 200 OK
  • **kwargs – Any other kwargs for the request.
Return type:

Optional[AttrDict]

exception parasolr.solr.base.CoreExists[source]

Raised when a Solr core exists and it should not.

exception parasolr.solr.base.ImproperConfiguration[source]

Raised when a required setting is not present or is an invalid value.

exception parasolr.solr.base.SolrClientException[source]

Base class for all exceptions in this module

Client and Search API

class parasolr.solr.client.ParasolrDict(*args, **kwargs)[source]

A subclass of attrdict.AttrDict that can convert itself to a regular dictionary.

as_dict()[source]

Copy attributes from self as a dictionary, and recursively convert instances of ParasolrDict.

class parasolr.solr.client.QueryResponse(response)[source]

Thin wrapper to give access to Solr select responses.

Parameters:response (Dict) – A Solr query response
_process_facet_counts(facet_counts)[source]

Convert facet_fields and facet_ranges to OrderedDict.

Parameters:facet_counts (AttrDict) – Solr facet_counts field.
Return type:OrderedDict
Returns:Solr facet_counts field
class parasolr.solr.client.SolrClient(solr_url, collection, commitWithin=None, session=None)[source]

Class to aggregate all of the other Solr APIs and settings.

Parameters:
  • solr_url (str) – Base url for solr.
  • collection (str) – Name of Solr collection or core.
  • commitWithin (Optional[int]) – Time in ms for soft commits to happen.
  • session (Optional[Session]) – A python-requests requests.Session.
collection = ''

core or collection name

commitWithin = 1000

commitWithin time in ms

core_admin_handler = 'admin/cores'

CoreAdmin API handler

query(wrap=True, **kwargs)[source]

Perform a query with the specified kwargs.

Parameters:**kwargs – Any valid Solr search parameters.
Return type:Optional[QueryResponse]
Returns:A search QueryResponse.
schema_handler = 'schema'

Schema API handler

select_handler = 'select'

Select handler

Schema API

Module with class and methods for the Solr Schema API.

class parasolr.solr.schema.Schema(solr_url, collection, handler, session=None)[source]

Class for managing Solr Schema API

Parameters:
  • solr_url (str) – Base url for Solr.
  • collection (str) – Name of the collection or core.
  • handler (str) – Handler name for Solr Schema API.
  • session (Optional[Session]) – A python-requests requests.Session.
_post_field(method, **field_kwargs)[source]

Post a field definition to the schema API.

Parameters:
  • method (str) – Solr field method to use.
  • **field_kwargs – Field arguments to use in definition. Any valid schema definition may be used; if passed as kwargs, rather than dict, klass may be used instead of class.
Return type:

None

add_copy_field(source, dest, maxChars=None)[source]

Add a copy field between two existing fields.

Parameters:
  • source (str) – Source Solr field.
  • dest (str) – Destination Solr field.
  • maxChars (Optional[int]) – Maximum characters to copy.
Return type:

None

add_field(**field_kwargs)[source]

Add a field with the supplied definition.

Parameters:**field_kwargs – Any valid Solr field definition values.
Return type:None
add_field_type(**field_kwargs)[source]

Add a field type to a Solr collection or core.

Parameters:**field_kwargs – Any valid Solr field definition values.
Return type:None
Returns:None
delete_copy_field(source, dest)[source]

Delete a Solr copy field.

Parameters:
  • source (str) – Source Solr field.
  • dest (str) – Destination Solr field.
Return type:

None

delete_field(name)[source]

Delete a field with the supplied name.

Parameters:name (str) – Name of field to delete.
Return type:None
delete_field_type(name)[source]

Delete a field type from a Solr collection or core.

Parameters:name (str) – Name of Solr field type to delete.
Return type:None
get_schema()[source]

Get the full schema for a Solr collection or core.

Return type:AttrDict
Returns:Schema as returned by Solr.
list_copy_fields(source_fl=None, dest_fl=None)[source]

Return a list of copy fields from Solr.

Parameters:
  • source_fl (Optional[list]) – Source field to filter by.
  • dest_fl (Optional[list]) – Destination field to filter by.
Return type:

List[AttrDict]

Returns:

list of copy fields as returned by Solr.

list_field_types(showDefaults=True)[source]

List all field types in a Solr collection or core.

Parameters:showDefaults (bool) – Show default fields
Return type:List[AttrDict]
Returns:list of copy fields as returned by Solr.
list_fields(fields=None, includeDynamic=False, showDefaults=False)[source]

Get a list of field definitions for a Solr Collection or core.

Parameters:
  • fields (Optional[list]) – A list of fields to filter by.
  • includeDynamic (bool) – Include Solr dynamic fields in search.
  • showDefaults (bool) – Show default Solr fields.
Return type:

list

Returns:

list of fields as returned by Solr.

replace_field(**field_kwargs)[source]

Replace a field with the supplied definition

Parameters:**field_kwargs – Any valid Solr field definition values; must be a full redefinition, not a partial update.
Return type:None
replace_field_type(**field_kwargs)[source]

Replace a field type from a Solr collection or core.

Parameters:**field_kwargs – Any valid Solr field definition values, but must be a full redefinition, not a partial update.
Return type:None

Update API

class parasolr.solr.update.Update(solr_url, collection, handler, commitWithin, session=None)[source]

API client for Solr update functionality.

Parameters:
_delete(del_obj)[source]

Private method to pass a delete object to the update handler.

Parameters:del_obj (Union[dict, list]) – Object to be serialized into valid JSON for Solr delete.
Return type:None
delete_by_id(id_list)[source]

Delete documents by id field.

Parameters:id_list (list) – A list of ids.
Return type:None
delete_by_query(query)[source]

Delete documents by an arbitrary search query.

Parameters:query (str) – Any valid Solr query.
Return type:None
index(docs, commit=False, commitWithin=None)[source]

Index a document or documents, by default with a soft commit.

Parameters:
  • docs (list) – list of dict objects to index.
  • commit (bool, optional) – Whether or not to make a hard commit to the index.
  • commitWithin (int, optional) – Override default commitWithin for soft commits.
Return type:

None

Core Admin API

class parasolr.solr.admin.CoreAdmin(solr_url, handler, session=None)[source]

API client for Solr core admin.

Parameters:
  • solr_url (str) – Base url for Solr.
  • handler (str) – Handler for CoreAdmin API
  • session (Optional[Session]) – A python-requests requests.Session
create(name, **kwargs)[source]

Create a new core and register it.

Parameters:
  • name (str) – Name of core to create.
  • **kwargs – Any valid parameter for core creation in Solr.
Return type:

None

ping(core)[source]

Ping a core to check status.

Parameters:core (str) – Name of core to ping.
Return type:bool
Returns:True if core status is OK, otherwise False.
reload(core)[source]

Reload a Solr Core.

Parameters:core (str) – Name of core to reload.
Return type:None
status(core=None)[source]

Get the status of all cores or one core.

Parameters:core (Optional[str]) – Name of core to get status.
Return type:Optional[AttrDict]
unload(core, **kwargs)[source]

Unload a core, without defaults to remove data dir or index.

Parameters:
  • core (str) – Name of core to unload.
  • **kwargs – Any valid parameter for core unload in Solr.
Return type:

None

Schema

Solr schema configuration and management.

Extend SolrSchema for your project and configure the fields, field types, and copy fields you want defined in Solr. Fields should be defined using SolrField and field types with SolrAnalyzer and SolrFieldType. For example:

from parasolr import schema

class MySolrSchema(schema.SolrSchema):
    '''Project Solr schema configuration'''

    # field declarations
    author = schema.SolrField('text_en')
    author_exact = schema.SolrStringField()
    title = schema.SolrField('text_en')
    title_nostem = schema.SolrStringField()
    subtitle = schema.SolrField('text_en')
    collections = schema.SolrField('text_en', multivalued=True)

    #: copy fields, for facets and variant search options
    copy_fields = {
        'author': 'author_exact',
        'collections': 'collections_s',
        'title': ['title_nostem', 'title_s'],
        'subtitle': 'subtitle_s',
    }

Copy fields should be a dictionary of source and destination fields; both single value and list are supported for destination.

If you want to define a custom field type, you can define an analyzer for use in one or more field type declarations:

class UnicodeTextAnalyzer(schema.SolrAnalyzer):
    '''Solr text field analyzer with unicode folding. Includes all standard
    text field analyzers (stopword filters, lower case, possessive, keyword
    marker, porter stemming) and adds ICU folding filter factory.
    '''
    tokenizer = 'solr.StandardTokenizerFactory'
    filters = [
        {"class": "solr.StopFilterFactory", "ignoreCase": True,
         "words": "lang/stopwords_en.txt"},
        {"class": "solr.LowerCaseFilterFactory"},
        {"class": "solr.EnglishPossessiveFilterFactory"},
        {"class": "solr.KeywordMarkerFilterFactory"},
        {"class": "solr.PorterStemFilterFactory"},
        {"class": "solr.ICUFoldingFilterFactory"},
    ]


class SolrTextField(schema.SolrTypedField):
    field_type = 'text_en'

class MySolrSchema(schema.SolrSchema):
    '''Schema configuration with custom field types'''

    text_en = schema.SolrFieldType('solr.TextField',
                               analyzer=UnicodeFoldingTextAnalyzer)

    content = SolrTextField()

To update your configured solr core with your schema, run:

python manage.py solr_schema

This will automatically find your SolrSchema subclass and apply changes. See solr_schema manage command documentation for more details.


class parasolr.schema.SolrAnalyzer[source]

Class to declare a solr field analyzer with tokenizer and filters, for use with SolrFieldType.

classmethod as_solr_config()[source]
filters = None

list of the filters to apply

tokenizer = None

string name of the tokenizer to use

class parasolr.schema.SolrField(fieldtype, required=False, multivalued=False)[source]

A descriptor for declaring a solr field on a SolrSchema instance.

Parameters:
  • fieldtype (str) – The type of Solr field.
  • required (bool) – Whether the field is required.
  • multivalues – Whether the field is multi-valued.
Raises:

AttributeError – If __set__ is called.

class parasolr.schema.SolrFieldType(field_class, analyzer)[source]

A descriptor for declaring and configure a solr field type on

Parameters:
  • field_class (str) – The class of the SolrField
  • analyzer (str) – The name of the Solr analyzer to use on the field.
Raises:

AttributeError – If __set__ is called.

class parasolr.schema.SolrSchema[source]

Solr schema configuration.

classmethod configure_copy_fields(solr)[source]

Update configured Solr instance schema with copy fields.

Parameters:solr (SolrClient) – Configured Solr Schema.
Return type:None
classmethod configure_fields(solr)[source]

Update the configured Solr instance schema to match the configured fields.

Calls configure_copy_fields() after new fields have been created and before old fields are removed, since an outdated copy field could prevent removal.

Parameters:solr (SolrClient) – A configured Solr instance schem.
Return type:AttrDefault
Returns:attrdict.AttrDefault with counts for added, updated, and deleted fields.
classmethod configure_fieldtypes(solr)[source]

Update the configured Solr instance so the schema includes the configured field types, if any.

Parameters:solr (SolrClient) – A configured Solr instance.
Return type:AttrDefault
Returns:attrdict.AttrDefault with counts for updated and added field types.
copy_fields = {}

dictionary of copy fields to be configured key is source field, value is destination field or list of fields

classmethod get_configuration()[source]

Find a SolrSchema subclass for use as schema configuration. Currently only supports one schema configuration.

classmethod get_field_names()[source]

iterate over class attributes and return all that are instances of SolrField.

Return type:list
Returns:List of attributes that are SolrField.
classmethod get_field_types()[source]

iterate over class attributes and return all that are instances of SolrFieldType.

Return type:list
Returns:List of attriubtes that are SolrFieldType.
class parasolr.schema.SolrStringField(*args, **kwargs)[source]

Solr string field.

class parasolr.schema.SolrTypedField(*args, **kwargs)[source]

Base class for typed solr field descriptor. For use with your own field types, extend and set field_type.

Parameters:
  • *args – Arguments as passsed to SolrField.
  • **kwargs – Keyword arguments as passed to SolrField.

Indexing

Model-based indexing with Solr.

Items to be indexed in Solr should extend Indexable. The default implementation should work for most Django models; at a minimum you should extend Indexable.index_data() to include the information to be indexed in Solr. You may also customize Indexable.index_item_type() and Indexable.index_item_id().

To manually index content in Solr, see index manage command documentation.


class parasolr.indexing.Indexable[source]

Mixin for objects that are indexed in Solr. Subclasses must implement index_id and index methods.

ID_SEPARATOR = '.'

id separator for auto-generated index ids

classmethod all_indexables()[source]

Find all Indexable subclasses for indexing. Ignore abstract Indexable subclasses such as ModelIndexable.

index()[source]

Index the current object in Solr. Allows passing in parameter, e.g. to set a commitWithin value.

index_chunk_size = 150

number of items to index at once when indexing a large number of items

index_data()[source]

Dictionary of data to index in Solr for this item. Default implementation adds index_id() and index_item_type()

index_id()[source]

Solr identifier. By default, combines index item_type() and id with :attr:ID_SEPARATOR`.

classmethod index_item_type()[source]

Label for this kind of indexable item. Must be unique across all Indexable items in an application. By default, uses Django model verbose name. Used in default index id and in index manage command.

classmethod index_items(items, progbar=None)[source]

Indexable class method to index multiple items at once. Takes a list, queryset, or generator of Indexable items or dictionaries. Items are indexed in chunks, based on Indexable.index_chunk_size.

Parameters:
  • items – list, queryset, or generator of indexable objects or dictionaries
  • progbar – optional progressbar.Progressbar object to
  • when indexing items in chunks. (update) –
Returns:

Total number of items indexed

classmethod items_to_index()[source]

Get all items to be indexed for a single class of Indexable content. Subclasses can override this method to return a custom iterable, e.g. a Django QuerySet that takes advantage of prefetching. By default, returns all Django objects for a model. Raises NotImplementedError if that fails.

remove_from_index()[source]

Remove the current object from Solr by identifier using index_id()

solr = None

solr connection

parasolr.indexing.all_subclasses(cls)[source]

recursive method to find all subclasses

QuerySet

Object-oriented approach to Solr searching and filtering modeled on django.models.queryset.QuerySet. Supports iteration, slicing, counting, and boolean check to see if a search has results.

Filter, search and sort methods return a new queryset, and can be chained. For example:

SolrQuerySet(solrclient).filter(item_type='person')                             .search(name='hem*')                             .order_by('sort_name')

If you are working with Django you should use parasolr.django.SolrQuerySet, which will automatically initialize a new parasolr.django.SolrClient if one is not passed in.

class parasolr.query.queryset.EmptySolrQuerySet(*args, **kwargs)[source]

Marker class that can be used to check if a given queryset is empty via isinstance():

assert isinstance(SolrQuerySet().none(), EmptySolrQuerySet) -> True
assert isinstance(queryset, EmptySolrQuerySet) # True if empty
class parasolr.query.queryset.SolrQuerySet(solr)[source]

A Solr queryset object that allows for object oriented searching and filtering of Solr results. Allows search results to be pagination using slicing, count, and iteration.

ANY_VALUE = '[* TO *]'

any value constant

LOOKUP_SEP = '__'

lookup separator

_clone()[source]

Return a copy of the current QuerySet for modification via filters.

Return type:SolrQuerySet
static _lookup_to_filter(key, value, tag='')[source]

Convert keyword/value argument, with optional lookups separated by __, including: in and exists. Field names should NOT include double-underscores by convention. Accepts an optional tag argument to specify an exclude tag as needed.

Returns: A propertly formatted Solr query string.
Return type:str
_set_faceting_opts(query_opts)[source]

Configure faceting attributes directly on query_opts. Modifies dictionary directly.

Return type:None
_set_highlighting_opts(query_opts)[source]

Configure highlighting attributes on query_opts. Modifies dictionary directly.

Return type:None
_set_stats_opts(query_opts)[source]

Configure stats attributes directly on query_opts. Modifies dictionary directly.

Return type:None
all()[source]

Return a new queryset that is a copy of the current one.

Return type:SolrQuerySet
also(*args, **kwargs)[source]

Use field limit option to return the specified fields, optionally provide aliases for them in the return. Works exactly the same way as only() except that it does not any previously specified field limits.

Return type:SolrQuerySet
count()[source]

Total number of results for the current query

Return type:int
default_search_operator = 'AND'

by default, combine search queries with AND

facet(*args, **kwargs)[source]

Request facets for specified fields. Returns a new SolrQuerySet with Solr faceting enabled and facet.field parameter set. Does not support ranged faceting.

Subsequent calls will reset the facet.field to the last set of args in the chain.

For example:

qs = queryset.facet('person_type', 'age')
qs = qs.facet('item_type')

would result in item_type being the only facet field.

Return type:SolrQuerySet
facet_field(field, exclude='', **kwargs)[source]

Request faceting for a single field. Returns a new SolrQuerySet with Solr faceting enabled and the field added to the list of facet fields. Any keyword arguments will be set as field-specific facet configurations.

ex will specify a related filter query tag to exclude when generating counts for the facet.

Return type:SolrQuerySet
facet_range(field, **kwargs)[source]

Request range faceting for a single field. Returns a new SolrQuerySet with Solr range faceting enabled and the field added to the list of facet fields. Keyword arguments such as start, end, and gap will be set as field-specific facet configurations.

Return type:SolrQuerySet
filter(*args, tag='', **kwargs)[source]

Return a new SolrQuerySet with Solr filter queries added. Multiple filters can be combined either in a single method call, or they can be chained for the same effect. For example:

queryset.filter(item_type='person').filter(birth_year=1900)
queryset.filter(item_type='person', birth_year=1900)

A tag may be specified for the filter to be used with facet.field exclusions:

queryset.filter(item_type='person', tag='person')

To provide a filter that should be used unmodified, provide the exact string of your filter query:

queryset.filter('birth_year:[1800 TO *]')

You can also search for pre-defined using lookups on a field, for example:

queryset.filter(item_type__in=['person', 'book'])
queryset.filter(item_type__exists=False)

Currently supported field lookups:

  • in : takes a list of values; supports ‘’ or None to match on field not set
  • exists: boolean filter to look for any value / no value
  • range: range query. Takes a list or tuple of two values
    for the start and end of the range. Either value can be unset for an open-ended range (e.g. year__range=(1800, None))
Return type:SolrQuerySet
get_facets()[source]

Return a dictionary of facet information included in the Solr response. Includes facet fields, facet ranges, etc. Facet field results are returned as an ordered dict of value and count.

Return type:Dict[str, Dict]
get_highlighting()[source]

Return the highlighting portion of the Solr response.

get_results(**kwargs)[source]

Query Solr and get the results for the current query and filter options. Populates result cache and returns the documents portion of the reponse.

Return type:List[dict]
Returns:Solr response documents as a list of dictionaries.
get_stats()[source]

Return a dictionary of stats information in Solr format or None on error.

Return type:Optional[Dict[str, ParasolrDict]]
highlight(field, **kwargs)[source]

“Configure highlighting. Takes arbitrary Solr highlight parameters and adds the hl. prefix to them. Example use:

queryset.highlight('content', snippets=3, method='unified')
Return type:SolrQuerySet
none()[source]

Return an empty result list.

Return type:SolrQuerySet
only(*args, replace=True, **kwargs)[source]

Use field limit option to return only the specified fields. Optionally provide aliases for them in the return. Subsequent calls will replace any previous field limits. Example:

queryset.only('title', 'author', 'date')
queryset.only('title:title_t', 'date:pubyear_i')
Return type:SolrQuerySet
order_by(*args)[source]

Apply sort options to the queryset by field name. If the field name starts with -, sort is descending; otherwise ascending.

Return type:SolrQuerySet
query(**kwargs)[source]

Return a new SolrQuerySet with the results populated from Solr. Any options passed in via keyword arguments take precedence over query options on the queryset.

Return type:SolrQuerySet
query_opts()[source]

Construct query options based on current queryset configuration. Includes filter queries, start and rows, sort, and search query.

Return type:Dict[str, str]
raw_query_parameters(**kwargs)[source]

Add abritrary raw parameters to be included in the query request, e.g. for variables referenced in join or field queries. Analogous to the input of the same name in the Solr web interface.

Return type:SolrQuerySet
search(*args, **kwargs)[source]

Return a new SolrQuerySet with search queries added. All queries will combined with the default search operator when constructing the q parameter sent to Solr..

Return type:SolrQuerySet
set_limits(start, stop)[source]

Set limits to get a subsection of the results, to support slicing.

stats(*args, **kwargs)[source]

Request stats for specified fields. Returns a new SolrQuerySet with Solr faceting enabled and stats.field parameter set.

Subsequent calls will reset the stats.field to the last set of args in the chain.

For example:

qs = queryset.stats('person_type', 'age')
qs = qs.stats('account_start_i')

would result in account_start_i being the only facet field.

Any kwargs will be prepended with stats.. You may also pass local parameters along with field names, i.e. {!ex=filterA}account_start_i.

Return type:SolrQuerySet
class parasolr.query.aliased_queryset.AliasedSolrQuerySet(*args, **kwargs)[source]

Extension of SolrQuerySet with support for aliasing Solr fields to more readable versions for use in code. To use, extend this class and define a dictionary of field_aliases with the same syntax you would when calling only(). Those field aliases will be set as the default initial value for field_list, and aliases can be used in all extended methods.

_unalias_args(*args)[source]

convert alias name to solr field for list of args

_unalias_kwargs(**kwargs)[source]

convert alias name to solr field for keys in kwargs

_unalias_kwargs_with_lookups(**kwargs)[source]

convert alias name to solr field for keys in kwargs with support for __ lookups for filters

facet(*args, **kwargs)[source]

Extend parasolr.query.queryset.SolrQuerySet.facet() to support using aliased field names in args.

Return type:AliasedSolrQuerySet
facet_field(field, exclude='', **kwargs)[source]

Extend parasolr.query.queryset.SolrQuerySet.facet_field`() to support using aliased field names for field parameter.

Return type:AliasedSolrQuerySet
field_aliases = {}

map of application-specific, readable field names to actual solr fields (i.e. if using dynamic field types)

filter(*args, tag='', **kwargs)[source]

Extend parasolr.query.queryset.SolrQuerySet.filter() to support using aliased field names for keyword argument keys.

Return type:AliasedSolrQuerySet
get_facets()[source]

Extend parasolr.query.queryset.SolrQuerySet.get_facets() to use aliased field names for facet and range facet keys.

Return type:Dict[str, int]
get_stats()[source]

Extend parasolr.query.queryset.SolrQuerySet.get_stats() to return return aliased field names for field_list keys.

Return type:Dict[str, Dict]
highlight(field, **kwargs)[source]

Extend parasolr.query.queryset.SolrQuerySet.highlight() to support using aliased field names in kwargs.

Return type:AliasedSolrQuerySet
only(*args, **kwargs)[source]

Extend parasolr.query.queryset.SolrQuerySet.only`() to support using aliased field names for args (but not kwargs).

Return type:AliasedSolrQuerySet
order_by(*args)[source]

Extend parasolr.query.queryset.SolrQuerySet.order_by`() to support using aliased field names in sort arguments.

Return type:AliasedSolrQuerySet
stats(*args, **kwargs)[source]

Extend parasolr.query.queryset.SolrQuerySet.stats() to support using aliased field names in args.

Return type:AliasedSolrQuerySet

Django

Indexing

This module provides indexing support for Django models. Also see Indexable.

To use, add ModelIndexable as a mixin to the model class you want to be indexed. At minimum, you’ll want to extend the index_data method to include the data you want in the indexed:

def index_data(self):
    index_data = super().index_data()

    # if there are some records that should not be included
    # return id only. This will blank out any previously indexed
    # values, and item will not be findable by type.
    # if not ...
        # del index_data['item_type']
        # return index_data

    # add values to index data
    index_data.update({
        ...
    })
    return index_data

You can optionally extend items_to_index() and index_item_type().


class parasolr.django.indexing.ModelIndexable[source]

Find a related model for use in signal-based indexing. Supports app.Model notation or attribute on the current model (supports queryset syntax for attributes on related models.)

classmethod identify_index_dependencies()[source]

Identify and set lists of index dependencies for the subclass of Indexable.

QuerySet

Provides SolrQuerySet subclasses that will automatically use SolrClient if no solr client is passed on.

class parasolr.django.queryset.AliasedSolrQuerySet(solr=None)[source]

Combination of SolrQuerySet and AliasedSolrQuerySet

class parasolr.django.queryset.SolrQuerySet(solr=None)[source]

SolrQuerySet subclass that will automatically use SolrClient if no solr client is passed on.

:param Optional parasolr.solr.client.SolrClient:

Signals

This module provides on-demand reindexing of Django models when they change, based on Django signals. To use this signal handler, import import it in the ready method of a django app. This will automatically bind connect any configured signal handlers:

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        # import and connect signal handlers for Solr indexing
        from parasolr.django.signals import IndexableSignalHandler

To configure index dependencies, add a property on any ModelIndexable subclass with the dependencies and signals that should trigger reindexing. Example:

class MyModel(Model, ModelIndexable):

    index_depends_on = {
        'collections': {
            'post_save': signal_method,
            'pre_delete': signal_method
        }
    }

The keys of the dependency dict can be:

  • an attribute on the indexable model (i.e., the name of a many-to-many relationship); this will bind an additional signal handler on the m2m relationship change.
  • an attribute on a related model using django queryset notation (use this for a secondary many-to many relationship, e.g. collections__authors)
  • a string with the model name in app.ModelName notation, to find and load a model directly

The dictionaries for each related model or attribute should contain:

Currently attribute lookup only supports many-to-many and reverse many-to-many relationships.

Typically you will want to bind post_save and pre_delete for many-to-many relationships.

class parasolr.django.signals.IndexableSignalHandler[source]

Signal handler for indexing Django model-based indexables. Automatically identifies and binds handlers based on configured index dependencies on indexable objects..

static connect()[source]

bind indexing signal handlers to save and delete signals for Indexable subclassess and any indexing dependencies

static disconnect()[source]

disconnect indexing signal handlers

static handle_delete(sender, instance, **kwargs)[source]

remove from index on delete if an instance of ModelIndexable

static handle_relation_change(sender, instance, action, **kwargs)[source]

index on add, remove, and clear for ModelIndexable instances

static handle_save(sender, instance, **kwargs)[source]

reindex on save if an instance of ModelIndexable

Manage Commands

Solr schema

solr_schema is a custom manage command to update the configured schema definition for the configured Solr instance. Reports on the number of fields that are added or updated, and any that are out of date and were removed.

Example usage:

python manage.py solr_schema

Index

index is a custom manage command to index content into Solr. It should only be run after your schema has been configured via solr_schema.

By default, indexes _all_ indexable content.

You can optionally index specific items by type or by index id. Default index types are generated based on model verbose names.

A progress bar will be displayed by default if there are more than 5 items to process. This can be suppressed via script options.

You may optionally request the index or part of the index to be cleared before indexing, for use when index data has changed sufficiently that previous versions need to be removed.

Example usage:

# index everything
python manage.py index
# index specific items
python manage.py index person:1 person:1 location:2
# index one kind of item only
python manage.py index -i person
# suppress progressbar
python manage.py index --no-progress
# clear everything, then index everything
python manage.py index --clear all
# clear and then index one kind of item
python manage.py index --clear person --index person
# clear everything, index nothing
python manage.py index --clear all --index none