Subversion Repositories SmartDukaan

Rev

Rev 20214 | Rev 20217 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.dtrapi.services;

import in.shop2020.config.ConfigException;
import in.shop2020.thrift.clients.config.ConfigClient;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.log4j.Logger;



public class SolrService{

        private static URIBuilder autoSuggestUrl;
        private static URIBuilder generalSearchUrl;
        private static String SOLR_URL;
        private static Logger log = Logger.getLogger(Class.class);

        static{
                try {
                        SOLR_URL = ConfigClient.getClient().get("dtr_solr_url");
                } catch (ConfigException e) {
                        log.error("Error while gettting dtr_solr_url param from config service", e);
                        SOLR_URL = "http://localhost:8983/solr/collection1/select";
                }
                try {
                        autoSuggestUrl = new URIBuilder(SOLR_URL);
                } catch (URISyntaxException e) {
                        log.error("Error while building solr_url "+e);
                }
                autoSuggestUrl.addParameter("fl", "title,subCategoryId,category_id,category,subCategory,score");  //Fields to choose
                autoSuggestUrl.addParameter("wt", "json");                                        //Output format
                autoSuggestUrl.addParameter("group", "true");                             //group data
                autoSuggestUrl.addParameter("group.query", "category_id:3");  //group by category
                autoSuggestUrl.addParameter("group.query", "category_id:5");  //group by category
                autoSuggestUrl.addParameter("group.field", "subCategoryId");  //group by subCategory
                autoSuggestUrl.addParameter("group.limit", "10");             //search limit for each grouped field or query 
        }

        public String getSuggestions(String search_text) throws URISyntaxException, IOException{
                autoSuggestUrl.setParameter("q", "suggest:("+search_text+")");
                URL url = autoSuggestUrl.build().toURL();
                log.info("Search Url "+url.toString());
                InputStream is = url.openStream();
                String jsonString;
                try{
                        jsonString = IOUtils.toString(is, "UTF-8");
                }
                finally{
                        is.close();
                }
                return jsonString; 
        }

}