Whether you’re querying or updating a solr server, you need to set up a connection to the solr server. Pass the URL of the solr server to a SolrInterface object.
solr_interface = sunburnt.SolrInterface("http://localhost:8983/solr/")
If you are using a multicore setup <http://wiki.apache.org/solr/CoreAdmin> (which is strongly recommended, even if you only use a single core), then you need to pass the full URL to the core in question.
solr_interface = sunburnt.SolrInterface("http://localhost:8983/solr/master/")
The SolrInterface object can take three additional optional parameters.
It’s generally a sensible idea to not use the default http_connection, which doesn’t do any caching. If you’re likely to find your program making the same requests more than once (because perhaps your users make the same common searches), then you should use a caching http connection. Solr does very good internal caching of search results, but also supports proper HTTP-level caching, and you’ll get much better performance by taking advantage of that. To do that, set up your interface object like so:
solr_url = "http://localhost:8983/solr"
h = httplib2.Http(cache="/var/tmp/solr_cache")
solr_interface = SolrInterface(url=solr_url, http_connection=h)
Sometimes it’s necessary to make changes to your Solr schema. You may want to add new fields, or change the configuration of existing fields.
There are various ways to approach this. One of the most transparent ways is to duplicate an existing core, update its schema offline, and then use Solr’s multicore commands to change which core is exposed. This can be done entirely transparently to any clients which are currently connected.
However, the SolrInterface object is set up with a single schema when it’s initialized (whether by reading the schema from the Solr instance, or by the schema being passed in as a parameter). If the core is changed to have a different schema, the SolrInterface object will not reflect this change until you tell it to re-read the schema:
si = SolrInterface(solr_server)
# Elsewhere, restart solr with a different schema
si.init_schema()