Subversion Repositories SmartDukaan

Rev

Rev 26606 | Rev 27030 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.solr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.HttpHostConnectException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.client.RestClient;

@Service("solrServiceCommon")
public class SolrService {
        
        @Value("${new.solr.url}")
        private String solrUrl;
        public String getContent(String queryTerm) throws Exception {
                RestClient rc = new RestClient();
                Map<String, String> params = new HashMap<>();
                List<String> mandatoryQ = new ArrayList<>();
                if (queryTerm != null && !queryTerm.equals("null")) {
                        mandatoryQ.add(String.format("+(%s)", queryTerm));
                } else {
                        queryTerm = null;
                }
                params.put("q", StringUtils.join(mandatoryQ, " "));
                params.put("fl", "*");
                if (queryTerm == null) {
                        params.put("sort", "create_s desc");
                }
                params.put("start", String.valueOf(0));
                params.put("rows", String.valueOf(20));
                params.put("wt", "json");
                String response = null;
                try {
                        response = rc.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
                } catch (HttpHostConnectException e) {
                        throw new ProfitMandiBusinessException("", "", "Could not connect to host");
                }
                JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
                JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
                return docs.toString();
        }

        public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
                RestClient rc = new RestClient();
                Map<Integer, JSONObject> documentMap = new HashMap<>();
                Map<String, String> params = new HashMap<>();
                params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
                params.put("fl", "*");
                params.put("start", String.valueOf(0));
                params.put("rows", String.valueOf(50));
                params.put("wt", "json");
                String response = null;
                try {
                        response = rc.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
                } catch (HttpHostConnectException e) {
                        throw new ProfitMandiBusinessException("", "", "Could not connect to host");
                }
                JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
                JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
                for (int i = 0; i < docs.length(); i++) {
                        JSONObject doc = docs.getJSONObject(i);
                        documentMap.put(doc.getInt("catalogId_i"), doc);
                }
                return documentMap;
        }

}