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