| 339 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.serving.services.SolrSearchService;
|
|
|
7 |
|
|
|
8 |
import java.io.UnsupportedEncodingException;
|
|
|
9 |
import java.net.URLEncoder;
|
|
|
10 |
import java.util.ArrayList;
|
|
|
11 |
import java.util.Arrays;
|
| 354 |
rajveer |
12 |
import java.util.HashMap;
|
| 339 |
rajveer |
13 |
import java.util.LinkedHashMap;
|
|
|
14 |
import java.util.List;
|
|
|
15 |
import java.util.Map;
|
|
|
16 |
|
|
|
17 |
import javax.servlet.http.HttpServletRequest;
|
|
|
18 |
|
|
|
19 |
import org.apache.commons.lang.StringUtils;
|
|
|
20 |
import org.apache.juli.logging.Log;
|
|
|
21 |
import org.apache.juli.logging.LogFactory;
|
|
|
22 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
23 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
24 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @author naveen
|
|
|
28 |
*
|
|
|
29 |
*/
|
|
|
30 |
public class SearchController extends BaseController
|
|
|
31 |
implements ServletRequestAware {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
*
|
|
|
35 |
*/
|
|
|
36 |
private static Log log = LogFactory.getLog(SearchController.class);
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
*
|
|
|
40 |
*/
|
|
|
41 |
private HttpServletRequest request;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
*
|
|
|
45 |
*/
|
|
|
46 |
private Map<String, String[]> results;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
*
|
|
|
50 |
*/
|
|
|
51 |
private Map<String, List<String[]>> facets;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
*
|
|
|
55 |
*/
|
|
|
56 |
private List<String[]> crumbs;
|
|
|
57 |
|
| 354 |
rajveer |
58 |
private Double minPrice;
|
|
|
59 |
private Double maxPrice;
|
|
|
60 |
|
| 339 |
rajveer |
61 |
/**
|
|
|
62 |
*
|
|
|
63 |
* @return
|
|
|
64 |
* @throws UnsupportedEncodingException
|
|
|
65 |
*/
|
|
|
66 |
// GET /query
|
|
|
67 |
public HttpHeaders index() throws UnsupportedEncodingException {
|
|
|
68 |
log.info("this.request=" + this.request);
|
|
|
69 |
|
|
|
70 |
String qry = this.request.getParameter("q");
|
|
|
71 |
log.info("qry=" + qry);
|
|
|
72 |
|
|
|
73 |
String[] fqrys = this.request.getParameterValues("fq");
|
|
|
74 |
|
|
|
75 |
this.crumbs = new ArrayList<String[]>();
|
|
|
76 |
|
|
|
77 |
String strCrumb = "";
|
|
|
78 |
String urlCrumb = "";
|
|
|
79 |
if(qry != null) {
|
|
|
80 |
strCrumb = qry;
|
|
|
81 |
urlCrumb = "q=" + URLEncoder.encode(qry, "UTF-8");
|
|
|
82 |
this.crumbs.add(new String[] { strCrumb, urlCrumb } );
|
|
|
83 |
log.info("acrumb=" +
|
|
|
84 |
Arrays.toString(new String[] { strCrumb, urlCrumb }));
|
| 354 |
rajveer |
85 |
}else {
|
|
|
86 |
/**
|
|
|
87 |
* FIXME to handle if qry is null
|
|
|
88 |
*/
|
| 339 |
rajveer |
89 |
}
|
|
|
90 |
|
| 354 |
rajveer |
91 |
// HashMap<String,String> facetqueries = new HashMap<String, String>();
|
|
|
92 |
|
| 339 |
rajveer |
93 |
if(fqrys != null) {
|
|
|
94 |
log.info("fqrys=" + Arrays.toString(fqrys));
|
|
|
95 |
|
|
|
96 |
for(int i=0; i<fqrys.length; i++) {
|
|
|
97 |
String facetValue = StringUtils.split(fqrys[i], ":")[1];
|
| 354 |
rajveer |
98 |
// facetqueries.put(StringUtils.split(fqrys[i], ":")[0], StringUtils.split(fqrys[i], ":")[1]);
|
|
|
99 |
//strCrumb += " " + facetValue;
|
|
|
100 |
strCrumb = facetValue;
|
| 339 |
rajveer |
101 |
urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
102 |
String[] acrumb = new String[] { strCrumb, urlCrumb };
|
|
|
103 |
log.info("acrumb=" + Arrays.toString(acrumb));
|
|
|
104 |
|
|
|
105 |
this.crumbs.add(acrumb);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
| 354 |
rajveer |
109 |
/**
|
|
|
110 |
* FIXME facte definition ids
|
|
|
111 |
*/
|
| 339 |
rajveer |
112 |
// Hard coded for now
|
| 354 |
rajveer |
113 |
// long[] facetDefIDs = new long[] {50001, 50003, 50004, 50005, 50006,
|
|
|
114 |
// 50007, 50008, 50009};
|
| 339 |
rajveer |
115 |
|
| 354 |
rajveer |
116 |
String[] facetDefIDs = new String[] {"Category","F_50002","F_50001", "F_50003", "F_50004", "F_50005", "F_50006",
|
|
|
117 |
"F_50007", "F_50008", "F_50009"};
|
|
|
118 |
/**
|
|
|
119 |
* FIXME facet labels
|
|
|
120 |
*/
|
| 339 |
rajveer |
121 |
// Hard-coded for now
|
| 354 |
rajveer |
122 |
String[] facetLabels = new String[] {"Category","Price",
|
| 339 |
rajveer |
123 |
"Brand", "Form Factor", "Carry In Pocket", "Cellular Technologies",
|
|
|
124 |
"Data Connectivity", "Camera Resolution", "Built-in Memory",
|
|
|
125 |
"Talk time"
|
|
|
126 |
};
|
|
|
127 |
|
| 354 |
rajveer |
128 |
SolrSearchService search = new SolrSearchService(qry, fqrys, facetDefIDs);
|
|
|
129 |
this.results = new LinkedHashMap<String, String[]>();
|
|
|
130 |
this.results = search.getResultMap();
|
| 339 |
rajveer |
131 |
|
| 354 |
rajveer |
132 |
/*
|
| 339 |
rajveer |
133 |
long[] entityIDs = search.getResultEntityIDs();
|
|
|
134 |
log.info("entityIDs=" + Arrays.toString(entityIDs));
|
|
|
135 |
|
|
|
136 |
String[] entityNames = search.getResultEntityNames();
|
|
|
137 |
log.info("entityNames=" + Arrays.toString(entityNames));
|
|
|
138 |
|
|
|
139 |
this.results = new LinkedHashMap<String, String[]>();
|
|
|
140 |
|
|
|
141 |
if(entityIDs != null) {
|
|
|
142 |
for(int i=0; i<entityIDs.length; i++) {
|
|
|
143 |
String key = (new Long(entityIDs[i])).toString();
|
|
|
144 |
|
|
|
145 |
String[] values = new String[] {entityNames[i]};
|
|
|
146 |
|
|
|
147 |
this.results.put(key, values);
|
|
|
148 |
}
|
|
|
149 |
}
|
| 354 |
rajveer |
150 |
*/
|
| 339 |
rajveer |
151 |
//log.info("this.results=" + this.results);
|
|
|
152 |
|
|
|
153 |
// Facets
|
|
|
154 |
this.facets = new LinkedHashMap<String, List<String[]>>();
|
|
|
155 |
String qryString = this.request.getQueryString();
|
|
|
156 |
log.info("qryString=" + qryString);
|
|
|
157 |
|
| 354 |
rajveer |
158 |
|
| 339 |
rajveer |
159 |
for (int i=0; i<facetDefIDs.length; i++) {
|
| 354 |
rajveer |
160 |
String facetDefID = facetDefIDs[i];
|
|
|
161 |
String facetLabel = facetLabels[i];
|
|
|
162 |
HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
|
|
|
163 |
if(facetDetailMap==null)
|
|
|
164 |
continue;
|
|
|
165 |
List<String[]> values = new ArrayList<String[]>();
|
|
|
166 |
for(String facet: facetDetailMap.keySet()){
|
|
|
167 |
// String extraInfo = "LINK";
|
|
|
168 |
// if(facetqueries.containsKey(facet)){
|
|
|
169 |
// extraInfo = "NO_LINK";
|
|
|
170 |
// }
|
|
|
171 |
String drilldownURL = qryString;
|
|
|
172 |
drilldownURL += "&fq=" + facetDefID + ":" +
|
|
|
173 |
URLEncoder.encode(facet, "UTF-8");
|
|
|
174 |
String[] afacet = new String[] { facet,
|
|
|
175 |
facetDetailMap.get(facet).toString(), drilldownURL };
|
|
|
176 |
values.add(afacet);
|
|
|
177 |
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
this.facets.put(facetLabel, values);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
Map<String, Double> priceMap = search.getPriceStatsMap();
|
|
|
184 |
this.minPrice = priceMap.get("min");
|
|
|
185 |
this.maxPrice = priceMap.get("max");
|
|
|
186 |
/*
|
|
|
187 |
for (int i=0; i<facetDefIDs.length; i++) {
|
| 339 |
rajveer |
188 |
long facetDefID = facetDefIDs[i];
|
|
|
189 |
String facetLabel = facetLabels[i];
|
|
|
190 |
//log.info("facetLabel=" + facetLabel);
|
|
|
191 |
|
|
|
192 |
String[] facetValues = search.getFacetValues(facetDefID);
|
|
|
193 |
String[] facetCounts = search.getFacetCounts(facetDefID);
|
|
|
194 |
|
| 354 |
rajveer |
195 |
|
| 339 |
rajveer |
196 |
List<String[]> values = new ArrayList<String[]>();
|
| 354 |
rajveer |
197 |
|
|
|
198 |
|
| 339 |
rajveer |
199 |
for(int j=0; j<facetValues.length; j++) {
|
|
|
200 |
|
|
|
201 |
if(facetCounts[j].equals("0")) {
|
|
|
202 |
continue;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
String drilldownURL = qryString;
|
|
|
206 |
drilldownURL += "&fq=F_" + facetDefID + ":" +
|
|
|
207 |
URLEncoder.encode(facetValues[j], "UTF-8");
|
|
|
208 |
//log.info("drilldownURL=" + drilldownURL);
|
|
|
209 |
|
|
|
210 |
String[] afacet = new String[] { facetValues[j],
|
|
|
211 |
facetCounts[j], drilldownURL };
|
|
|
212 |
//log.info("afacet=" + Arrays.toString(afacet));
|
|
|
213 |
|
|
|
214 |
values.add(afacet);
|
|
|
215 |
}
|
|
|
216 |
//log.info("values=" + values);
|
|
|
217 |
|
|
|
218 |
this.facets.put(facetLabel, values);
|
| 354 |
rajveer |
219 |
|
| 339 |
rajveer |
220 |
}
|
|
|
221 |
//log.info("this.facets=" + this.facets);
|
| 354 |
rajveer |
222 |
*/
|
| 339 |
rajveer |
223 |
|
|
|
224 |
return new DefaultHttpHeaders("index")
|
|
|
225 |
.disableCaching();
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
*
|
|
|
230 |
* @return
|
|
|
231 |
*/
|
|
|
232 |
public Map<String, String[]> getResults() {
|
|
|
233 |
return this.results;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
*
|
|
|
238 |
* @return
|
|
|
239 |
*/
|
|
|
240 |
public Map<String, List<String[]>> getFacets() {
|
|
|
241 |
return this.facets;
|
|
|
242 |
}
|
|
|
243 |
|
| 354 |
rajveer |
244 |
public Double getMinPrice() {
|
|
|
245 |
return this.minPrice;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
public Double getMaxPrice() {
|
|
|
249 |
return this.maxPrice;
|
|
|
250 |
}
|
|
|
251 |
|
| 339 |
rajveer |
252 |
/**
|
|
|
253 |
*
|
|
|
254 |
* @return
|
|
|
255 |
*/
|
|
|
256 |
public List<String[]> getCrumbs() {
|
|
|
257 |
return this.crumbs;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
*
|
|
|
262 |
*/
|
|
|
263 |
@Override
|
|
|
264 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
265 |
this.request = request;
|
|
|
266 |
}
|
|
|
267 |
}
|