| 7226 |
anupam.sin |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.datalogger.EventType;
|
|
|
4 |
import in.shop2020.model.v1.catalog.Category;
|
|
|
5 |
//import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
|
|
|
6 |
//import in.shop2020.serving.cache.SnippetCacheWrapper;
|
| 7263 |
anupam.sin |
7 |
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
|
|
|
8 |
import in.shop2020.serving.cache.SnippetCacheWrapper;
|
| 7226 |
anupam.sin |
9 |
import in.shop2020.serving.service.SearchService;
|
|
|
10 |
import in.shop2020.serving.utils.Utils;
|
|
|
11 |
import in.shop2020.utils.CategoryManager;
|
|
|
12 |
import in.shop2020.utils.DataLogger;
|
|
|
13 |
|
|
|
14 |
import java.io.UnsupportedEncodingException;
|
|
|
15 |
import java.net.URLEncoder;
|
|
|
16 |
import java.util.ArrayList;
|
|
|
17 |
import java.util.Arrays;
|
|
|
18 |
import java.util.HashMap;
|
|
|
19 |
import java.util.LinkedHashMap;
|
|
|
20 |
import java.util.List;
|
|
|
21 |
import java.util.Map;
|
|
|
22 |
import java.util.Map.Entry;
|
|
|
23 |
|
|
|
24 |
import org.apache.commons.lang.StringUtils;
|
|
|
25 |
import org.apache.log4j.Logger;
|
|
|
26 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
27 |
|
|
|
28 |
import com.google.gson.Gson;
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* @author rajveer
|
|
|
33 |
*
|
|
|
34 |
*/
|
|
|
35 |
@Result(name="redirect", location="${location}", type="redirect")
|
|
|
36 |
public class SearchController extends BaseController {
|
|
|
37 |
|
|
|
38 |
private static final long serialVersionUID = -8392433517042806559L;
|
|
|
39 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
40 |
|
|
|
41 |
private List<String> results;
|
|
|
42 |
private Map<String, Double> dynamicSearchMap;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
*
|
|
|
46 |
*/
|
|
|
47 |
private Map<String, String> snippets;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
*
|
|
|
51 |
*/
|
|
|
52 |
private Map<String, List<String[]>> facets = null;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
*
|
|
|
56 |
*/
|
|
|
57 |
private Map<String, List<List<String>>> crumbs;
|
|
|
58 |
|
|
|
59 |
private String query;
|
| 7255 |
amit.gupta |
60 |
private String q;
|
| 7226 |
anupam.sin |
61 |
private String sortOrder = null;
|
|
|
62 |
private Double minPrice = null;
|
|
|
63 |
private Double maxPrice = null;
|
|
|
64 |
|
|
|
65 |
private long windowSize = 20;
|
|
|
66 |
private long page = 1;
|
|
|
67 |
private long totalResults;
|
|
|
68 |
private long beginIndex = 0;
|
|
|
69 |
private String url;
|
|
|
70 |
private String priceUrl = "?";
|
|
|
71 |
private String sortUrl;
|
|
|
72 |
private String location;
|
|
|
73 |
private static HashMap<String, List<String>> categoriesChildren = null;
|
|
|
74 |
private static Map<Long, String> categoryLabelMap = null;
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
*
|
|
|
78 |
* @return
|
|
|
79 |
* @throws UnsupportedEncodingException
|
|
|
80 |
*/
|
|
|
81 |
// GET /query
|
|
|
82 |
|
|
|
83 |
public String index() {
|
|
|
84 |
|
|
|
85 |
log.info("this.request=" + this.request);
|
|
|
86 |
|
|
|
87 |
//url = this.request.getRequestURL();
|
|
|
88 |
query = this.request.getParameter("q");
|
| 7255 |
amit.gupta |
89 |
q = this.query;
|
| 7226 |
anupam.sin |
90 |
try {
|
|
|
91 |
url = "q="+ URLEncoder.encode(query, "UTF-8");
|
|
|
92 |
|
|
|
93 |
if(query.trim().isEmpty()){
|
|
|
94 |
location = "/";
|
|
|
95 |
return "redirect";
|
|
|
96 |
}
|
|
|
97 |
sortUrl = url;
|
|
|
98 |
if(this.request.getParameter("sort") != null){
|
|
|
99 |
url= url + "&sort=" + this.request.getParameter("sort");
|
|
|
100 |
sortOrder = this.request.getParameter("sort");
|
|
|
101 |
}
|
|
|
102 |
priceUrl += url;
|
|
|
103 |
if(this.request.getParameter("page") != null){
|
|
|
104 |
this.page = Long.parseLong(this.request.getParameter("page"));
|
|
|
105 |
this.beginIndex = this.windowSize * (this.page-1);
|
|
|
106 |
}
|
|
|
107 |
if(this.request.getParameter("minPrice") != null){
|
|
|
108 |
this.minPrice = (new Double(this.request.getParameter("minPrice")));
|
|
|
109 |
url= url + "&minPrice=" + this.request.getParameter("minPrice");
|
|
|
110 |
}
|
|
|
111 |
if(this.request.getParameter("maxPrice") != null){
|
|
|
112 |
this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
|
|
|
113 |
url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
String[] fqrys = this.request.getParameterValues("fq");
|
|
|
118 |
|
|
|
119 |
this.crumbs = new HashMap<String, List<List<String>>>();
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
String urlCrumb = "";
|
|
|
123 |
if(query != null) {
|
|
|
124 |
urlCrumb = url;
|
|
|
125 |
}else {
|
|
|
126 |
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if(fqrys != null) {
|
|
|
130 |
log.info("fqrys=" + Arrays.toString(fqrys));
|
|
|
131 |
String filterUrl = "";
|
|
|
132 |
for(int i=0; i<fqrys.length; i++){
|
|
|
133 |
urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
134 |
priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
135 |
sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
136 |
String facetName = StringUtils.split(fqrys[i], ":")[0];
|
|
|
137 |
String facetValue = StringUtils.split(fqrys[i], ":")[1];
|
|
|
138 |
String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
|
|
|
139 |
filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
140 |
List<String> acrumb = Arrays.asList(facetValue, filterUrl);
|
|
|
141 |
if(!crumbs.containsKey(facetLabel)) {
|
|
|
142 |
crumbs.put(facetLabel, new ArrayList<List<String>>());
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
this.crumbs.get(facetLabel).add(acrumb);
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
url = urlCrumb;
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
SearchService search = new SearchService(query, fqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, sortOrder, sourceId);
|
|
|
153 |
|
|
|
154 |
this.results = search.getResultMap();
|
|
|
155 |
this.dynamicSearchMap = search.getDynamicPriceMap();
|
|
|
156 |
// Facets
|
|
|
157 |
List<String> fqs = null;
|
|
|
158 |
if (fqrys != null) {
|
|
|
159 |
fqs = Arrays.asList(fqrys);
|
|
|
160 |
}else {
|
|
|
161 |
fqs = new ArrayList<String>();
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
setFacet(fqs, search);
|
|
|
165 |
|
|
|
166 |
//If
|
|
|
167 |
Map<String, Double> priceMap = search.getPriceStatsMap();
|
|
|
168 |
if(priceMap != null){
|
|
|
169 |
if (this.minPrice == null){
|
|
|
170 |
this.minPrice = priceMap.get("min");
|
|
|
171 |
}
|
|
|
172 |
if(this.maxPrice == null) {
|
|
|
173 |
this.maxPrice = priceMap.get("max");
|
|
|
174 |
}
|
|
|
175 |
} else{
|
|
|
176 |
this.minPrice = 0.0;
|
|
|
177 |
this.maxPrice = 0.0;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
this.totalResults = search.getTotalResults();
|
|
|
181 |
} catch (Exception e) {
|
|
|
182 |
// TODO Auto-generated catch block
|
|
|
183 |
e.printStackTrace();
|
|
|
184 |
}
|
|
|
185 |
return "index";
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
private void setFacet(List<String> fqs, SearchService search) throws Exception{
|
|
|
190 |
|
|
|
191 |
this.facets = new LinkedHashMap<String, List<String[]>>();
|
|
|
192 |
List<String> filtrableFacets = search.getFilterableFacets();
|
|
|
193 |
for (String filtrableFacet : filtrableFacets) {
|
|
|
194 |
|
|
|
195 |
Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
|
|
|
196 |
if(facetDetailMap==null)
|
|
|
197 |
continue;
|
|
|
198 |
List<String[]> values = new ArrayList<String[]>();
|
|
|
199 |
for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
|
|
|
200 |
String selected = "";
|
|
|
201 |
if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
|
|
|
202 |
selected = "checked=\"checked\"";
|
|
|
203 |
}
|
|
|
204 |
String facet = facetEntry.getKey();
|
|
|
205 |
String drilldownURL = "&fq=" +
|
|
|
206 |
URLEncoder.encode( filtrableFacet + ":" + facet, "UTF-8");
|
|
|
207 |
String[] afacet = new String[] { facet,
|
|
|
208 |
facetEntry.getValue().toString(), drilldownURL, selected};
|
|
|
209 |
values.add(afacet);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
|
| 7263 |
anupam.sin |
217 |
public Map<String, String> getSnippets() throws Exception {
|
|
|
218 |
if(results != null){
|
|
|
219 |
snippets = new HashMap<String, String>();
|
|
|
220 |
for(String docId: results){
|
|
|
221 |
String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
|
|
|
222 |
if (snippet != null) {
|
|
|
223 |
snippets.put(docId, snippet);
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
return snippets;
|
|
|
228 |
}
|
| 7226 |
anupam.sin |
229 |
|
|
|
230 |
/**
|
|
|
231 |
*
|
|
|
232 |
* @return
|
|
|
233 |
*/
|
|
|
234 |
public String getQuery() {
|
|
|
235 |
return this.query;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
*
|
|
|
241 |
* @return
|
|
|
242 |
*/
|
|
|
243 |
public List<String> getResults() {
|
|
|
244 |
return this.results;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
/**
|
|
|
248 |
*
|
|
|
249 |
* @return
|
|
|
250 |
*/
|
|
|
251 |
public Map<String, List<String[]>> getFacets() {
|
|
|
252 |
return this.facets;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public long getTotalResults(){
|
|
|
256 |
return totalResults;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public String getUrl(){
|
|
|
260 |
return this.url;
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
public String getPriceUrl(){
|
|
|
264 |
return this.priceUrl;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
public String getSortUrl(){
|
|
|
268 |
return this.sortUrl;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
public long getBeginIndex(){
|
|
|
272 |
if(totalResults>0)
|
|
|
273 |
return beginIndex+1;
|
|
|
274 |
return beginIndex;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
public long getTotalPages() {
|
|
|
278 |
return 1 + (totalResults-1)/windowSize;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
public long getCurrentPage() {
|
|
|
282 |
return this.page;
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
public Double getMinPrice() {
|
|
|
286 |
return this.minPrice;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
public Double getMaxPrice() {
|
|
|
290 |
return this.maxPrice;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
public Map<String, List<List<String>>> getCrumbs() {
|
|
|
294 |
return this.crumbs;
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
public String getSortOrder(){
|
|
|
298 |
return this.sortOrder;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public String getLocation(){
|
|
|
302 |
return this.location;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
public List<String> getChildren(String categoryLabel) {
|
|
|
306 |
return getCategoriesChildren().get(categoryLabel);
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
public static Map<String, List<String>> getCategoriesChildren(){
|
|
|
310 |
Map<Long,String> idLabelMap = new HashMap<Long, String>();
|
|
|
311 |
if (categoriesChildren == null) {
|
|
|
312 |
categoriesChildren = new HashMap<String, List<String>>();
|
|
|
313 |
for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
|
|
|
314 |
idLabelMap.put(category.getId(), category.getLabel());
|
|
|
315 |
}
|
|
|
316 |
for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
|
|
|
317 |
if(category.getParent_category_id() != 10000 && category.getParent_category_id() != 0 &&
|
|
|
318 |
category.getParent_category_id() != Utils.MOBILE_PHONES_CATEGORY){
|
|
|
319 |
String parentLabel = idLabelMap.get(category.getParent_category_id());
|
|
|
320 |
if(!categoriesChildren.containsKey(parentLabel)){
|
|
|
321 |
categoriesChildren.put(parentLabel, new ArrayList<String>());
|
|
|
322 |
}
|
|
|
323 |
categoriesChildren.get(parentLabel).add(category.getLabel());
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
return categoriesChildren;
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
public static String getCategoryLabel(Long id){
|
|
|
332 |
if(categoryLabelMap == null){
|
|
|
333 |
Map<Long,String> idLabelMap = new HashMap<Long, String>();
|
|
|
334 |
for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
|
|
|
335 |
idLabelMap.put(category.getId(), category.getLabel());
|
|
|
336 |
}
|
|
|
337 |
categoryLabelMap = idLabelMap;
|
|
|
338 |
}
|
|
|
339 |
return categoryLabelMap.get(id);
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
public String getDynamicSearchMap(){
|
|
|
343 |
if (this.dynamicSearchMap == null) {
|
|
|
344 |
return "{}";
|
|
|
345 |
} else {
|
|
|
346 |
return new Gson().toJson(this.dynamicSearchMap);
|
|
|
347 |
}
|
|
|
348 |
}
|
| 7255 |
amit.gupta |
349 |
|
|
|
350 |
|
|
|
351 |
public void setQ(String q) {
|
|
|
352 |
this.q = q;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
|
|
|
356 |
public String getQ() {
|
|
|
357 |
return q;
|
|
|
358 |
}
|
| 7226 |
anupam.sin |
359 |
}
|