Subversion Repositories SmartDukaan

Rev

Rev 27601 | Rev 28221 | 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;
27603 amit.gupta 29
	public String getContent(String queryTerm, int categoryId, List<String> brands, int limit) 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")) {
27325 amit.gupta 33
			mandatoryQ.add(String.format("+(%s)", "*"+queryTerm + "*"));
25380 amit.gupta 34
		} else {
35
			queryTerm = null;
36
		}
27325 amit.gupta 37
		params.put("q", StringUtils.join(mandatoryQ, " "));
27601 amit.gupta 38
		if(categoryId > 0) {
39
			params.put("q", params.get("q") + " +filter(categoryId_i:" + categoryId + ")");
40
		}
41
		if(brands.size()>0) {
42
			params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");
43
		}
25380 amit.gupta 44
		params.put("fl", "*");
45
		if (queryTerm == null) {
46
			params.put("sort", "create_s desc");
47
		}
22346 amit.gupta 48
		params.put("start", String.valueOf(0));
27603 amit.gupta 49
		if(limit==0) {
50
			params.put("fl", "catalogId_i, title_s");
51
			params.put("rows", String.valueOf(5000));
52
		} else {
53
			params.put("rows", String.valueOf(20));
54
		}
22346 amit.gupta 55
		params.put("wt", "json");
25380 amit.gupta 56
		String response = null;
22346 amit.gupta 57
		try {
27030 amit.gupta 58
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
25380 amit.gupta 59
		} catch (HttpHostConnectException e) {
60
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
22346 amit.gupta 61
		}
25380 amit.gupta 62
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
63
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
64
		return docs.toString();
22346 amit.gupta 65
	}
25380 amit.gupta 66
 
26606 amit.gupta 67
	public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
68
		Map<Integer, JSONObject> documentMap = new HashMap<>();
69
		Map<String, String> params = new HashMap<>();
70
		params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
71
		params.put("fl", "*");
72
		params.put("start", String.valueOf(0));
73
		params.put("rows", String.valueOf(50));
74
		params.put("wt", "json");
75
		String response = null;
76
		try {
27030 amit.gupta 77
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
26606 amit.gupta 78
		} catch (HttpHostConnectException e) {
79
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
80
		}
81
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
82
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
83
		for (int i = 0; i < docs.length(); i++) {
84
			JSONObject doc = docs.getJSONObject(i);
85
			documentMap.put(doc.getInt("catalogId_i"), doc);
86
		}
87
		return documentMap;
88
	}
27030 amit.gupta 89
 
90
	//This method is the used to pull docs based on search nad shall be used interchangably for both the things
91
	public JSONArray getSolrDocs(String queryTerm, String categoryId, String offset, String limit,
92
			String sort, String brand, int subCategoryId, boolean hotDeal) throws Throwable {
93
		List<String> parentFilter = new ArrayList<>();
94
		parentFilter.add("categoryId_i:" + categoryId);
26606 amit.gupta 95
 
27030 amit.gupta 96
		List<String> childFilter = new ArrayList<>();
97
		childFilter.add("itemId_i:*");
98
 
99
		Map<String, String> params = new HashMap<>();
100
		if (queryTerm == null || queryTerm.equals("null")) {
101
			queryTerm = "";
102
		} else {
103
			queryTerm = "(" + queryTerm + ")";
104
		}
105
		if (hotDeal) {
106
			childFilter.add("hot_deal_b:true");
107
		} else {
108
			childFilter.add("active_b:true");
109
		}
110
		if (subCategoryId != 0) {
111
			parentFilter.add("subCategoryId_i:" + subCategoryId);
112
		}
113
		if (StringUtils.isNotBlank(brand)) {
27105 amit.gupta 114
			parentFilter.add("brand_ss:" + "\\\"" + brand + "\\\"");
27030 amit.gupta 115
		}
116
		if (queryTerm == "") {
27091 amit.gupta 117
			params.put("sort", (sort=="" ? "" : sort + ", ")+"create_s desc" );
27030 amit.gupta 118
		} else {
119
			parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));
120
		}
121
		String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";
122
		String childFilterString = String.join(" AND ", childFilter);
123
		params.put("q", String.format("{!parent which=%s}%s", parentFilterString, childFilterString));
124
		params.put("fl",
125
				String.format("*, [child parentFilter=id:catalog* childFilter=%s]", "\"" + childFilterString + "\""));
126
		params.put("start", String.valueOf(offset));
127
		params.put("rows", String.valueOf(limit));
128
		params.put("wt", "json");
129
		String response = null;
130
		try {
131
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
132
		} catch (HttpHostConnectException e) {
133
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
134
		}
135
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
136
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
137
		return docs;
138
	}
139
 
22346 amit.gupta 140
}