SolrClient package

This is the primary module for interacting with Solr. All other components are accessed through a SolrClient instance. Basic usage:

>>> from SolrClient import SolrClient
>>> solr = SolrClient('http://localhost:8983/solr') #Solr URL
>>> res = solr.query('SolrClient_unittest',{ #Query params are sent as a python dict
        'q':'product_name:Lorem',
        'facet':True,
        'facet.field':'facet_test',
})
>>> res.get_results_count() #Number of items returned
4
>>> res.get_facets() #If you asked for facets it will give you facets as a python dict
{'facet_test': {'ipsum': 0, 'sit': 0, 'dolor': 2, 'amet,': 1, 'Lorem': 1}}
>>> res.docs #Returns documents as a list
[{'product_name_exact': 'orci. Morbi ipsum ullamcorper, quam', '_version_': 15149272615480197 12, 'facet_test': ['dolor'], 'date': '2015-10-13T14:40:20.492Z', 'id': 'cb666bd1-ab8e-4951-98 29-5ccd4c12d10b', 'price': 10, 'product_name': 'ullamcorper, nulla. Vestibulum Lorem orci,'},  {'product_name_exact': 'enim aliquet orci. sapien, mattis,', '_version_': 151492726156689408 0, 'facet_test': ['dolor'], 'date': '2015-10-13T14:40:20.492Z', 'id': '8cb40255-ea07-4ab2-a30 f-6e843781a043', 'price': 22, 'product_name': 'dui. Lorem ullamcorper, lacus. hendrerit'}, {' product_name_exact': 'arcu In Nunc vel Nunc', '_version_': 1514927261568991234, 'facet_test':  ['Lorem'], 'date': '2015-10-13T14:40:20.493Z', 'id': '287702d2-90b8-4dce-8e66-00a016e51bdd',  'price': 93, 'product_name': 'ipsum vel. Lorem dui. risus'}, {'product_name_exact': 'Vivamus  sem ac dolor neque', '_version_': 1514927261656023040, 'facet_test': ['amet,'], 'date': '201 5-10-13T14:40:20.494Z', 'id': 'f3c396f0-1fc2-4847-a966-1ebe055b8bd7', 'price': 60, 'product_n ame': 'consectetur Mauris dolor Lorem adipiscing'}]

SolrClient.SolrClient module

class SolrClient.SolrClient(host='http://localhost:8983/solr', transport=<class SolrClient.transport.transportrequests.TransportRequests>, devel=False, auth=None, log=None, **kwargs)

Bases: object

Creates a new SolrClient.

Parameters:
  • host – Specifies the location of Solr Server. ex ‘http://localhost:8983/solr’. Can also take a list of host values in which case it will use the first server specified, but will switch over to the second one if the first one is not available.
  • transport – Transport class to use. So far only requests is supported.
  • devel (bool) – Can be turned on during development or debugging for a much greater logging. Requires logging to be configured with DEBUG level.
