Subversion Repositories SmartDukaan

Rev

Rev 27105 | Rev 27325 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22346 amit.gupta 1
package com.spice.profitmandi.common.solr;
2
 
25380 amit.gupta 3
import java.util.ArrayList;
27030 amit.gupta 4
import java.util.Arrays;
25380 amit.gupta 5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
22346 amit.gupta 8
 
25380 amit.gupta 9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.http.conn.HttpHostConnectException;
11
import org.json.JSONArray;
12
import org.json.JSONObject;
27030 amit.gupta 13
import org.springframework.beans.factory.annotation.Autowired;
26736 amit.gupta 14
import org.springframework.beans.factory.annotation.Value;
25380 amit.gupta 15
import org.springframework.stereotype.Service;
16
 
17
import com.spice.profitmandi.common.enumuration.SchemeType;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.web.client.RestClient;
20
 
25386 amit.gupta 21
@Service("solrServiceCommon")
22346 amit.gupta 22
public class SolrService {
26736 amit.gupta 23
 
24
	@Value("${new.solr.url}")
25
	private String solrUrl;
27030 amit.gupta 26
 
27
	@Autowired
28
	private RestClient restClient;
26606 amit.gupta 29
	public String getContent(String queryTerm) throws Exception {
22346 amit.gupta 30
		Map<String, String> params = new HashMap<>();
25380 amit.gupta 31
		List<String> mandatoryQ = new ArrayList<>();
32
		if (queryTerm != null && !queryTerm.equals("null")) {
33
			mandatoryQ.add(String.format("+(%s)", queryTerm));
34
		} else {
35
			queryTerm = null;
36
		}
27324 amit.gupta 37
		params.put("q", StringUtils.join("*" + mandatoryQ + "*", " "));
25380 amit.gupta 38
		params.put("fl", "*");
39
		if (queryTerm == null) {
40
			params.put("sort", "create_s desc");
41
		}
22346 amit.gupta 42
		params.put("start", String.valueOf(0));
25380 amit.gupta 43
		params.put("rows", String.valueOf(20));
22346 amit.gupta 44
		params.put("wt", "json");
25380 amit.gupta 45
		String response = null;
22346 amit.gupta 46
		try {
27030 amit.gupta 47
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
25380 amit.gupta 48
		} catch (HttpHostConnectException e) {
49
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
22346 amit.gupta 50
		}
25380 amit.gupta 51
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
52
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
53
		return docs.toString();
22346 amit.gupta 54
	}
25380 amit.gupta 55
 
26606 amit.gupta 56
	public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
57
		Map<Integer, JSONObject> documentMap = new HashMap<>();
58
		Map<String, String> params = new HashMap<>();
59
		params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
60
		params.put("fl", "*");
61
		params.put("start", String.valueOf(0));
62
		params.put("rows", String.valueOf(50));
63
		params.put("wt", "json");
64
		String response = null;
65
		try {
27030 amit.gupta 66
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
26606 amit.gupta 67
		} catch (HttpHostConnectException e) {
68
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
69
		}
70
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
71
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
72
		for (int i = 0; i < docs.length(); i++) {
73
			JSONObject doc = docs.getJSONObject(i);
74
			documentMap.put(doc.getInt("catalogId_i"), doc);
75
		}
76
		return documentMap;
77
	}
27030 amit.gupta 78
 
79
	//This method is the used to pull docs based on search nad shall be used interchangably for both the things
80
	public JSONArray getSolrDocs(String queryTerm, String categoryId, String offset, String limit,
81
			String sort, String brand, int subCategoryId, boolean hotDeal) throws Throwable {
82
		List<String> parentFilter = new ArrayList<>();
83
		parentFilter.add("categoryId_i:" + categoryId);
26606 amit.gupta 84
 
27030 amit.gupta 85
		List<String> childFilter = new ArrayList<>();
86
		childFilter.add("itemId_i:*");
87
 
88
		Map<String, String> params = new HashMap<>();
89
		if (queryTerm == null || queryTerm.equals("null")) {
90
			queryTerm = "";
91
		} else {
92
			queryTerm = "(" + queryTerm + ")";
93
		}
94
		if (hotDeal) {
95
			childFilter.add("hot_deal_b:true");
96
		} else {
97
			childFilter.add("active_b:true");
98
		}
99
		if (subCategoryId != 0) {
100
			parentFilter.add("subCategoryId_i:" + subCategoryId);
101
		}
102
		if (StringUtils.isNotBlank(brand)) {
27105 amit.gupta 103
			parentFilter.add("brand_ss:" + "\\\"" + brand + "\\\"");
27030 amit.gupta 104
		}
105
		if (queryTerm == "") {
27091 amit.gupta 106
			params.put("sort", (sort=="" ? "" : sort + ", ")+"create_s desc" );
27030 amit.gupta 107
		} else {
108
			parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));
109
		}
110
		String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";
111
		String childFilterString = String.join(" AND ", childFilter);
112
		params.put("q", String.format("{!parent which=%s}%s", parentFilterString, childFilterString));
113
		params.put("fl",
114
				String.format("*, [child parentFilter=id:catalog* childFilter=%s]", "\"" + childFilterString + "\""));
115
		params.put("start", String.valueOf(offset));
116
		params.put("rows", String.valueOf(limit));
117
		params.put("wt", "json");
118
		String response = null;
119
		try {
120
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
121
		} catch (HttpHostConnectException e) {
122
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
123
		}
124
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
125
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
126
		return docs;
127
	}
128
 
22346 amit.gupta 129
}