Subversion Repositories SmartDukaan

Rev

Rev 20260 | 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.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 String SOLR_URL;
        private static final String limit ="20";
        private static final String groupLimit = "10";
        private static final String autoSuggestField = "title,subCategoryId,category_id,category,subCategory,score";
        private static final String searchResultsField = "title,skuBundleId,id,thumbnail,ids";
        private static final String outputFormat = "json";
        private static Logger log = Logger.getLogger(Class.class);

        static{
                synchronized(SolrService.class){
                        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:8080/solr/select";
                        }
                }
        }

        public String getSuggestions(String search_text) throws URISyntaxException, IOException{
                URIBuilder autoSuggestUrl = new URIBuilder(SOLR_URL);
                autoSuggestUrl.addParameter("fl", autoSuggestField);                  //Fields to choose
                autoSuggestUrl.addParameter("wt", outputFormat);                                          //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", groupLimit);
                autoSuggestUrl.addParameter("q", "suggest:("+search_text+")");
                URL url = autoSuggestUrl.build().toURL();
                log.info("Search Url Autosuggest"+url.toString());
                
                InputStream is = url.openStream();
                String jsonString;
                try{
                        jsonString = IOUtils.toString(is, "UTF-8");
                }
                finally{
                        is.close();
                }
                return jsonString; 
        }
        
        public String getSearchResults(String search_text, String offset) throws URISyntaxException, IOException{
                URIBuilder generalSearchUrl = new URIBuilder(SOLR_URL);
                generalSearchUrl.addParameter("q", search_text);
                generalSearchUrl.addParameter("fl", searchResultsField);                  //Fields to choose
                generalSearchUrl.addParameter("wt", outputFormat);                                        //Output format
                generalSearchUrl.addParameter("rows", limit);                                     //limit
                if (offset!=null && !offset.isEmpty()){
                        generalSearchUrl.setParameter("start", offset);
                }
                URL url = generalSearchUrl.build().toURL();
                log.info("Search Url Search Results"+url.toString());
                InputStream is = url.openStream();
                String jsonString;
                try{
                        jsonString = IOUtils.toString(is, "UTF-8");
                }
                finally{
                        is.close();
                }
                return jsonString; 
        }

}