Using SolrJ

6 篇文章 0 订阅

SolrJ (also sometimes known as SolJava) is an API that makes it easy for Java applications to talk to Solr. SolrJ hides a lot of the details of connecting to Solr and allows your application to interact with Solr with simple high-level methods.

The center of SolrJ is the org.apache.solr.client.solrj package, which contains just five main classes. Begin by creating a SolrServer, which represents the Solr instance you want to use. Send SolrRequests or SolrQuerys and get back SolrResponses.

SolrServer is abstract, so to connect to a remote Solr instance, you'll actually create an instance of org.apache.solr.client.solrj.impl.CommonsHttpSolrServer, which knows how to use HTTP to talk to Solr.

String urlString = "http://localhost:8983/solr";
SolrServer solr = new CommonsHttpSolrServer(urlString);

Creating a SolrServer does not make a network connection - that happens later when you perform a query or some other operation — but it will throw MalformedURLException if you give it a bad URL string.

Once you have a SolrServer, you can use it by calling methods like query()add(), and commit().

For more information on SolrJ, see https://wiki.apache.org/solr/Solrj.

Building and Running SolrJ Applications

The SolrJ API is included with Solr, so you do not have to download or install anything else. However, in order to build and run applications that use SolrJ, you have to add some libraries to the classpath.

At build time, the examples presented with this section require the following libraries in the classpath (all paths are relative to the root of the Solr installation).

apache-solr-common-3.x.0.jarapache-solr-solrj-3.x.0.jar

At run time, the examples in this section require the following libraries:

  • apache-solr-common-3.x.0.jar
  • apache-solr-solrj-3.x.0.jar
  • solrj-lib/commons-httpclient-3.x.jar
  • solrj-lib/commons-logging-1.0.4.jar
  • solrj-lib/commons-codec-3.x.jar

The Ant script bundled with this sections' examples includes the libraries as appropriate when building and running.

You can sidestep a lot of the messing around with the JAR files by using Maven instead of Ant. All you will need to do to include SolrJ in your application is to put the following dependency in the project's pom.xml:

<dependency>
   <groupId>org.apache.solr</groupId>
   <artifactId>solr-solrj</artifactId>
   <version>3.x.0</version>
</dependency>

If you are worried about the SolrJ libraries expanding the size of your client application, you can use a code obfuscator like ProGuard to remove APIs that you are not using. ProGuard is available here:

http://proguard.sourceforge.net/

Setting XMLResponseParser

SolrJ uses a binary format, rather than XML, as its default format. Users of earlier Solr releases who wish to continue working with XML must explicitly set the parser to the XMLResponseParser, like so:

server.setParser(new XMLResponseParser());

Performing Queries

Use query() to have Solr search for results. You have to pass a SolrQuery object that describes the query, and you will get back a QueryResponse (from theorg.apache.solr.client.solrj.response package).

SolrQuery has methods that make it easy to add parameters to choose a request handler and send parameters to it. Here is a very simple example that uses the default request handler and sets the q parameter:

SolrQuery parameters = new SolrQuery();
parameters.set("q", mQueryString);

To choose a different request handler, for example, just set the qt parameter like this:

parameters.set("qt", "/spellCheckCompRH");

Once you have your SolrQuery set up, submit it with query():

QueryResponse response = solr.query(parameters);

The client make a network connection, the query is sent, Solr processes the query, and the response is sent and parsed into a QueryResponse.

The QueryResponse is a collection of documents that satisfy the query parameters. You can retrieve the documents directly with getResults() and you can call other methods to find out information about highlighting or facets.

SolrDocumentList list = response.getResults();

Indexing Documents

Other operations are just as simple. To index (add) a document, all you need to do is create a SolrInputDocument and pass it along to the SolrServer's add() method.

String urlString = "http://localhost:8983/solr";
SolrServer solr = new CommonsHttpSolrServer(urlString);
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "552199");
document.addField("name", "Gouda cheese wheel");
document.addField("price", "49.99");
UpdateResponse response = solr.add(document);

Remember to commit your changes!

solr.commit();

Uploading Content in XML or Binary Formats

SolrJ lets you upload content in XML and binary formats instead of the default XML format. Use the following to upload using Binary format. this is the same format which SolrJ uses to fetch results.

server.setRequestWriter(new BinaryRequestWriter()); 

EmbeddedSolrServer

The EmbeddedSolrServer provides the Java interface described above without requiring an HTTP connection. This is the recommended approach if you need to use Solr in an embedded application. This approach enables you to work with the same Java interface whether or not you have access to HTTP.

EmbeddedSolrServer works only with handlers registered in solrconfig.xml. RequestHandler must be mapped to /update for a request to function. For information about configuring handlers in solrconfig.xml, see Configuring solrconfig.xml.

Note that the following property could be set through JVM level arguments:

System.setProperty("solr.solr.home", "/home/shalinsmangar/work/oss/branch-1.3/example/solr");
CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = initializer.initialize();
EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");

If you want to use MultiCore features (which are described in Configuring solr.xml), then you should use this:

File home = new File( "/path/to/solr/home" );
File f = new File( home, "solr.xml" );
CoreContainer container = new CoreContainer();
container.load( "/path/to/solr/home", f );
EmbeddedSolrServer server = new EmbeddedSolrServer( container, "core name as defined in solr.xml" );
    ...

Using the StreamingUpdateSolrServer

If you are working with Java, you can take advantage of the StreamingUpdateSolrServer to perform bulk updates at high speed. StreamingHttpSolrServer buffers all added documents and writes them into open HTTP connections. This class is thread safe. Although any SolrServer request can be made with this implementation, it is only recommended to use the StreamingUpdateSolrServer for /update requests.

You can learn more about the StreamingUpdateSolrServer here:

http://lucene.apache.org/solr/api/org/apache/solr/client/solrj/impl/StreamingUpdateSolrServer.html

More Information

As you begin developing with SolrJ, you will find the API documentation indispensable. It is available online at the Apache Lucene site:

http://lucene.apache.org/solr/api/solrj/index.html

For more information about using SolrJ, read the page at the Solr Wiki:

http://wiki.apache.org/solr/Solrj

The Solr Wiki also contains another example which demonstrates setting qt:

http://wiki.apache.org/solr/SolJava

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值