001    package org.kuali.incubator;
002    
003    import org.apache.solr.client.solrj.SolrQuery;
004    import org.apache.solr.client.solrj.SolrServerException;
005    import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
006    import org.apache.solr.client.solrj.response.QueryResponse;
007    import org.apache.solr.common.SolrDocument;
008    import org.apache.solr.common.SolrDocumentList;
009    
010    import java.util.*;
011    
012    /**
013     * Created by IntelliJ IDEA.
014     * User: pvsubrah
015     * Date: 10/21/11
016     * Time: 10:57 AM
017     * To change this template use File | Settings | File Templates.
018     */
019    public class SolrRequestReponseHandler {
020    
021        public List retriveResults(String queryString) {
022            CommonsHttpSolrServer server = null;
023            ArrayList<HashMap<String, Object>> hitsOnPage = new ArrayList<HashMap<String, Object>>();
024    
025            try {
026                String serverUrl = PropertyUtil.getPropertyUtil().getProperty("discovery.url");
027                server = new CommonsHttpSolrServer(serverUrl);
028            } catch (Exception e) {
029                e.printStackTrace();
030            }
031    
032            SolrQuery query = new SolrQuery();
033            query.setQuery(queryString);
034            query.setIncludeScore(true);
035    
036            try {
037                QueryResponse qr = server.query(query);
038    
039                SolrDocumentList sdl = qr.getResults();
040    
041    
042                for (SolrDocument d : sdl) {
043                    HashMap<String, Object> values = new HashMap<String, Object>();
044    
045                    for (Iterator<Map.Entry<String, Object>> i = d.iterator(); i.hasNext(); ) {
046                        Map.Entry<String, Object> e2 = i.next();
047    
048                        values.put(e2.getKey(), e2.getValue());
049                    }
050    
051                    hitsOnPage.add(values);
052                }
053            } catch (SolrServerException e) {
054                e.printStackTrace();
055            }
056            return hitsOnPage;
057        }
058    }