Solrj Example

* SolrJ is a java interface to access the index data.
* SolrJ 1.4 provides the ability to connect to Solr without sending requests over HTTP connections, and to compress requests into a Java binary format instead of sending XML
* Solr is a powerfull tool for searching. you can find more about solr See Solr Search
* SolrJ is used to retrieve the records in the form of Documents or Beans.
* Also we can add a new document in Solr Index using solrj.



Solrj to retrieve the records in the form of Documents:
There are three parts to get the records,
* We want to create the HTTP Request using CommonsHttpSolrServer
* We want to construct the solr query
* We want to specify which format we want to fetch records

i) Creating HTTP Request Using CommonsHttpSolrServer:

The CommonsHttpSolrServer uses the Apache Commons HTTP Client to connect to solr.
String url = http://localhost:8080/solr/collection1
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );

ii) Creating solr query SolrQuery:

SolrQuery is used to filter the Indexed records.
SolrQuery Main methods:
setQuery(qval) - It is used to search default field
setFilterQueries(fq) - It is Field Specific Search
SolrQuery query = new SolrQuery();
   query.setQuery('*:*');
   query.setFilterQueries("FieldName:\"Fieldvalue\"");

iii) Getting the Results in the SolrDocument:

QueryResponse rsp = server.query(query);
List<SolrDocument> docs = rsp.getResults();
  for(SolrDocument document : docs){
   Object formName = document.getFieldValue("FORMNAME");
   System.out.println(formName);
  }

Getting the Results in the SolrBean:

QueryResponse rsp = server.query(query);
List<Employee> docs = rsp.getBeans(Employee.class);
  for(Employee document : docs){
   Object empName = document.getEmpName();
   System.out.println(empName);
  }
Employee.class
package solrj.example;

public class Employee{
 private String EmpName;
 private String EmpID;
 @Field("EmpName")
 public void setEmpName(String empName) {
  EmpName = empName;
 }
 public String getEmpName() {
  return EmpName;
 }
 @Field("EmpID")
 public void setEmpID(String empID) {
  EmpID = empID;
 }
 public String getEmpID() {
  return EmpID;
 }
}
The Complete Solrj example code using document Format:
String url = http://localhost:8080/solr/collection1
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );
SolrQuery query = new SolrQuery();
          query.setQuery('*:*');
QueryResponse rsp = server.query(query);
List<SolrDocument> docs = rsp.getResults();
  for(SolrDocument document : docs){
   Object formName = document.getFieldValue("EmpName");
   System.out.println(formName);
  }
The Complete Solrj example code using Bean Format:
String url = http://localhost:8080/solr/collection1
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );
SolrQuery query = new SolrQuery();
          query.setQuery('*:*');
QueryResponse rsp = server.query(query);
List<Employee> docs = rsp.getBeans(Employee.class);
  for(Employee document : docs){
   Object empName = document.getEmpName();
   System.out.println(empName);
  }
I should point out, as I often do when I write about Solr, 
that I don't consider myself an expert in this area, so if you have 
any corrections or supplementary information to contribute to this topic,
please do so in the comments. And by all means, do the necessary research
before you take my word as gold. I'm still learning, and I'm happy to
correct any mistaken views.
You Might Also Like:
Solr Search