Subversion Repositories SmartDukaan

Rev

Rev 12141 | Rev 12429 | 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
 
8324 amit.gupta 87
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 88
 
89
 
820 rajveer 90
		if(query.trim().isEmpty()){
12391 amit.gupta 91
				query="*";
820 rajveer 92
		}
569 rajveer 93
		sortUrl = url;
94
		if(this.request.getParameter("sort") != null){
95
    		url= url + "&sort=" + this.request.getParameter("sort");
96
    		sortOrder = this.request.getParameter("sort");
97
    	}
1921 vikas 98
		priceUrl += url;
545 rajveer 99
    	if(this.request.getParameter("page") != null){
100
    		this.page = Long.parseLong(this.request.getParameter("page"));
101
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 102
    	}
3173 rajveer 103
    	if(this.request.getParameter("minPrice") != null){
104
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
105
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 106
    	}
3173 rajveer 107
    	if(this.request.getParameter("maxPrice") != null){
108
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
109
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 110
    	}    	
339 rajveer 111
 
569 rajveer 112
 
339 rajveer 113
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 114
 
6866 amit.gupta 115
    	this.crumbs = new HashMap<String, List<List<String>>>();
339 rajveer 116
 
6998 amit.gupta 117
 
569 rajveer 118
    	String urlCrumb = "";
119
    	if(query != null) {
120
	    	urlCrumb = url; 
121
	    }else {
122
 
123
    	}
124
 
339 rajveer 125
    	if(fqrys != null) {
126
	    	log.info("fqrys=" + Arrays.toString(fqrys));
6866 amit.gupta 127
	    	String filterUrl = "";
569 rajveer 128
	    	for(int i=0; i<fqrys.length; i++){
129
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
130
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
131
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
545 rajveer 132
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 133
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
6998 amit.gupta 134
	    		String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
6866 amit.gupta 135
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
136
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
137
	    		if(!crumbs.containsKey(facetLabel)) {
138
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
139
	    		}
140
 
141
	    		this.crumbs.get(facetLabel).add(acrumb);
569 rajveer 142
	    	}
143
    	}
144
    	url = urlCrumb;
517 rajveer 145
 
146
 
8325 amit.gupta 147
    	log.info("Queried term:" + query);
12108 amit.gupta 148
    	SolrSearchService search = new SolrSearchService(URLEncoder.encode("\"" + query + "\"" + " OR " + query, "UTF-8"), fqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, sortOrder, sourceId, userinfo.isPrivateDealUser());
790 vikas 149
 
354 rajveer 150
    	this.results =  search.getResultMap(); 
12141 amit.gupta 151
    	this.dealResults =  search.getDealMap();    	
6931 amit.gupta 152
    	this.dynamicSearchMap = search.getDynamicPriceMap();
545 rajveer 153
    	// Facets
6866 amit.gupta 154
    	List<String> fqs = null;
155
    	if (fqrys != null) {
156
    		fqs = Arrays.asList(fqrys);
157
    	}else {
158
    		fqs = new ArrayList<String>();
159
    	}
339 rajveer 160
 
6866 amit.gupta 161
		setFacet(fqs, search);
339 rajveer 162
 
6931 amit.gupta 163
		//If 
6866 amit.gupta 164
    	Map<String, Double> priceMap = search.getPriceStatsMap();
165
    	if(priceMap != null){
6931 amit.gupta 166
    		if (this.minPrice == null){
167
    			this.minPrice = priceMap.get("min");
168
    		}
169
    		if(this.maxPrice == null) {
170
    			this.maxPrice = priceMap.get("max");
171
    		}
172
    	} else{
6866 amit.gupta 173
    		this.minPrice = 0.0;
174
    		this.maxPrice = 0.0;
175
    	}
176
 
177
    	this.totalResults = search.getTotalResults();
178
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
179
                query, Long.toString(Utils.ROOT_CATEGORY), Long.toString(totalResults));
180
    	return "index";
181
    }
182
 
183
 
184
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
185
 
186
    	this.facets = new LinkedHashMap<String, List<String[]>>();
6998 amit.gupta 187
    	List<String> filtrableFacets = search.getFilterableFacets();
188
    	for (String filtrableFacet : filtrableFacets) {
6866 amit.gupta 189
 
6998 amit.gupta 190
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
354 rajveer 191
    		if(facetDetailMap==null)
192
    			continue;
193
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 194
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
6866 amit.gupta 195
    			String selected = "";
6998 amit.gupta 196
    			if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
6866 amit.gupta 197
    				selected = "checked=\"checked\"";
198
    			}
2948 chandransh 199
    		    String facet = facetEntry.getKey();
6866 amit.gupta 200
      			String drilldownURL = "&fq=" +  
6998 amit.gupta 201
    				URLEncoder.encode( filtrableFacet + ":" + facet, "UTF-8"); 
354 rajveer 202
      			String[] afacet = new String[] { facet, 
6866 amit.gupta 203
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
354 rajveer 204
      			values.add(afacet);    
205
    		}
