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