Subversion Repositories SmartDukaan

Rev

Rev 25386 | Rev 26606 | 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 javax.servlet.http.HttpServletRequest;
22346 amit.gupta 9
 
25380 amit.gupta 10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.http.conn.HttpHostConnectException;
12
import org.json.JSONArray;
13
import org.json.JSONObject;
14
import org.springframework.stereotype.Service;
15
 
16
import com.spice.profitmandi.common.enumuration.SchemeType;
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
18
import com.spice.profitmandi.common.web.client.RestClient;
19
 
25386 amit.gupta 20
@Service("solrServiceCommon")
22346 amit.gupta 21
public class SolrService {
25380 amit.gupta 22
	public String getContent( String queryTerm) throws Exception {
23
		RestClient rc = new RestClient();
22346 amit.gupta 24
		Map<String, String> params = new HashMap<>();
25380 amit.gupta 25
		List<String> mandatoryQ = new ArrayList<>();
26
		if (queryTerm != null && !queryTerm.equals("null")) {
27
			mandatoryQ.add(String.format("+(%s)", queryTerm));
28
		} else {
29
			queryTerm = null;
30
		}
31
		params.put("q", StringUtils.join(mandatoryQ, " "));
32
		params.put("fl", "*");
33
		if (queryTerm == null) {
34
			params.put("sort", "create_s desc");
35
		}
22346 amit.gupta 36
		params.put("start", String.valueOf(0));
25380 amit.gupta 37
		params.put("rows", String.valueOf(20));
22346 amit.gupta 38
		params.put("wt", "json");
25380 amit.gupta 39
		String response = null;
22346 amit.gupta 40
		try {
26581 amit.gupta 41
			response = rc.get(SchemeType.HTTP, "192.168.179.131", 8984, "solr/demo/select", params);
25380 amit.gupta 42
		} catch (HttpHostConnectException e) {
43
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
22346 amit.gupta 44
		}
25380 amit.gupta 45
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
46
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
47
		return docs.toString();
22346 amit.gupta 48
	}
25380 amit.gupta 49
 
22346 amit.gupta 50
}