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