Http://wiki.apache.org/solr/Solrj
Solr1.3
- Commonshttpsolrserver
- Setting xmlresponseparser
- Changing other connection settings
- Embeddedsolrserver
- Usage
- Adding data to SOLR
- Streaming documents for an update
- Directly adding pojos to SOLR
- Setting the requestwriter
- Reading data from SOLR
- Advanced usage
- Highlighting
- Setting the classpath
- Ant
- 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
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 ();}
Server. deletebyquery ("*: *"); // delete everything!
Solrinputdocument doc1 = new solrinputdocument (); doc1.addfield ("ID", "id1", 1.0f); doc1.addfield ("name", "doc1", 1.0f); doc1.addfield ("price ", 10 );
Solrinputdocument doc2 = new solrinputdocument (); doc2.addfield ("ID", "ID2", 1.0f); doc2.addfield ("name", "doc2", 1.0f); doc2.addfield ("price ", 20 );
Collection <solrinputdocument> docs = new arraylist <solrinputdocument> (); docs. Add (doc1); docs. Add (doc2 );
Server. Add (DOCS );
Server. Commit ();
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
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
Solrserver Server = getsolrserver ();
Item = new item (); item. ID = "one"; item. Categories = new string [] {"AAA", "BBB", "CCC "};
Server. addbean (item );
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
Solrserver Server = getsolrserver ();
Solrquery query = new solrquery (); query. setquery ("*: *"); query. addsortfield ("price", solrquery. Order. ASC );
Queryresponse RSp = server. Query (query );
Solrdocumentlist docs = RSP. getresults ();
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
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