commit(collection, openSearcher=False, softCommit=False, waitSearcher=True, commit=True, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • openSearcher (bool) – If new searcher is to be opened
  • softCommit (bool) – SoftCommit
  • waitServer (bool) – Blocks until the new searcher is opened
  • commit (bool) – Commit

Sends a commit to a Solr collection.

cursor_query(collection, query)
Parameters:
  • collection (str) – The name of the collection for the request.
  • query (dict) – Dictionary of solr args.

Will page through the result set in increments using cursorMark until it has all items. Sort is required for cursorMark queries, if you don’t specify it, the default is ‘id desc’.

Returns an iterator of SolrResponse objects. For Example:

>>> for res in solr.cursor_query('SolrClient_unittest',{'q':'*:*'}):
        print(res)
delete_doc_by_id(collection, doc_id, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • id (str) – ID of the document to be deleted. Can specify ‘*’ to delete everything.

Deletes items from Solr based on the ID.

>>> solr.delete_doc_by_id('SolrClient_unittest','changeme')
delete_doc_by_query(collection, query, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • query (str) – Query selecting documents to be deleted.

Deletes items from Solr based on a given query.

>>> solr.delete_doc_by_query('SolrClient_unittest','*:*')
get(collection, doc_id, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • doc_id (str) – ID of the document to be retrieved.

Retrieve document from Solr based on the ID.

>>> solr.get('SolrClient_unittest','changeme')
get_zk()
index(collection, docs, params=None, min_rf=None, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request.
  • list docs (docs) – List of dicts. ex: [{“title”: “testing solr indexing”, “id”: “test1”}]
  • int min_rf (min_rf) – Required number of replicas to write to’

Sends supplied list of dicts to solr for indexing.

>>> docs = [{'id':'changeme','field1':'value1'}, {'id':'changeme1','field2':'value2'}]
>>> solr.index('SolrClient_unittest', docs)
index_json(collection, data, params=None, min_rf=None, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request.
  • str data (data) – Valid Solr JSON as a string. ex: ‘[{“title”: “testing solr indexing”, “id”: “test1”}]’
  • int min_rf (min_rf) – Required number of replicas to write to’

Sends supplied json to solr for indexing, supplied JSON must be a list of dictionaries.

>>> docs = [{'id':'changeme','field1':'value1'},
            {'id':'changeme1','field2':'value2'}]
>>> solr.index_json('SolrClient_unittest',json.dumps(docs))
local_index(collection, filename, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • filename (str) – String file path of the file to index.

Will index specified file into Solr. The file must be local to the server, this is faster than other indexing options. If the files are already on the servers I suggest you use this. For example:

>>> solr.local_index('SolrClient_unittest',
                           '/local/to/server/temp_file.json')
mget(collection, doc_ids, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • doc_ids (tuple) – ID of the document to be retrieved.

Retrieve documents from Solr based on the ID.

>>> solr.get('SolrClient_unittest','changeme')
paging_query(collection, query, rows=1000, start=0, max_start=200000)
Parameters:
  • collection (str) – The name of the collection for the request.
  • query (dict) – Dictionary of solr args.
  • rows (int) – Number of rows to return in each batch. Default is 1000.
  • start (int) – What position to start with. Default is 0.
  • max_start (int) – Once the start will reach this number, the function will stop. Default is 200000.

Will page through the result set in increments of row WITHOUT using cursorMark until it has all items or until max_start is reached. Use max_start to protect your Solr instance if you are not sure how many items you will be getting. The default is 200,000, which is still a bit high.

Returns an iterator of SolrResponse objects. For Example:

>>> for res in solr.paging_query('SolrClient_unittest',{'q':'*:*'}):
        print(res)
query(collection, query, request_handler='select', **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • request_handler (str) – Request handler, default is ‘select’
  • query (dict) – Python dictonary of Solr query parameters.

Sends a query to Solr, returns a SolrResults Object. query should be a dictionary of solr request handler arguments. Example:

res = solr.query('SolrClient_unittest',{
    'q':'*:*',
    'facet':True,
    'facet.field':'facet_test',
})
query_raw(collection, query, request_handler='select', **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • request_handler (str) – Request handler, default is ‘select’
  • query (dict) – Python dictionary of Solr query parameters.

Sends a query to Solr, returns a dict. query should be a dictionary of solr request handler arguments. Example:

res = solr.query_raw('SolrClient_unittest',{
    'q':'*:*',
    'facet':True,
    'facet.field':'facet_test',
})
stream_file(collection, filename, **kwargs)
Parameters:
  • collection (str) – The name of the collection for the request
  • filename (str) – Filename of json file to index.

Will open the json file, uncompressing it if necessary, and submit it to specified solr collection for indexing.

>>> solr.local_index('SolrClient_unittest',
                           '/local/to/script/temp_file.json')