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