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);
 }
}


13 comments:

  1. does it work for android also?

    ReplyDelete
  2. Thanks Buddy.. You saved my day... :)

    ReplyDelete
  3. dude, it works! you saved my life. thanks

    ReplyDelete
  4. Awesome, thanks! Exactly what I needed.

    I did get a NoClassDefFound Exception because the Apache Commons Logging library was missing, though - if anyone else has that problem, you can download the .jar here: http://commons.apache.org/proper/commons-logging/download_logging.cgi

    ReplyDelete
  5. Hi, If I run thru Main method its working but when I created JAR of it and call it in another project its giving me java.lang.ClassNotFoundException: org.apache.solr.common.params.SolrParams error. can you help me?

    ReplyDelete
  6. Am just beginning to work with Solr and SolrJ. In the example above, you submit and index one and only one document, specifically setting the field values per indexed fields using doc.addField(). Does SolrJ have no native means to submit a folder or directory of documents, each of which has these fields? Is the way to attack that problem to open a handle to the directory, and use an iterator to iterate through each file to parse out the values and give them to doc.addField()? Would that approach be the same regardless of whether the files are csv or xml? Is there a SOLR or general java library preferred for parsing such documents to get at the field values? Since each file likely has many records, I'm anticipating that you need to wrap your calls to doc.addField() in an iterator to work through the files and a nested iterator to loop through each record in the file. Is that a neanderthal way to approach indexing all the files and records in a directory? Thanks very much.

    ReplyDelete
  7. Thanks for de information. I try it!
    Gratz!!

    ReplyDelete
  8. Replies
    1. Could you help me? I follow steps but it's not working.

      Delete
  9. How can I downloads documents from apache solr?

    ReplyDelete
  10. instead of hard coding in document.addfield i want to fetch index data how should i do that ?
    I mean i want to build a qury builder

    ReplyDelete
  11. learn coding for kids Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.

    ReplyDelete