Subversion Repositories SmartDukaan

Rev

Rev 35369 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 35369 Rev 36923
Line 75... Line 75...
75
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
75
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
76
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
76
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
77
		return docs;
77
		return docs;
78
	}
78
	}
79
 
79
 
-
 
80
	/**
-
 
81
	 * Same query as {@link #getContentDocs} but also surfaces Solr's
-
 
82
	 * <code>response.numFound</code> so callers can show "showing X of Y" totals.
-
 
83
	 * Returns a small wrapper with the docs array (still capped by {@code limit})
-
 
84
	 * and the unlimited match total.
-
 
85
	 */
-
 
86
	public ContentSearchResult getContentWithTotal(String queryTerm, List<Integer> categoryIds, List<String> brands,
-
 
87
			int limit, boolean activeOnly) throws Exception {
-
 
88
		Map<String, String> params = new HashMap<>();
-
 
89
		List<String> mandatoryQ = new ArrayList<>();
-
 
90
		if (queryTerm != null && !queryTerm.equals("null")) {
-
 
91
			mandatoryQ.add(String.format("+(%s)", "*" + queryTerm + "*"));
-
 
92
		} else {
-
 
93
			queryTerm = null;
-
 
94
		}
-
 
95
		params.put("q", StringUtils.join(mandatoryQ, " "));
-
 
96
		if (categoryIds != null && categoryIds.size() > 0) {
-
 
97
			params.put("q", params.get("q") + " +filter(categoryId_i:(" + StringUtils.join(categoryIds, " ") + "))");
-
 
98
		}
-
 
99
		if (brands.size() > 0) {
-
 
100
			brands = brands.stream().map(x -> x.replaceAll(" ", "\\\\ ")).collect(Collectors.toList());
-
 
101
			params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");
-
 
102
		}
-
 
103
		if (activeOnly) {
-
 
104
			params.put("q", params.get("q") + " AND active_b:true");
-
 
105
		}
-
 
106
		params.put("fl", "*");
-
 
107
		if (queryTerm == null) {
-
 
108
			params.put("sort", "create_s desc");
-
 
109
		}
-
 
110
		params.put("start", String.valueOf(0));
-
 
111
		if (limit == 0) {
-
 
112
			params.put("fl", "catalogId_i, title_s");
-
 
113
			params.put("rows", String.valueOf(5000));
-
 
114
		} else {
-
 
115
			params.put("rows", String.valueOf(limit));
-
 
116
		}
-
 
117
		params.put("wt", "json");
-
 
118
		String response;
-
 
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
		long numFound = solrResponseJSONObj.optLong("numFound", docs.length());
-
 
127
		return new ContentSearchResult(docs, numFound);
-
 
128
	}
-
 
129
 
-
 
130
	/** Wrapper for {@link #getContentWithTotal}. */
-
 
131
	public static class ContentSearchResult {
-
 
132
		private final JSONArray docs;
-
 
133
		private final long totalCount;
-
 
134
 
-
 
135
		public ContentSearchResult(JSONArray docs, long totalCount) {
-
 
136
			this.docs = docs;
-
 
137
			this.totalCount = totalCount;
-
 
138
		}
-
 
139
 
-
 
140
		public JSONArray getDocs() { return docs; }
-
 
141
		public long getTotalCount() { return totalCount; }
-
 
142
	}
-
 
143
 
80
	public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
144
	public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {
81
		Map<Integer, JSONObject> documentMap = new HashMap<>();
145
		Map<Integer, JSONObject> documentMap = new HashMap<>();
82
		Map<String, String> params = new HashMap<>();
146
		Map<String, String> params = new HashMap<>();
83
		params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
147
		params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));
84
		params.put("fl", "*");
148
		params.put("fl", "*");