Subversion Repositories SmartDukaan

Rev

Rev 26581 | Rev 26736 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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