Quantcast
Viewing latest article 1
Browse Latest Browse All 3

Searching in Apache Solr with Solr4j


We will describe a function for searching in Apache Solr -

 
public static Map searchIndexSolr(String searchString) throws Exception {
    //Instantiate the Apache Solr Server
    SolrServer solr = new CommonsHttpSolrServer("http://localhost:8983/solr");

    ModifiableSolrParams params = new ModifiableSolrParams();
    
    //Setting the Search Parameter
    params.set("q", searchString);

    //Response from Solr Server
    QueryResponse response = solr.query(params);

    //Convert response to Solr Documents
    SolrDocumentList docs = response.getResults();
	    
    Iterator iter = response.getResults().iterator();
    ArrayList catString = new ArrayList();

    ArrayList categories = null;
	    
    boolean firstDoc = false;

    //Iterate over the Solr Documents	    
    while (iter.hasNext()) {
      SolrDocument resultDoc = iter.next();
      String content = (String) resultDoc.getFieldValue("content");
      String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field
      String subject = (String) resultDoc.getFieldValue("subject");
      categories = (ArrayList) resultDoc.getFieldValue("categories");
      System.out.println("**************");
      System.out.println("categories is -->>"+categories.toString());
   }
}

More advanced Solr Searches will be described later.

Comments are welcome.


Viewing latest article 1
Browse Latest Browse All 3

Trending Articles