| 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.*;
|
| 31712 |
amit.gupta |
17 |
import java.util.stream.Collectors;
|
| 25380 |
amit.gupta |
18 |
|
| 25386 |
amit.gupta |
19 |
@Service("solrServiceCommon")
|
| 22346 |
amit.gupta |
20 |
public class SolrService {
|
| 28221 |
amit.gupta |
21 |
|
|
|
22 |
private static final Logger logger = LogManager.getLogger(SolrService.class);
|
|
|
23 |
|
| 26736 |
amit.gupta |
24 |
@Value("${new.solr.url}")
|
|
|
25 |
private String solrUrl;
|
| 28221 |
amit.gupta |
26 |
|
| 27030 |
amit.gupta |
27 |
@Autowired
|
|
|
28 |
private RestClient restClient;
|
| 28221 |
amit.gupta |
29 |
|
| 31330 |
tejbeer |
30 |
public String getContent(String queryTerm, List<Integer> categoryIds, List<String> brands, int limit,
|
|
|
31 |
boolean activeOnly) throws Exception {
|
|
|
32 |
|
|
|
33 |
JSONArray docs = this.getContentDocs(queryTerm, categoryIds, brands, limit, activeOnly);
|
|
|
34 |
return docs.toString();
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public JSONArray getContentDocs(String queryTerm, List<Integer> categoryIds, List<String> brands, int limit,
|
|
|
38 |
boolean activeOnly) throws Exception {
|
| 22346 |
amit.gupta |
39 |
Map<String, String> params = new HashMap<>();
|
| 25380 |
amit.gupta |
40 |
List<String> mandatoryQ = new ArrayList<>();
|
|
|
41 |
if (queryTerm != null && !queryTerm.equals("null")) {
|
| 28221 |
amit.gupta |
42 |
mandatoryQ.add(String.format("+(%s)", "*" + queryTerm + "*"));
|
| 25380 |
amit.gupta |
43 |
} else {
|
|
|
44 |
queryTerm = null;
|
|
|
45 |
}
|
| 27325 |
amit.gupta |
46 |
params.put("q", StringUtils.join(mandatoryQ, " "));
|
| 30267 |
amit.gupta |
47 |
if (categoryIds != null && categoryIds.size() > 0) {
|
| 30262 |
amit.gupta |
48 |
params.put("q", params.get("q") + " +filter(categoryId_i:(" + StringUtils.join(categoryIds, " ") + "))");
|
| 27601 |
amit.gupta |
49 |
}
|
| 28221 |
amit.gupta |
50 |
if (brands.size() > 0) {
|
| 31714 |
amit.gupta |
51 |
brands = brands.stream().map(x -> x.replaceAll(" ", "\\\\ ")).collect(Collectors.toList());
|
| 27601 |
amit.gupta |
52 |
params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");
|
|
|
53 |
}
|
| 30080 |
amit.gupta |
54 |
if (activeOnly) {
|
|
|
55 |
params.put("q", params.get("q") + " AND active_b:true");
|
|
|
56 |
}
|
| 25380 |
amit.gupta |
57 |
params.put("fl", "*");
|
|
|
58 |
if (queryTerm == null) {
|
|
|
59 |
params.put("sort", "create_s desc");
|
|
|
60 |
}
|
| 22346 |
amit.gupta |
61 |
params.put("start", String.valueOf(0));
|
| 28221 |
amit.gupta |
62 |
if (limit == 0) {
|
| 27603 |
amit.gupta |
63 |
params.put("fl", "catalogId_i, title_s");
|
|
|
64 |
params.put("rows", String.valueOf(5000));
|
|
|
65 |
} else {
|
| 28653 |
amit.gupta |
66 |
params.put("rows", String.valueOf(limit));
|
| 27603 |
amit.gupta |
67 |
}
|
| 22346 |
amit.gupta |
68 |
params.put("wt", "json");
|
| 25380 |
amit.gupta |
69 |
String response = null;
|
| 22346 |
amit.gupta |
70 |
try {
|
| 27030 |
amit.gupta |
71 |
response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
|
| 25380 |
amit.gupta |
72 |
} catch (HttpHostConnectException e) {
|
|
|
73 |
throw new ProfitMandiBusinessException("", "", "Could not connect to host");
|
| 22346 |
amit.gupta |
74 |
}
|
| 25380 |
amit.gupta |
75 |
JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
|
|
|
76 |
JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
|
| 31330 |
tejbeer |
77 |
return docs;
|
| 22346 |
amit.gupta |
78 |
}
|
| 25380 |
amit.gupta |
79 |
|
| 26606 |
amit.gupta |
80 |
public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
|
|
|
81 |
Map<Integer, JSONObject> documentMap = new HashMap<>();
|
|
|
82 |
Map<String, String> params = new HashMap<>();
|
|
|
83 |
params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
|
|
|
84 |
params.put("fl", "*");
|
|
|
85 |
params.put("start", String.valueOf(0));
|
| 28221 |
amit.gupta |
86 |
params.put("rows", String.valueOf(100));
|
| 26606 |
amit.gupta |
87 |
params.put("wt", "json");
|
|
|
88 |
String response = null;
|
|
|
89 |
try {
|
| 27030 |
amit.gupta |
90 |
response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
|
| 26606 |
amit.gupta |
91 |
} catch (HttpHostConnectException e) {
|
|
|
92 |
throw new ProfitMandiBusinessException("", "", "Could not connect to host");
|
|
|
93 |
}
|
|
|
94 |
JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
|
|
|
95 |
JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
|
|
|
96 |
for (int i = 0; i < docs.length(); i++) {
|
|
|
97 |
JSONObject doc = docs.getJSONObject(i);
|
|
|
98 |
documentMap.put(doc.getInt("catalogId_i"), doc);
|
|
|
99 |
}
|
|
|
100 |
return documentMap;
|
|
|
101 |
}
|
| 28221 |
amit.gupta |
102 |
|
| 28653 |
amit.gupta |
103 |
// This method is the used to pull docs based on search and shall be used
|
| 28221 |
amit.gupta |
104 |
// interchangably for both the things
|
| 33573 |
amit.gupta |
105 |
public JSONArray getSolrDocs(String queryTerm, String categoryId, int offset, int limit, String sort,
|
| 35369 |
ranu |
106 |
String brand, int subCategoryId, boolean hotDeal, boolean group, boolean eol_filter) throws Throwable {
|
| 27030 |
amit.gupta |
107 |
List<String> parentFilter = new ArrayList<>();
|
|
|
108 |
parentFilter.add("categoryId_i:" + categoryId);
|
| 31596 |
amit.gupta |
109 |
parentFilter.add("show_default_b:true");
|
| 27030 |
amit.gupta |
110 |
List<String> childFilter = new ArrayList<>();
|
|
|
111 |
childFilter.add("itemId_i:*");
|
|
|
112 |
|
|
|
113 |
Map<String, String> params = new HashMap<>();
|
|
|
114 |
if (queryTerm == null || queryTerm.equals("null")) {
|
|
|
115 |
queryTerm = "";
|
|
|
116 |
} else {
|
|
|
117 |
queryTerm = "(" + queryTerm + ")";
|
|
|
118 |
}
|
|
|
119 |
if (hotDeal) {
|
|
|
120 |
childFilter.add("hot_deal_b:true");
|
|
|
121 |
} else {
|
|
|
122 |
childFilter.add("active_b:true");
|
|
|
123 |
}
|
|
|
124 |
if (subCategoryId != 0) {
|
|
|
125 |
parentFilter.add("subCategoryId_i:" + subCategoryId);
|
|
|
126 |
}
|
| 35369 |
ranu |
127 |
|
|
|
128 |
if(eol_filter){
|
|
|
129 |
parentFilter.add("eol_no_stock_b:false");
|
|
|
130 |
}
|
| 27030 |
amit.gupta |
131 |
if (StringUtils.isNotBlank(brand)) {
|
| 27105 |
amit.gupta |
132 |
parentFilter.add("brand_ss:" + "\\\"" + brand + "\\\"");
|
| 27030 |
amit.gupta |
133 |
}
|
|
|
134 |
if (queryTerm == "") {
|
| 28221 |
amit.gupta |
135 |
params.put("sort", (sort == "" ? "" : sort + ", ") + "create_s desc");
|
| 27030 |
amit.gupta |
136 |
} else {
|
|
|
137 |
parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));
|
|
|
138 |
}
|
|
|
139 |
String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";
|
|
|
140 |
String childFilterString = String.join(" AND ", childFilter);
|
|
|
141 |
params.put("q", String.format("{!parent which=%s}%s", parentFilterString, childFilterString));
|
|
|
142 |
params.put("fl",
|
|
|
143 |
String.format("*, [child parentFilter=id:catalog* childFilter=%s]", "\"" + childFilterString + "\""));
|
|
|
144 |
params.put("start", String.valueOf(offset));
|
|
|
145 |
params.put("rows", String.valueOf(limit));
|
|
|
146 |
params.put("wt", "json");
|
| 34023 |
vikas.jang |
147 |
|
|
|
148 |
String groupByField = null;
|
|
|
149 |
|
|
|
150 |
if (group) {
|
|
|
151 |
groupByField = "superCatalog_s";
|
|
|
152 |
params.put("group", String.valueOf(group));
|
|
|
153 |
params.put("group.field", groupByField);
|
|
|
154 |
params.put("group.limit", "1");
|
|
|
155 |
if (!sort.isEmpty()) {
|
|
|
156 |
params.put("group.sort", sort);
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
logger.info("groupByField {}", groupByField);
|
| 27030 |
amit.gupta |
160 |
String response = null;
|
| 34023 |
vikas.jang |
161 |
|
| 27030 |
amit.gupta |
162 |
try {
|
|
|
163 |
response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
|
|
|
164 |
} catch (HttpHostConnectException e) {
|
|
|
165 |
throw new ProfitMandiBusinessException("", "", "Could not connect to host");
|
|
|
166 |
}
|
| 28221 |
amit.gupta |
167 |
|
| 34023 |
vikas.jang |
168 |
/*JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
|
|
|
169 |
JSONArray docs = solrResponseJSONObj.getJSONArray("docs");*/
|
|
|
170 |
|
|
|
171 |
JSONObject solrResponseJSONObj = new JSONObject(response);
|
|
|
172 |
JSONArray docs;
|
|
|
173 |
|
|
|
174 |
if (group) {
|
|
|
175 |
logger.info("Reached in if condition {}",groupByField);
|
|
|
176 |
JSONObject grouped = solrResponseJSONObj.getJSONObject("grouped");
|
|
|
177 |
JSONArray groups = grouped.getJSONObject(groupByField).getJSONArray("groups");
|
|
|
178 |
|
|
|
179 |
docs = new JSONArray();
|
|
|
180 |
for (int i = 0; i < groups.length(); i++) {
|
|
|
181 |
JSONObject groupObj = groups.getJSONObject(i);
|
|
|
182 |
JSONArray groupDocs = groupObj.getJSONObject("doclist").getJSONArray("docs");
|
|
|
183 |
for (int j = 0; j < groupDocs.length(); j++) {
|
|
|
184 |
docs.put(groupDocs.getJSONObject(j));
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
} else {
|
|
|
188 |
logger.info("Reached in else condition {}",groupByField);
|
|
|
189 |
docs = solrResponseJSONObj.getJSONObject("response").getJSONArray("docs");
|
|
|
190 |
}
|
| 34187 |
vikas.jang |
191 |
|
| 34186 |
vikas.jang |
192 |
logger.info("Reached at end {}",docs);
|
| 34023 |
vikas.jang |
193 |
|
| 27030 |
amit.gupta |
194 |
return docs;
|
|
|
195 |
}
|
|
|
196 |
|
| 22346 |
amit.gupta |
197 |
}
|