Subversion Repositories SmartDukaan

Rev

Rev 27601 | Rev 28221 | 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.Arrays;
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.Autowired;
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;
        
        @Autowired
        private RestClient restClient;
        public String getContent(String queryTerm, int categoryId, List<String> brands, int limit) throws Exception {
                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, " "));
                if(categoryId > 0) {
                        params.put("q", params.get("q") + " +filter(categoryId_i:" + categoryId + ")");
                }
                if(brands.size()>0) {
                        params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");
                }
                params.put("fl", "*");
                if (queryTerm == null) {
                        params.put("sort", "create_s desc");
                }
                params.put("start", String.valueOf(0));
                if(limit==0) {
                        params.put("fl", "catalogId_i, title_s");
                        params.put("rows", String.valueOf(5000));
                } else {
                        params.put("rows", String.valueOf(20));
                }
                params.put("wt", "json");
                String response = null;
                try {
                        response = restClient.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 {
                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 = restClient.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;
        }
        
        //This method is the used to pull docs based on search nad shall be used interchangably for both the things
        public JSONArray getSolrDocs(String queryTerm, String categoryId, String offset, String limit,
                        String sort, String brand, int subCategoryId, boolean hotDeal) throws Throwable {
                List<String> parentFilter = new ArrayList<>();
                parentFilter.add("categoryId_i:" + categoryId);

                List<String> childFilter = new ArrayList<>();
                childFilter.add("itemId_i:*");

                Map<String, String> params = new HashMap<>();
                if (queryTerm == null || queryTerm.equals("null")) {
                        queryTerm = "";
                } else {
                        queryTerm = "(" + queryTerm + ")";
                }
                if (hotDeal) {
                        childFilter.add("hot_deal_b:true");
                } else {
                        childFilter.add("active_b:true");
                }
                if (subCategoryId != 0) {
                        parentFilter.add("subCategoryId_i:" + subCategoryId);
                }
                if (StringUtils.isNotBlank(brand)) {
                        parentFilter.add("brand_ss:" + "\\\"" + brand + "\\\"");
                }
                if (queryTerm == "") {
                        params.put("sort", (sort=="" ? "" : sort + ", ")+"create_s desc" );
                } else {
                        parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));
                }
                String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";
                String childFilterString = String.join(" AND ", childFilter);
                params.put("q", String.format("{!parent which=%s}%s", parentFilterString, childFilterString));
                params.put("fl",
                                String.format("*, [child parentFilter=id:catalog* childFilter=%s]", "\"" + childFilterString + "\""));
                params.put("start", String.valueOf(offset));
                params.put("rows", String.valueOf(limit));
                params.put("wt", "json");
                String response = null;
                try {
                        response = restClient.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;
        }

}