Subversion Repositories SmartDukaan

Rev

Rev 12391 | Rev 12616 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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