View Javadoc

1   package org.kuali.incubator;
2   
3   import org.apache.solr.client.solrj.SolrQuery;
4   import org.apache.solr.client.solrj.SolrServerException;
5   import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
6   import org.apache.solr.client.solrj.response.QueryResponse;
7   import org.apache.solr.common.SolrDocument;
8   import org.apache.solr.common.SolrDocumentList;
9   
10  import java.util.*;
11  
12  /**
13   * Created by IntelliJ IDEA.
14   * User: pvsubrah
15   * Date: 10/21/11
16   * Time: 10:57 AM
17   * To change this template use File | Settings | File Templates.
18   */
19  public class SolrRequestReponseHandler {
20  
21      public List retriveResults(String queryString) {
22          CommonsHttpSolrServer server = null;
23          ArrayList<HashMap<String, Object>> hitsOnPage = new ArrayList<HashMap<String, Object>>();
24  
25          try {
26              String serverUrl = PropertyUtil.getPropertyUtil().getProperty("discovery.url");
27              server = new CommonsHttpSolrServer(serverUrl);
28          } catch (Exception e) {
29              e.printStackTrace();
30          }
31  
32          SolrQuery query = new SolrQuery();
33          query.setQuery(queryString);
34          query.setIncludeScore(true);
35  
36          try {
37              QueryResponse qr = server.query(query);
38  
39              SolrDocumentList sdl = qr.getResults();
40  
41  
42              for (SolrDocument d : sdl) {
43                  HashMap<String, Object> values = new HashMap<String, Object>();
44  
45                  for (Iterator<Map.Entry<String, Object>> i = d.iterator(); i.hasNext(); ) {
46                      Map.Entry<String, Object> e2 = i.next();
47  
48                      values.put(e2.getKey(), e2.getValue());
49                  }
50  
51                  hitsOnPage.add(values);
52              }
53          } catch (SolrServerException e) {
54              e.printStackTrace();
55          }
56          return hitsOnPage;
57      }
58  }