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