206
 
6998 amit.gupta 207
    		this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
354 rajveer 208
    	}
6866 amit.gupta 209
	}
517 rajveer 210
 
211
 
6866 amit.gupta 212
	public Map<String, String> getSnippets() throws Exception {
3273 vikas 213
        if(results != null){
12141 amit.gupta 214
    		snippets = new HashMap<String, String>();	
215
    		if(userinfo.isPrivateDealUser()) {
216
    			for(String docId: results){
217
	    			if(dealResults.contains(docId)) {
218
	    				String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.PRIVATE_DEAL_SNIPPET_KEY, docId, sourceId);
219
	    				if (snippet != null) {
220
	    					snippets.put(docId, snippet);
221
	    					continue;
222
	    				}
223
	    			}
224
	    			String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
225
	    			if (snippet != null) {
226
	    				snippets.put(docId, snippet);
227
	    			}
228
	    		} 
229
			} else {
230
				for(String docId: results){
231
					String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
232
					if (snippet != null) {
233
						snippets.put(docId, snippet);
234
					}
235
				} 
236
 
237
			}
238
    	}
239
		return snippets;
517 rajveer 240
    }
241
 
242
    /**
243
     * 
244
     * @return
245
     */
246
    public String getQuery() {
12391 amit.gupta 247
    	if(this.query.equals("*")){
248
    		return "";
249
    	}
517 rajveer 250
    	return this.query;
251
    }
339 rajveer 252
 
517 rajveer 253
 
339 rajveer 254
    /**
255
     * 
256
     * @return
257
     */
790 vikas 258
    public List<String> getResults() {
339 rajveer 259
    	return this.results;
260
    }
261
 
262
    /**
263
     * 
264
     * @return
265
     */
266
    public Map<String, List<String[]>> getFacets() {
267
    	return this.facets;
268
    }
269
 
545 rajveer 270
    public long getTotalResults(){
271
    	return totalResults;
272
    }
273
 
274
    public String getUrl(){
275
    	return this.url;
276
    }
277
 
278
    public String getPriceUrl(){
279
    	return this.priceUrl;
280
    }
281
 
569 rajveer 282
    public String getSortUrl(){
283
    	return this.sortUrl;
284
    }
285
 
545 rajveer 286
	public long getBeginIndex(){
287
		if(totalResults>0)
288
			return beginIndex+1;
289
		return beginIndex;
290
	}
291
 
292
	public long getTotalPages() {
1376 rajveer 293
		return 1 + (totalResults-1)/windowSize;
545 rajveer 294
	}
295
 
296
	public long getCurrentPage() {
297
		return this.page;
298
	}
299
 
354 rajveer 300
    public Double getMinPrice() {
301
    	return this.minPrice;
302
    }
303
 
304
    public Double getMaxPrice() {
305
    	return this.maxPrice;
306
    }
307
 
6866 amit.gupta 308
    public Map<String, List<List<String>>> getCrumbs() {
339 rajveer 309
    	return this.crumbs;
310
    }
569 rajveer 311
 
786 rajveer 312
    public String getSortOrder(){
313
    	return this.sortOrder;
314
    }
820 rajveer 315
 
316
    public String getLocation(){
317
    	return this.location;
318
    }
6866 amit.gupta 319
 
320
    public List<String> getChildren(String categoryLabel) {
321
    	return getCategoriesChildren().get(categoryLabel);
322
    }
323
 
324
    public static Map<String, List<String>> getCategoriesChildren(){
325
    	Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
326
    	if (categoriesChildren == null) {
327
    		categoriesChildren = new HashMap<String, List<String>>();
328
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
329
    			idLabelMap.put(category.getId(), category.getLabel());
330
    		}
331
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
332
    			if(category.getParent_category_id() != 10000 && category.getParent_category_id() != 0 &&
333
    					category.getParent_category_id() != Utils.MOBILE_PHONES_CATEGORY){
334
    				String parentLabel = idLabelMap.get(category.getParent_category_id());
335
    				if(!categoriesChildren.containsKey(parentLabel)){
336
    					categoriesChildren.put(parentLabel, new ArrayList<String>());
337
    				}
338
    				categoriesChildren.get(parentLabel).add(category.getLabel());
339
    			}
340
    		}
341
    	}
342
 
343
    	return categoriesChildren;
344
    }
6931 amit.gupta 345
 
6998 amit.gupta 346
    public static String getCategoryLabel(Long id){
347
    	if(categoryLabelMap == null){
348
    		Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
349
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
350
    			idLabelMap.put(category.getId(), category.getLabel());
351
    		}
352
    		categoryLabelMap = idLabelMap;
353
    	} 
354
    	return categoryLabelMap.get(id);
355
    }
356
 
6931 amit.gupta 357
	public String getDynamicSearchMap(){
358
    	if (this.dynamicSearchMap == null) {
359
    		return "{}";
360
    	} else {
361
    		return new Gson().toJson(this.dynamicSearchMap);
362
    	}
363
    }
339 rajveer 364
}