Use of solrj

Source: Internet
Author: User
Tags solr xml parser

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

Solr1.3

    1. Commonshttpsolrserver
      1. Setting xmlresponseparser
      2. Changing other connection settings
    2. Embeddedsolrserver
    3. Usage
      1. Adding data to SOLR
        1. Streaming documents for an update
        2. Directly adding pojos to SOLR
      2. Setting the requestwriter
      3. Reading data from SOLR
        1. Advanced usage
        2. Highlighting
    4. Setting the classpath
      1. Ant
      2. Maven

 

Solrj is a Java client to access SOLR. It offers a Java interface to add, update, and query the SOLR index.

Commonshttpsolrserver

The commonshttpsolrserver uses the Apache commons HTTP client to connect to SOLR.

String url = "http: // localhost: 8983/SOLR";/* commonshttpsolrserver is thread-safe and if you are using the following constructor, you * Must * re-use the same instance for all requests. if instances are created on the fly, it can cause a connection leak. the recommended practice is to keep a static instance of commonshttpsolrserver per SOLR server URL and share it for all requests. see https://issues.apache.org/jira/browse/SOLR-861 For more details */solrserver Server = new commonshttpsolrserver (URL );

 

Setting xmlresponseparser

Solrj uses a binary format as the default format now. For users with SOLR 1.2 or older versions of SOLR 1.3 must explicitly ask solrj to use XML format.

 
Server. setparser (New xmlresponseparser ());
Changing other connection settings

Commonshttpsolrserver allows setting Connection Properties.

 
String url = "http: // localhost: 8983/SOLR" commonshttpsolrserver Server = new commonshttpsolrserver (URL); server. setsotimeout (1000); // socket read timeout server. setconnectiontimeout (100); server. setdefamaxmaxconnectionsperhost (100); server. setmaxtotalconnections (100); server. setfollowredirects (false); // defaults to false // allowcompression defaults to false. // server side must support gzip or deflate for this to have any effect. server. setallowcompression (true); server. setmaxretries (1); // defaults to 0.> 1 not recommended.

 

 

Embeddedsolrserver

The embeddedsolrserver provides the same interface without requiring an HTTP connection.

// Note that the following property cocould be set through JVM level arguments too system. setproperty ("SOLR. SOLR. home ","/home/shalinsmangar/work/OSS/branch-1.3/example/SOLR "); corecontainer. initializer = new corecontainer. initializer (); corecontainer = initializer. initialize (); embeddedsolrserver Server = new embeddedsolrserver (corecontainer ,"");

 

If you want to use multicore features, then you shocould 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 ");...

If you need to use SOLR in an embedded application, this is the recommended approach. It allows you to work with the same interface whether or not you have access to HTTP.

Note -- embeddedsolrserver works only with handlers registered in solrconfig. xml. A requesthandler must be mapped to/update for a request to/update to function.

Usage

Solrj is designed as an extendable framework to pass solrrequest to the solrserver and return a solrresponse.

For simplicity, the most common commands are modeled in the solrserver:

Adding data to SOLR
    • Get an instance of server first

 
Solrserver Server = getsolrserver ();

TheGetsolrserver ()Method body can be as follows if you use a remote server,

