| 22346 |
amit.gupta |
1 |
package com.spice.profitmandi.common.solr;
|
|
|
2 |
|
| 25380 |
amit.gupta |
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Map;
|
| 22346 |
amit.gupta |
7 |
|
| 25380 |
amit.gupta |
8 |
import org.apache.commons.lang3.StringUtils;
|
|
|
9 |
import org.apache.http.conn.HttpHostConnectException;
|
|
|
10 |
import org.json.JSONArray;
|
|
|
11 |
import org.json.JSONObject;
|
| 26736 |
amit.gupta |
12 |
import org.springframework.beans.factory.annotation.Value;
|
| 25380 |
amit.gupta |
13 |
import org.springframework.stereotype.Service;
|
|
|
14 |
|
|
|
15 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
|
|
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
17 |
import com.spice.profitmandi.common.web.client.RestClient;
|
|
|
18 |
|
| 25386 |
amit.gupta |
19 |
@Service("solrServiceCommon")
|
| 22346 |
amit.gupta |
20 |
public class SolrService {
|
| 26736 |
amit.gupta |
21 |
|
|
|
22 |
@Value("${new.solr.url}")
|
|
|
23 |
private String solrUrl;
|
| 26606 |
amit.gupta |
24 |
public String getContent(String queryTerm) throws Exception {
|
| 25380 |
amit.gupta |
25 |
RestClient rc = new RestClient();
|
| 22346 |
amit.gupta |
26 |
Map<String, String> params = new HashMap<>();
|
| 25380 |
amit.gupta |
27 |
List<String> mandatoryQ = new ArrayList<>();
|
|
|
28 |
if (queryTerm != null && !queryTerm.equals("null")) {
|
|
|
29 |
mandatoryQ.add(String.format("+(%s)", queryTerm));
|
|
|
30 |
} else {
|
|
|
31 |
queryTerm = null;
|
|
|
32 |
}
|
|
|
33 |
params.put("q", StringUtils.join(mandatoryQ, " "));
|
|
|
34 |
params.put("fl", "*");
|
|
|
35 |
if (queryTerm == null) {
|
|
|
36 |
params.put("sort", "create_s desc");
|
|
|
37 |
}
|
| 22346 |
amit.gupta |
38 |
params.put("start", String.valueOf(0));
|
| 25380 |
amit.gupta |
39 |
params.put("rows", String.valueOf(20));
|
| 22346 |
amit.gupta |
40 |
params.put("wt", "json");
|
| 25380 |
amit.gupta |
41 |
String response = null;
|
| 22346 |
amit.gupta |
42 |
try {
|
| 26736 |
amit.gupta |
43 |
response = rc.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
|
| 25380 |
amit.gupta |
44 |
} catch (HttpHostConnectException e) {
|
|
|
45 |
throw new ProfitMandiBusinessException("", "", "Could not connect to host");
|
| 22346 |
amit.gupta |
46 |
}
|
| 25380 |
amit.gupta |
47 |
JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
|
|
|
48 |
JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
|
|
|
49 |
return docs.toString();
|
| 22346 |
amit.gupta |
50 |
}
|
| 25380 |
amit.gupta |
51 |
|
| 26606 |
amit.gupta |
52 |
public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
|
|
|
53 |
RestClient rc = new RestClient();
|
|
|
54 |
Map<Integer, JSONObject> documentMap = new HashMap<>();
|
|
|
55 |
Map<String, String> params = new HashMap<>();
|
|
|
56 |
params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
|
|
|
57 |
params.put("fl", "*");
|
|
|
58 |
params.put("start", String.valueOf(0));
|
|
|
59 |
params.put("rows", String.valueOf(50));
|
|
|
60 |
params.put("wt", "json");
|
|
|
61 |
String response = null;
|
|
|
62 |
try {
|
| 26736 |
amit.gupta |
63 |
response = rc.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
|
| 26606 |
amit.gupta |
64 |
} catch (HttpHostConnectException e) {
|
|
|
65 |
throw new ProfitMandiBusinessException("", "", "Could not connect to host");
|
|
|
66 |
}
|
|
|
67 |
JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
|
|
|
68 |
JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
|
|
|
69 |
for (int i = 0; i < docs.length(); i++) {
|
|
|
70 |
JSONObject doc = docs.getJSONObject(i);
|
|
|
71 |
documentMap.put(doc.getInt("catalogId_i"), doc);
|
|
|
72 |
}
|
|
|
73 |
return documentMap;
|
|
|
74 |
}
|
|
|
75 |
|
| 22346 |
amit.gupta |
76 |
}
|