| 339 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 517 |
rajveer |
6 |
import in.shop2020.serving.pages.PageContentKeys;
|
|
|
7 |
import in.shop2020.serving.pages.PageEnum;
|
|
|
8 |
import in.shop2020.serving.pages.PageManager;
|
|
|
9 |
import in.shop2020.serving.services.PageLoaderHandler;
|
| 339 |
rajveer |
10 |
import in.shop2020.serving.services.SolrSearchService;
|
| 517 |
rajveer |
11 |
import in.shop2020.serving.utils.FileUtils;
|
|
|
12 |
import in.shop2020.util.Utils;
|
| 339 |
rajveer |
13 |
|
| 517 |
rajveer |
14 |
import java.io.File;
|
| 339 |
rajveer |
15 |
import java.io.UnsupportedEncodingException;
|
|
|
16 |
import java.net.URLEncoder;
|
|
|
17 |
import java.util.ArrayList;
|
|
|
18 |
import java.util.Arrays;
|
| 354 |
rajveer |
19 |
import java.util.HashMap;
|
| 339 |
rajveer |
20 |
import java.util.LinkedHashMap;
|
|
|
21 |
import java.util.List;
|
|
|
22 |
import java.util.Map;
|
|
|
23 |
|
|
|
24 |
import javax.servlet.http.HttpServletRequest;
|
|
|
25 |
|
|
|
26 |
import org.apache.commons.lang.StringUtils;
|
|
|
27 |
import org.apache.juli.logging.Log;
|
|
|
28 |
import org.apache.juli.logging.LogFactory;
|
|
|
29 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
30 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
31 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @author naveen
|
|
|
35 |
*
|
|
|
36 |
*/
|
| 517 |
rajveer |
37 |
public class SearchController extends BaseController {
|
| 339 |
rajveer |
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
*/
|
|
|
42 |
private static Log log = LogFactory.getLog(SearchController.class);
|
|
|
43 |
|
| 517 |
rajveer |
44 |
private Map<String,String> htmlSnippets = new HashMap<String, String>();
|
| 339 |
rajveer |
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
*/
|
| 517 |
rajveer |
48 |
private Map<String, String[]> results;
|
|
|
49 |
|
| 339 |
rajveer |
50 |
/**
|
|
|
51 |
*
|
|
|
52 |
*/
|
| 517 |
rajveer |
53 |
private Map<String, String> snippets;
|
| 339 |
rajveer |
54 |
|
|
|
55 |
/**
|
|
|
56 |
*
|
|
|
57 |
*/
|
|
|
58 |
private Map<String, List<String[]>> facets;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
*
|
|
|
62 |
*/
|
|
|
63 |
private List<String[]> crumbs;
|
|
|
64 |
|
| 517 |
rajveer |
65 |
private String query;
|
|
|
66 |
|
| 354 |
rajveer |
67 |
private Double minPrice;
|
|
|
68 |
private Double maxPrice;
|
|
|
69 |
|
| 339 |
rajveer |
70 |
/**
|
|
|
71 |
*
|
|
|
72 |
* @return
|
|
|
73 |
* @throws UnsupportedEncodingException
|
|
|
74 |
*/
|
|
|
75 |
// GET /query
|
|
|
76 |
public HttpHeaders index() throws UnsupportedEncodingException {
|
| 517 |
rajveer |
77 |
|
|
|
78 |
PageLoaderHandler pageLoader = new PageLoaderHandler();
|
|
|
79 |
|
|
|
80 |
htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.getUserId(), userinfo.isSessionId(), userinfo.getNameOfUser()));
|
|
|
81 |
htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
|
|
|
82 |
htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
|
|
|
83 |
htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
|
|
|
84 |
htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isSessionId()));
|
|
|
85 |
htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
|
|
|
86 |
htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
|
|
|
87 |
htmlSnippets.put("SEARCH_HEADER","");
|
|
|
88 |
htmlSnippets.put("SEARCH_DETAILS","");
|
|
|
89 |
|
| 339 |
rajveer |
90 |
log.info("this.request=" + this.request);
|
|
|
91 |
|
| 517 |
rajveer |
92 |
query = this.request.getParameter("q");
|
|
|
93 |
log.info("query=" + query);
|
| 339 |
rajveer |
94 |
|
|
|
95 |
String[] fqrys = this.request.getParameterValues("fq");
|
|
|
96 |
|
|
|
97 |
this.crumbs = new ArrayList<String[]>();
|
|
|
98 |
|
|
|
99 |
String strCrumb = "";
|
|
|
100 |
String urlCrumb = "";
|
| 517 |
rajveer |
101 |
if(query != null) {
|
|
|
102 |
strCrumb = query;
|
|
|
103 |
urlCrumb = "q=" + URLEncoder.encode(query, "UTF-8");
|
| 339 |
rajveer |
104 |
this.crumbs.add(new String[] { strCrumb, urlCrumb } );
|
|
|
105 |
log.info("acrumb=" +
|
|
|
106 |
Arrays.toString(new String[] { strCrumb, urlCrumb }));
|
| 354 |
rajveer |
107 |
}else {
|
|
|
108 |
/**
|
|
|
109 |
* FIXME to handle if qry is null
|
|
|
110 |
*/
|
| 339 |
rajveer |
111 |
}
|
|
|
112 |
|
| 354 |
rajveer |
113 |
// HashMap<String,String> facetqueries = new HashMap<String, String>();
|
|
|
114 |
|
| 339 |
rajveer |
115 |
if(fqrys != null) {
|
|
|
116 |
log.info("fqrys=" + Arrays.toString(fqrys));
|
|
|
117 |
|
|
|
118 |
for(int i=0; i<fqrys.length; i++) {
|
|
|
119 |
String facetValue = StringUtils.split(fqrys[i], ":")[1];
|
| 354 |
rajveer |
120 |
// facetqueries.put(StringUtils.split(fqrys[i], ":")[0], StringUtils.split(fqrys[i], ":")[1]);
|
|
|
121 |
//strCrumb += " " + facetValue;
|
|
|
122 |
strCrumb = facetValue;
|
| 339 |
rajveer |
123 |
urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
124 |
String[] acrumb = new String[] { strCrumb, urlCrumb };
|
|
|
125 |
log.info("acrumb=" + Arrays.toString(acrumb));
|
|
|
126 |
|
|
|
127 |
this.crumbs.add(acrumb);
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
| 354 |
rajveer |
131 |
/**
|
|
|
132 |
* FIXME facte definition ids
|
|
|
133 |
*/
|
| 339 |
rajveer |
134 |
// Hard coded for now
|
| 354 |
rajveer |
135 |
// long[] facetDefIDs = new long[] {50001, 50003, 50004, 50005, 50006,
|
|
|
136 |
// 50007, 50008, 50009};
|
| 339 |
rajveer |
137 |
|
| 517 |
rajveer |
138 |
String[] facetDefIDs = new String[] {"Category","F_50002","F_50001", "F_50006", "F_50007" };
|
|
|
139 |
|
|
|
140 |
/* All facets. Removing 5 of them
|
| 354 |
rajveer |
141 |
String[] facetDefIDs = new String[] {"Category","F_50002","F_50001", "F_50003", "F_50004", "F_50005", "F_50006",
|
|
|
142 |
"F_50007", "F_50008", "F_50009"};
|
| 517 |
rajveer |
143 |
*/
|
|
|
144 |
|
|
|
145 |
|
| 354 |
rajveer |
146 |
/**
|
|
|
147 |
* FIXME facet labels
|
|
|
148 |
*/
|
| 339 |
rajveer |
149 |
// Hard-coded for now
|
| 517 |
rajveer |
150 |
/*
|
| 354 |
rajveer |
151 |
String[] facetLabels = new String[] {"Category","Price",
|
| 517 |
rajveer |
152 |
"Brand", "Form Factor", "Carry In Pocket", "Cellular Technologies",
|
|
|
153 |
"Data Connectivity", "Camera Resolution", "Built-in Memory",
|
|
|
154 |
"Talk time"
|
|
|
155 |
};
|
|
|
156 |
*/
|
| 339 |
rajveer |
157 |
|
| 517 |
rajveer |
158 |
String[] facetLabels = new String[] {"Category","Price",
|
|
|
159 |
"Brand", "Data Connectivity", "Camera Resolution"
|
|
|
160 |
};
|
|
|
161 |
|
|
|
162 |
SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs);
|
| 354 |
rajveer |
163 |
this.results = new LinkedHashMap<String, String[]>();
|
|
|
164 |
this.results = search.getResultMap();
|
| 339 |
rajveer |
165 |
|
| 354 |
rajveer |
166 |
/*
|
| 339 |
rajveer |
167 |
long[] entityIDs = search.getResultEntityIDs();
|
|
|
168 |
log.info("entityIDs=" + Arrays.toString(entityIDs));
|
|
|
169 |
|
|
|
170 |
String[] entityNames = search.getResultEntityNames();
|
|
|
171 |
log.info("entityNames=" + Arrays.toString(entityNames));
|
|
|
172 |
|
|
|
173 |
this.results = new LinkedHashMap<String, String[]>();
|
|
|
174 |
|
|
|
175 |
if(entityIDs != null) {
|
|
|
176 |
for(int i=0; i<entityIDs.length; i++) {
|
|
|
177 |
String key = (new Long(entityIDs[i])).toString();
|
|
|
178 |
|
|
|
179 |
String[] values = new String[] {entityNames[i]};
|
|
|
180 |
|
|
|
181 |
this.results.put(key, values);
|
|
|
182 |
}
|
|
|
183 |
}
|
| 354 |
rajveer |
184 |
*/
|
| 339 |
rajveer |
185 |
//log.info("this.results=" + this.results);
|
|
|
186 |
|
|
|
187 |
// Facets
|
|
|
188 |
this.facets = new LinkedHashMap<String, List<String[]>>();
|
|
|
189 |
String qryString = this.request.getQueryString();
|
|
|
190 |
log.info("qryString=" + qryString);
|
|
|
191 |
|
| 354 |
rajveer |
192 |
|
| 339 |
rajveer |
193 |
for (int i=0; i<facetDefIDs.length; i++) {
|
| 354 |
rajveer |
194 |
String facetDefID = facetDefIDs[i];
|
|
|
195 |
String facetLabel = facetLabels[i];
|
|
|
196 |
HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
|
|
|
197 |
if(facetDetailMap==null)
|
|
|
198 |
continue;
|
|
|
199 |
List<String[]> values = new ArrayList<String[]>();
|
|
|
200 |
for(String facet: facetDetailMap.keySet()){
|
|
|
201 |
// String extraInfo = "LINK";
|
|
|
202 |
// if(facetqueries.containsKey(facet)){
|
|
|
203 |
// extraInfo = "NO_LINK";
|
|
|
204 |
// }
|
|
|
205 |
String drilldownURL = qryString;
|
|
|
206 |
drilldownURL += "&fq=" + facetDefID + ":" +
|
|
|
207 |
URLEncoder.encode(facet, "UTF-8");
|
|
|
208 |
String[] afacet = new String[] { facet,
|
|
|
209 |
facetDetailMap.get(facet).toString(), drilldownURL };
|
|
|
210 |
values.add(afacet);
|
|
|
211 |
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
this.facets.put(facetLabel, values);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
Map<String, Double> priceMap = search.getPriceStatsMap();
|
| 517 |
rajveer |
218 |
if(priceMap != null){
|
|
|
219 |
this.minPrice = priceMap.get("min");
|
|
|
220 |
this.maxPrice = priceMap.get("max");
|
|
|
221 |
}else{
|
|
|
222 |
this.minPrice = 0.0;
|
|
|
223 |
this.maxPrice = 0.0;
|
|
|
224 |
}
|
| 354 |
rajveer |
225 |
/*
|
|
|
226 |
for (int i=0; i<facetDefIDs.length; i++) {
|
| 339 |
rajveer |
227 |
long facetDefID = facetDefIDs[i];
|
|
|
228 |
String facetLabel = facetLabels[i];
|
|
|
229 |
//log.info("facetLabel=" + facetLabel);
|
|
|
230 |
|
|
|
231 |
String[] facetValues = search.getFacetValues(facetDefID);
|
|
|
232 |
String[] facetCounts = search.getFacetCounts(facetDefID);
|
|
|
233 |
|
| 354 |
rajveer |
234 |
|
| 339 |
rajveer |
235 |
List<String[]> values = new ArrayList<String[]>();
|
| 354 |
rajveer |
236 |
|
|
|
237 |
|
| 339 |
rajveer |
238 |
for(int j=0; j<facetValues.length; j++) {
|
|
|
239 |
|
|
|
240 |
if(facetCounts[j].equals("0")) {
|
|
|
241 |
continue;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
String drilldownURL = qryString;
|
|
|
245 |
drilldownURL += "&fq=F_" + facetDefID + ":" +
|
|
|
246 |
URLEncoder.encode(facetValues[j], "UTF-8");
|
|
|
247 |
//log.info("drilldownURL=" + drilldownURL);
|
|
|
248 |
|
|
|
249 |
String[] afacet = new String[] { facetValues[j],
|
|
|
250 |
facetCounts[j], drilldownURL };
|
|
|
251 |
//log.info("afacet=" + Arrays.toString(afacet));
|
|
|
252 |
|
|
|
253 |
values.add(afacet);
|
|
|
254 |
}
|
|
|
255 |
//log.info("values=" + values);
|
|
|
256 |
|
|
|
257 |
this.facets.put(facetLabel, values);
|
| 354 |
rajveer |
258 |
|
| 339 |
rajveer |
259 |
}
|
|
|
260 |
//log.info("this.facets=" + this.facets);
|
| 354 |
rajveer |
261 |
*/
|
| 339 |
rajveer |
262 |
|
| 517 |
rajveer |
263 |
return new DefaultHttpHeaders("index")
|
| 339 |
rajveer |
264 |
.disableCaching();
|
|
|
265 |
}
|
| 517 |
rajveer |
266 |
|
|
|
267 |
|
|
|
268 |
public Map<String, String> getSnippets() throws Exception {
|
|
|
269 |
if(results != null){
|
|
|
270 |
snippets = new HashMap<String, String>();
|
|
|
271 |
for(String docId: results.keySet()){
|
|
|
272 |
snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"SearchSnippet.html"));
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
return snippets;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
*
|
|
|
280 |
* @return
|
|
|
281 |
*/
|
|
|
282 |
public String getQuery() {
|
|
|
283 |
return this.query;
|
|
|
284 |
}
|
| 339 |
rajveer |
285 |
|
| 517 |
rajveer |
286 |
|
| 339 |
rajveer |
287 |
/**
|
|
|
288 |
*
|
|
|
289 |
* @return
|
|
|
290 |
*/
|
|
|
291 |
public Map<String, String[]> getResults() {
|
|
|
292 |
return this.results;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
*
|
|
|
297 |
* @return
|
|
|
298 |
*/
|
|
|
299 |
public Map<String, List<String[]>> getFacets() {
|
|
|
300 |
return this.facets;
|
|
|
301 |
}
|
|
|
302 |
|
| 354 |
rajveer |
303 |
public Double getMinPrice() {
|
|
|
304 |
return this.minPrice;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
public Double getMaxPrice() {
|
|
|
308 |
return this.maxPrice;
|
|
|
309 |
}
|
|
|
310 |
|
| 339 |
rajveer |
311 |
/**
|
|
|
312 |
*
|
|
|
313 |
* @return
|
|
|
314 |
*/
|
|
|
315 |
public List<String[]> getCrumbs() {
|
|
|
316 |
return this.crumbs;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
/**
|
|
|
320 |
*
|
|
|
321 |
*/
|
| 517 |
rajveer |
322 |
|
|
|
323 |
public String getHeaderSnippet(){
|
|
|
324 |
return htmlSnippets.get("HEADER");
|
| 339 |
rajveer |
325 |
}
|
| 517 |
rajveer |
326 |
|
|
|
327 |
public String getMainMenuSnippet(){
|
|
|
328 |
return htmlSnippets.get("MAIN_MENU");
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
public String getSearchBarSnippet(){
|
|
|
332 |
return htmlSnippets.get("SEARCH_BAR");
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
public String getCustomerServiceSnippet(){
|
|
|
336 |
return htmlSnippets.get("CUSTOMER_SERVICE");
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
public String getSearchHeaderSnippet(){
|
|
|
340 |
return htmlSnippets.get("SEARCH_HEADER");
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
public String getSearchDetailsSnippet(){
|
|
|
344 |
return htmlSnippets.get("SEARCH_DETAILS");
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
public String getMyResearchSnippet(){
|
|
|
348 |
return htmlSnippets.get("MY_RESEARCH");
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
public String getFooterSnippet(){
|
|
|
352 |
return htmlSnippets.get("FOOTER");
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
public String getJsFileSnippet(){
|
|
|
356 |
return htmlSnippets.get("JS_FILES");
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
public String getCssFileSnippet(){
|
|
|
360 |
return htmlSnippets.get("CSS_FILES");
|
|
|
361 |
}
|
| 339 |
rajveer |
362 |
}
|