Monday, February 25, 2013

Tutorial on SolrJ


SolrJ, a java client to access Apache Solr, offers a java interface that enables the developer to add, update, and query the Solr index more conveniently. This post is a brief step-by-step tutorial on how to use SolrJ in your Java project. The installation of Apache Solr and introduction to Solr's basic commands is omitted here. Suppose the version of the installed Solr is 4.1.0.

Step 1: Start the Solr server on your computer




Step 2: Download solr-solrj-4.0.0.jar from http://repo1.maven.org/maven2/org/apache/solr/solr-solrj/4.0.0/solr-solrj-4.0.0.jar

Step 3: Copy solr-solrj-4.0.0.jar to a library folder in your Java project, and add it to the classpath.

Step 4: Copy all .jar files in your ..../solr-4.1.0/dist/solrj-lib folder to a library folder and add them to the classpath.

(For example, suppose Solr is installed in D:/Cwef34fr/solr-4.1.0/dist folder, copy all jar files inside D:/Cwef34fr/solr-4.1.0/dist/solrj-lib to a library folder of the project and then add them to the classpth)




Step 5: Copy the code below to create SolrJ.java in the project and configure it based on the schema of your indexed documents. Other various SolrJ ApIs can be looked up online at http://wiki.apache.org/solr/Solrj or other online documents.


import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;

import java.io.IOException;

public class SolrJ {

 private static final String urlString = "http://localhost:8983/solr";
 private SolrServer solrServer;

 public SolrJ() {
  if (solrServer == null) {
   solrServer = new HttpSolrServer(urlString);
  }
 }

 public void deleteByQuery(String queryString) {
  try {
   solrServer.deleteByQuery(queryString);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void addDocumentTest() {

  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "tsetstst3r4", 1.0f);
  doc.addField( "name", "doc1", 1.0f );
  doc.addField( "price", 10 );
  try {
   solrServer.add(doc);
  } catch (SolrServerException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 public QueryResponse getRueryResponse(String queryString) {
  SolrQuery query = new SolrQuery();
  query.setQuery(queryString);

  QueryResponse queryResponse = null;
  try {
   queryResponse = solrServer.query(query);
  } catch (SolrServerException e) {
   e.printStackTrace();
  }
  return queryResponse;
 }

 public static void main(String[] args) {
  //Go to http://wiki.apache.org/solr/Solrj to look up various other SolrJ APIs 
  
  SolrJ solrJ = new SolrJ();
//  solrJ.addDocumentTest();  
  QueryResponse response = solrJ.getRueryResponse("id:*");
  System.out.println("SolrJ 61 response =  " + response);
 }
}