Subversion Repositories SmartDukaan

Rev

Rev 27603 | Rev 28653 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27603 Rev 28221
Line 6... Line 6...
6
import java.util.List;
6
import java.util.List;
7
import java.util.Map;
7
import java.util.Map;
8
 
8
 
9
import org.apache.commons.lang3.StringUtils;
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.http.conn.HttpHostConnectException;
10
import org.apache.http.conn.HttpHostConnectException;
-
 
11
import org.apache.logging.log4j.LogManager;
-
 
12
import org.apache.logging.log4j.Logger;
11
import org.json.JSONArray;
13
import org.json.JSONArray;
12
import org.json.JSONObject;
14
import org.json.JSONObject;
13
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Value;
16
import org.springframework.beans.factory.annotation.Value;
15
import org.springframework.stereotype.Service;
17
import org.springframework.stereotype.Service;
Line 18... Line 20...
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.web.client.RestClient;
21
import com.spice.profitmandi.common.web.client.RestClient;
20
 
22
 
21
@Service("solrServiceCommon")
23
@Service("solrServiceCommon")
22
public class SolrService {
24
public class SolrService {
-
 
25
 
-
 
26
	private static final Logger logger = LogManager.getLogger(SolrService.class);
23
	
27
 
24
	@Value("${new.solr.url}")
28
	@Value("${new.solr.url}")
25
	private String solrUrl;
29
	private String solrUrl;
26
	
30
 
27
	@Autowired
31
	@Autowired
28
	private RestClient restClient;
32
	private RestClient restClient;
-
 
33
 
29
	public String getContent(String queryTerm, int categoryId, List<String> brands, int limit) throws Exception {
34
	public String getContent(String queryTerm, int categoryId, List<String> brands, int limit) throws Exception {
30
		Map<String, String> params = new HashMap<>();
35
		Map<String, String> params = new HashMap<>();
31
		List<String> mandatoryQ = new ArrayList<>();
36
		List<String> mandatoryQ = new ArrayList<>();
32
		if (queryTerm != null && !queryTerm.equals("null")) {
37
		if (queryTerm != null && !queryTerm.equals("null")) {
33
			mandatoryQ.add(String.format("+(%s)", "*"+queryTerm + "*"));
38
			mandatoryQ.add(String.format("+(%s)", "*" + queryTerm + "*"));
34
		} else {
39
		} else {
35
			queryTerm = null;
40
			queryTerm = null;
36
		}
41
		}
37
		params.put("q", StringUtils.join(mandatoryQ, " "));
42
		params.put("q", StringUtils.join(mandatoryQ, " "));
38
		if(categoryId > 0) {
43
		if (categoryId > 0) {
39
			params.put("q", params.get("q") + " +filter(categoryId_i:" + categoryId + ")");
44
			params.put("q", params.get("q") + " +filter(categoryId_i:" + categoryId + ")");
40
		}
45
		}
41
		if(brands.size()>0) {
46
		if (brands.size() > 0) {
42
			params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");
47
			params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");
43
		}
48
		}
44
		params.put("fl", "*");
49
		params.put("fl", "*");
45
		if (queryTerm == null) {
50
		if (queryTerm == null) {
46
			params.put("sort", "create_s desc");
51
			params.put("sort", "create_s desc");
47
		}
52
		}
48
		params.put("start", String.valueOf(0));
53
		params.put("start", String.valueOf(0));
49
		if(limit==0) {
54
		if (limit == 0) {
50
			params.put("fl", "catalogId_i, title_s");
55
			params.put("fl", "catalogId_i, title_s");
51
			params.put("rows", String.valueOf(5000));
56
			params.put("rows", String.valueOf(5000));
52
		} else {
57
		} else {
53
			params.put("rows", String.valueOf(20));
58
			params.put("rows", String.valueOf(20));
54
		}
59
		}
Line 68... Line 73...
68
		Map<Integer, JSONObject> documentMap = new HashMap<>();
73
		Map<Integer, JSONObject> documentMap = new HashMap<>();
69
		Map<String, String> params = new HashMap<>();
74
		Map<String, String> params = new HashMap<>();
70
		params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
75
		params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
71
		params.put("fl", "*");
76
		params.put("fl", "*");
72
		params.put("start", String.valueOf(0));
77
		params.put("start", String.valueOf(0));
73
		params.put("rows", String.valueOf(50));
78
		params.put("rows", String.valueOf(100));
74
		params.put("wt", "json");
79
		params.put("wt", "json");
75
		String response = null;
80
		String response = null;
76
		try {
81
		try {
77
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
82
			response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);
78
		} catch (HttpHostConnectException e) {
83
		} catch (HttpHostConnectException e) {
Line 84... Line 89...
84
			JSONObject doc = docs.getJSONObject(i);
89
			JSONObject doc = docs.getJSONObject(i);
85
			documentMap.put(doc.getInt("catalogId_i"), doc);
90
			documentMap.put(doc.getInt("catalogId_i"), doc);
86
		}
91
		}
87
		return documentMap;
92
		return documentMap;
88
	}
93
	}
89
	
94
 
90
	//This method is the used to pull docs based on search nad shall be used interchangably for both the things
95
	// This method is the used to pull docs based on search nad shall be used
-
 
96
	// interchangably for both the things
91
	public JSONArray getSolrDocs(String queryTerm, String categoryId, String offset, String limit,
97
	public JSONArray getSolrDocs(String queryTerm, String categoryId, String offset, String limit, String sort,
92
			String sort, String brand, int subCategoryId, boolean hotDeal) throws Throwable {
98
			String brand, int subCategoryId, boolean hotDeal) throws Throwable {
93
		List<String> parentFilter = new ArrayList<>();
99
		List<String> parentFilter = new ArrayList<>();
94
		parentFilter.add("categoryId_i:" + categoryId);
100
		parentFilter.add("categoryId_i:" + categoryId);
95
 
101
 
96
		List<String> childFilter = new ArrayList<>();
102
		List<String> childFilter = new ArrayList<>();
97
		childFilter.add("itemId_i:*");
103
		childFilter.add("itemId_i:*");
Line 112... Line 118...
112
		}
118
		}
113
		if (StringUtils.isNotBlank(brand)) {
119
		if (StringUtils.isNotBlank(brand)) {
114
			parentFilter.add("brand_ss:" + "\\\"" + brand + "\\\"");
120
			parentFilter.add("brand_ss:" + "\\\"" + brand + "\\\"");
115
		}
121
		}
116
		if (queryTerm == "") {
122
		if (queryTerm == "") {
117
			params.put("sort", (sort=="" ? "" : sort + ", ")+"create_s desc" );
123
			params.put("sort", (sort == "" ? "" : sort + ", ") + "create_s desc");
118
		} else {
124
		} else {
119
			parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));
125
			parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));
120
		}
126
		}
121
		String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";
127
		String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";
122
		String childFilterString = String.join(" AND ", childFilter);
128
		String childFilterString = String.join(" AND ", childFilter);
Line 132... Line 138...
132
		} catch (HttpHostConnectException e) {
138
		} catch (HttpHostConnectException e) {
133
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
139
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
134
		}
140
		}
135
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
141
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
136
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
142
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
-
 
143
 
137
		return docs;
144
		return docs;
138
	}
145
	}
139
 
146
 
140
}
147
}