11.05.2016 Views

Apache Solr Reference Guide Covering Apache Solr 6.0

21SiXmO

21SiXmO

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

from urllib2 import *<br />

Now open a connection to the server and get a response. The wt query parameter tells <strong>Solr</strong> to return results in a<br />

format that Python can understand.<br />

connection = urlopen(<br />

'http://localhost:8983/solr/collection_name/select?q=cheese&wt=python')<br />

response = eval(connection.read())<br />

Now interpreting the response is just a matter of pulling out the information that you need.<br />

print response['response']['numFound'], "documents found."<br />

# Print the name of each document.<br />

for document in response['response']['docs']:<br />

print " Name =", document['name']<br />

Python with JSON<br />

JSON is a more robust response format, but you will need to add a Python package in order to use it. At a<br />

command line, install the simplejson package like this:<br />

$ sudo easy_install simplejson<br />

Once that is done, making a query is nearly the same as before. However, notice that the wt query parameter is<br />

now json, and the response is now digested by simplejson.load() .<br />

from urllib2 import *<br />

import simplejson<br />

connection =<br />

urlopen('http://localhost:8983/solr/collection_name/select?q=cheese&wt=json')<br />

response = simplejson.load(connection)<br />

print response['response']['numFound'], "documents found."<br />

# Print the name of each document.<br />

for document in response['response']['docs']:<br />

print " Name =", document['name']<br />

Using <strong>Solr</strong>J<br />

<strong>Solr</strong>J is an API that makes it easy for Java applications to talk to <strong>Solr</strong>. <strong>Solr</strong>J hides a lot of the details of<br />

connecting to <strong>Solr</strong> and allows your application to interact with <strong>Solr</strong> with simple high-level methods.<br />

The center of <strong>Solr</strong>J is the org.apache.solr.client.solrj package, which contains just five main classes.<br />

Begin by creating a <strong>Solr</strong>Client, which represents the <strong>Solr</strong> instance you want to use. Then send <strong>Solr</strong>Reques<br />

ts or <strong>Solr</strong>Querys and get back <strong>Solr</strong>Responses.<br />

<strong>Solr</strong>Client is abstract, so to connect to a remote <strong>Solr</strong> instance, you'll actually create an instance of either Htt<br />

<strong>Apache</strong> <strong>Solr</strong> <strong>Reference</strong> <strong>Guide</strong> <strong>6.0</strong><br />

642

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!