Classes

Solr class

class mysolr.Solr(base_url='http://localhost:8080/solr/')

Acts as an easy-to-use interface to Solr.

Asynchronous search using async module from requests.

Parameters:
  • queries – List of queries. Each query is a dictionary containing any of the available Solr query parameters described in http://wiki.apache.org/solr/CommonQueryParameters. ‘q’ is a mandatory parameter.
  • size – Size of threadpool
  • resource – Request dispatcher. ‘select’ by default.
commit(wait_flush=True, wait_searcher=True, expunge_deletes=False)

Sends a commit message to Solr.

Parameters:
  • wait_flush – Block until index changes are flushed to disk (default is True).
  • wait_searcher – Block until a new searcher is opened and registered as the main query searcher, making the changes visible (default is True).
  • expunge_deletes – Merge segments with deletes away (default is False)
delete_by_key(identifier, commit=True)

Sends an ID delete message to Solr.

Parameters:commit – If True, sends a commit message after the operation is executed.
delete_by_query(query, commit=True)

Sends a query delete message to Solr.

Parameters:commit – If True, sends a commit message after the operation is executed.
is_up()

Check if a Solr server is up using ping call

more_like_this(resource='mlt', text=None, **kwargs)

Implements convenient access to Solr MoreLikeThis functionality

Please, visit http://wiki.apache.org/solr/MoreLikeThis to learn more about MLT configuration and common parameters.

There are two ways of using MLT in Solr:

Using a previously configured RequestHandler
You normally specify a query and the first matching document for that query is used to retrieve similar documents. You can however specify a text instead of a query, and similar documents to the text will be returned. You must configure a MLT RequestHandler in your solrconfig.xml in order to get advantage of this functionality. Note that this method has a default resource name with value “mlt”, but if your RequestHandler has a different name you must specify it when calling the more_like_this method.
Using the MLT Search Component:
The resulting documents in this case will be those that match the regular query, but the SolrResponse will have a “mlt” section where similar documents for each result document will be given.
Parameters:
  • resource – Request dispatcher. ‘ml’ by default.
  • text – Text to use for similar documents retrieval. None by default.
  • **kwargs

    Dictionary containing any of the available Solr query parameters described in http://wiki.apache.org/solr/CommonQueryParameters or MoreLikeThis Common parameters described in http://wiki.apache.org/solr/MoreLikeThis. ‘q’ is a mandatory parameter in all cases except when using a MLT RequestHandler with a Text parameter.

optimize(wait_flush=True, wait_searcher=True, max_segments=1)

Sends an optimize message to Solr.

Parameters:
  • wait_flush – Block until index changes are flushed to disk (default is True)
  • wait_searcher – Block until a new searcher is opened and registered as the main query searcher, making the changes visible (default is True)
  • max_segments – Optimizes down to at most this number of segments (default is 1)
ping()

Ping call to solr server.

rollback()

Sends a rollback message to Solr server.

search(resource='select', **kwargs)

Queries Solr with the given kwargs and returns a SolrResponse object.

Parameters:
search_cursor(resource='select', **kwargs)
update(documents, input_type='json', commit=True)

Sends an update/add message to add the array of hashes(documents) to Solr.

Parameters:
  • documents – A list of solr-compatible documents to index. You should use unicode strings for text/string fields.
  • input_type – The format which documents are sent. Remember that json is not supported until version 3.
  • commit – If True, sends a commit message after the operation is executed.

SolrResponse class

class mysolr.SolrResponse(http_response=None)

Parse solr response and make it accesible.

extract_errmessage()

Tries to extract an error message from a SolrResponse body content.

Useful for error identification (e.g.: indexation errors)

parse_content()

Tries to parse the raw content to know if its a structured results response or an unstructured HTML page (usually resulting from an error)

parse_facets(solr_facets)

Parse facets.

parse_spellcheck(solr_suggestions)

Parse spellcheck result into a more readable format.

Cursor class

class mysolr.Cursor(url, query)

Implements the concept of cursor in relational databases

fetch(rows=None)

Generator method that grabs all the documents in bulk sets of ‘rows’ documents

Parameters:rows – number of rows for each request

Fork me on GitHub