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