Python API

There is a simple client API as part of the Solr repository: http://svn.apache.org/viewvc/lucene/solr/tags/release-1.2.0/client/python/

Note: As of version 1.3, Solr no longer comes bundled with a Python client. The existing client was not sufficiently maintained or tested as development of Solr progressed, and committers felt that the code was not up to our usual high standards of release.

The client bundled with previous versions of Solr will continue to be available indefinitely.

solrpy

solrpy is available at The Python Package Index so you should be able to:

easy_install solrpy

Or you can check out the source code and:

python setup.py install

PySolr

There is a independent "pysolr" project available ... http://code.google.com/p/pysolr/

And Python Solr, And enhanced version of pysolr that supports pagination and batch operations.

Using Solr's Python output

Solr has an optional Python response format that extends its JSON output in the following ways to allow the response to be safely eval'd by Python's interpreter:

Here is a simple example of how one may query Solr using the Python response format:

from urllib2 import *
conn = urlopen('http://localhost:8983/solr/select?q=iPod&wt=python')
rsp = eval( conn.read() )

print "number of matches=", rsp['response']['numFound']

#print out the name field for each returned document
for doc in rsp['response']['docs']:
  print 'name field =', doc['name']

With Python 2.6 you can use the literal_eval function instead of eval. This only evaluates "safe" syntax for the built-in data types and not any executable code:

import ast
rsp = ast.literal_eval(conn.read())

Using normal JSON

Using eval is generally considered bad form and dangerous in Python. In theory if you trust the remote server it is okay, but if something goes wrong it means someone can run arbitrary code on your server (attacking eval is very easy).

It would be better to use a Python JSON library like simplejson. It would look like:

from urllib2 import *
import simplejson
conn = urlopen('http://localhost:8983/solr/select?q=iPod&wt=json')
rsp = simplejson.load(conn)
...

Safer, and as you can see, easy.


CategoryQueryResponseWriter

SolPython (last edited 2009-10-31 17:51:59 by YonikSeeley)