Public solrserver getsolrserver () {// The instance can be reused return New commonshttpsolrserver ();}

If it is a local server use the following,

 
Public solrserver getsolrserver () {// The instance can be reused return New embeddedsolrserver ();}
    • If you want to clean up the index before adding data do this

 
Server. deletebyquery ("*: *"); // delete everything!
    • Construct a document

 
Solrinputdocument doc1 = new solrinputdocument (); doc1.addfield ("ID", "id1", 1.0f); doc1.addfield ("name", "doc1", 1.0f); doc1.addfield ("price ", 10 );
    • Construct another document. Each document can be independently be added but it is more efficient to do a batch update. Every callSolrserverIs an HTTP call (this is not true for embeddedsolrserver ).

 
Solrinputdocument doc2 = new solrinputdocument (); doc2.addfield ("ID", "ID2", 1.0f); doc2.addfield ("name", "doc2", 1.0f); doc2.addfield ("price ", 20 );
    • Create a collection of documents

 
Collection <solrinputdocument> docs = new arraylist <solrinputdocument> (); docs. Add (doc1); docs. Add (doc2 );
    • Add the documents to SOLR

 
Server. Add (DOCS );
    • Do a commit

 
Server. Commit ();
    • To immediately commit after adding documents, you cocould use:

Updaterequest Req = new updaterequest (); Req. setaction (updaterequest. Action. Commit, false, false); Req. Add (DOCS); updateresponse RSp = Req. Process (server );
Streaming documents for an update

Solr1.4

This is the most optimal way of updating all your docs in one HTTP request.

 
Commonshttpsolrserver Server = new commonshttpsolrserver (); iterator <solrinputdocument> iter = new iterator <solrinputdocument> () {public Boolean hasnext () {boolean result; // set the result to true false to say if you have more than ensts return result;} public solrinputdocument next () {solrinputdocument result = NULL; // construct a new document here and set it to result return result ;}}; server. add (ITER );

You may also useAddbeans (iterator <?> Beansiter)Method To write pojos

Directly adding pojos to SOLR
    • Create a Java Bean with annotations.@ FieldAnnotation can be applied to a field or a setter method. If the field name is different from the bean field name give the aliased name in the annotation itself as shown in the categories field.

 
Import Org. apache. SOLR. client. solrj. beans. field; public class item {@ field string ID; @ field ("cat") string [] categories; @ Field List <string> features ;}

The@ FieldAnnotation can be applied on Setter methods as well example:

 
@ Field ("cat") Public void setcategory (string [] C) {This. Categories = C ;}

There shoshould be a corresponding getter method (without annotation) for reading attributes

    • Get an instance of server

 
Solrserver Server = getsolrserver ();
    • Create the bean instances

 
Item = new item (); item. ID = "one"; item. Categories = new string [] {"AAA", "BBB", "CCC "};
    • Add to SOLR

 
Server. addbean (item );
    • Adding multiple beans together

 
List <item> beans; // Add item objects to the list server. addbeans (beans );

Note -- reuse the instance of solrserver if you are using this feature (for performance)

Setting the requestwriter

Solr1.4 solrj lets you upload content in XML and binary format. Default is set to be XML. Use the following to upload using binary format. This is the same format which solrj uses to fetch results.

 
Server. setrequestwriter (New binaryrequestwriter ());
Reading data from SOLR
    • Get an instance of server first

 
Solrserver Server = getsolrserver ();
    • Construct a solrquery

 
Solrquery query = new solrquery (); query. setquery ("*: *"); query. addsortfield ("price", solrquery. Order. ASC );
    • Query the server

 
Queryresponse RSp = server. Query (query );
    • Get the results

 
Solrdocumentlist docs = RSP. getresults ();
    • To read documents as beans, the bean must be annotated as given in the example.

 
List <item> beans = RSP. getbeans (item. Class );
Advanced usage

Solrj provides a APIs to create queries instead of hand coding the query. Following is an example of a faceted query.

Solrserver Server = getsolrserver (); solrquery = new solrquery (). setquery ("iPod "). setfacet (true ). setfacetmincount (1 ). setfacetlimit (8 ). addfacetfield ("category "). addfacetfield ("instock"); queryresponse RSp = server. query (solrquery );

All the setter/Add Methods return its instance. Hence these CILS can be chained

Highlighting

Highlighting parameters are set like other common parameters.

 
Solrquery query = new solrquery (); query. setquery ("foo"); query. sethighlight (true ). sethighlightsnippets (1); // set other Params as needed query. setparam ("Hl. FL "," content "); queryresponse = getsolrserver (). query (query );

Then to get back the highlight results you need something like this:

Iterator <solrdocument> iter = queryresponse. getresults (); While (ITER. hasnext () {solrdocument resultdoc = ITER. next (); string content = (string) resultdoc. getfieldvalue ("content"); string id = (string) resultdoc. getfieldvalue ("ID"); // ID is the uniquekey field if (queryresponse. gethighlighting (). get (ID )! = NULL) {list <string> highightsnippets = queryresponse. gethighlighting (). Get (ID). Get ("content ");}}
Setting the classpathant

Solrj is a part of the 1.3.0 release. The jars required in the classpath for solrj are,

    • Commons-io-1.3.1.jar

    • Commons-httpclient-3.1.jar

    • Commons-codec-1.3.jar

    • Commons-logging-1.0.4.jar

    • Apache-solr-common-1.3.0.jar

    • Apache-solr-solrj-1.3.0.jar

If you are using an SOLR 1.2, add the Stax jars too. Do not forget to set the XML Parser

    • Geronimo-stax-api_1.0_spec-1.0.1.jar

    • Wstx-asl-3.2.7.jar

    • Stax-utils.jar

The above jars can be found in the DIST/solrj-lib directory of the SOLR 1.3.0 download.

Maven

Solrj is available in the official Maven repository. Add the following dependency to your pom. XML to use solrj

 
<Dependency> <artifactid> SOLR-solrj </artifactid> <groupid> Org. apache. SOLR </groupid> <version> 1.3.0 </version> <type> jar </type> <scope> compile </scope> </dependency>

If you need to use the embeddedsolrserver, you need to add the SOLR-core dependency too.

<Dependency> <artifactid> SOLR-core </artifactid> <groupid> Org. apache. SOLR </groupid> <version> 1.3.0 </version> <type> jar </type> <scope> compile </scope> </dependency>

Last edited 17:09:14Jayhill

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.