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