SolPython

  1. Python API
  2. PySolr
  3. Using Solr's Python output
  4. Using normal JSON

Python API

There is a simple client API as part of the Solr repository: [WWW] http://svn.apache.org/repos/asf/lucene/solr/trunk/client/python/

/!\ :TODO: /!\ it would be good to have a little more info about this ... what it can do, how stable it is, etc...

PySolr

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

/!\ :TODO: /!\ it would be good to have a little more info about this ... what it can do, how stable it is, etc...

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']

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 [WWW] 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

last edited 2008-02-06 21:46:29 by HossMan