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.

p<strong>Solr</strong>Client , or Cloud<strong>Solr</strong>Client . Both communicate with <strong>Solr</strong> via HTTP, the difference is that Http<strong>Solr</strong><br />

Client is configured using an explicit <strong>Solr</strong> URL, while Cloud<strong>Solr</strong>Client is configured using the zkHost String<br />

for a <strong>Solr</strong>Cloud cluster.<br />

Single node <strong>Solr</strong> client<br />

String urlString = "http://localhost:8983/solr/techproducts";<br />

<strong>Solr</strong>Client solr = new Http<strong>Solr</strong>Client(urlString);<br />

<strong>Solr</strong>Cloud client<br />

String zkHostString = "zkServerA:2181,zkServerB:2181,zkServerC:2181/solr";<br />

<strong>Solr</strong>Client solr = new Cloud<strong>Solr</strong>Client(zkHostString);<br />

Once you have a <strong>Solr</strong>Client, you can use it by calling methods like query() , add() , and commit() .<br />

Building and Running <strong>Solr</strong>J Applications<br />

The <strong>Solr</strong>J API is included with <strong>Solr</strong>, so you do not have to download or install anything else. However, in order to<br />

build and run applications that use <strong>Solr</strong>J, you have to add some libraries to the classpath.<br />

At build time, the examples presented with this section require solr-solrj-x.y.z.jar to be in the classpath.<br />

At run time, the examples in this section require the libraries found in the 'dist/solrj-lib' directory.<br />

The Ant script bundled with this sections' examples includes the libraries as appropriate when building and<br />

running.<br />

You can sidestep a lot of the messing around with the JAR files by using Maven instead of Ant. All you will need<br />

to do to include <strong>Solr</strong>J in your application is to put the following dependency in the project's pom.xml:<br />

<br />

org.apache.solr<br />

solr-solrj<br />

x.y.z<br />

<br />

If you are worried about the <strong>Solr</strong>J libraries expanding the size of your client application, you can use a code<br />

obfuscator like ProGuard to remove APIs that you are not using.<br />

Setting XMLResponseParser<br />

<strong>Solr</strong>J uses a binary format, rather than XML, as its default response format. If you are trying to mix <strong>Solr</strong> and <strong>Solr</strong>J<br />

versions where one is version 1.x and the other is 3.x or later, then you MUST use the XML response parser.<br />

The binary format changed in 3.x, and the two javabin versions are entirely incompatible. The following code will<br />

make this change:<br />

server.setParser(new XMLResponseParser());<br />

Performing Queries<br />

Use query() to have <strong>Solr</strong> search for results. You have to pass a <strong>Solr</strong>Query object that describes the query,<br />

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

643

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

Saved successfully!

Ooh no, something went wrong!