Subversion Repositories SmartDukaan

Rev

Rev 12106 | Rev 12141 | 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;
6931 amit.gupta 43
	private Map<String, Double> dynamicSearchMap;
517 rajveer 44
 
339 rajveer 45
	/**
46
	 * 
47
	 */
517 rajveer 48
	private Map<String, String> snippets;
339 rajveer 49
 
50
	/**
51
	 * 
52
	 */
545 rajveer 53
	private Map<String, List<String[]>> facets = null;
339 rajveer 54
 
55
	/**
56
	 * 
57
	 */
6866 amit.gupta 58
	private Map<String, List<List<String>>> crumbs;
339 rajveer 59
 
517 rajveer 60
	private String  query;
569 rajveer 61
	private String sortOrder = null;
545 rajveer 62
	private Double minPrice = null;
63
	private Double maxPrice = null;
354 rajveer 64
 
6942 amit.gupta 65
	private long windowSize = 20;
545 rajveer 66
	private long page = 1;
67
	private long totalResults;
68
	private long beginIndex = 0;
69
	private String url;
1921 vikas 70
	private String priceUrl = "?";
569 rajveer 71
	private String sortUrl;
820 rajveer 72
	private String location;
4137 varun.gupt 73
 
339 rajveer 74
	/**
75
	 * 
76
	 * @return
77
	 * @throws UnsupportedEncodingException
78
	 */
79
    // GET /query
820 rajveer 80
 
6866 amit.gupta 81
    public String index() throws Exception {
517 rajveer 82
 
637 rajveer 83
 
545 rajveer 84
		query = this.request.getParameter("q");
8327 amit.gupta 85
 
8324 amit.gupta 86
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 87
 
88
 
820 rajveer 89
		if(query.trim().isEmpty()){
6866 amit.gupta 90
				location = "/";
820 rajveer 91
				return "redirect";
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(); 
6931 amit.gupta 151
    	this.dynamicSearchMap = search.getDynamicPriceMap();
545 rajveer 152
    	// Facets
6866 amit.gupta 153
    	List<String> fqs = null;
154
    	if (fqrys != null) {
155
    		fqs = Arrays.asList(fqrys);
156
    	}else {
157
    		fqs = new ArrayList<String>();
158
    	}
339 rajveer 159
 
6866 amit.gupta 160
		setFacet(fqs, search);
339 rajveer 161
 
6931 amit.gupta 162
		//If 
6866 amit.gupta 163
    	Map<String, Double> priceMap = search.getPriceStatsMap();
164
    	if(priceMap != null){
6931 amit.gupta 165
    		if (this.minPrice == null){
166
    			this.minPrice = priceMap.get("min");
167
    		}
168
    		if(this.maxPrice == null) {
169
    			this.maxPrice = priceMap.get("max");
170
    		}
171
    	} else{
6866 amit.gupta 172
    		this.minPrice = 0.0;
173
    		this.maxPrice = 0.0;
174
    	}
175
 
176
    	this.totalResults = search.getTotalResults();
177
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
178
                query, Long.toString(Utils.ROOT_CATEGORY), Long.toString(totalResults));
179
    	return "index";
180
    }
181
 
182
 
183
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
184
 
185
    	this.facets = new LinkedHashMap<String, List<String[]>>();
6998 amit.gupta 186
    	List<String> filtrableFacets = search.getFilterableFacets();
187
    	for (String filtrableFacet : filtrableFacets) {
6866 amit.gupta 188
 
6998 amit.gupta 189
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
354 rajveer 190
    		if(facetDetailMap==null)
191
    			continue;
192
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 193
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
6866 amit.gupta 194
    			String selected = "";
6998 amit.gupta 195
    			if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
6866 amit.gupta 196
    				selected = "checked=\"checked\"";
197
    			}
2948 chandransh 198
    		    String facet = facetEntry.getKey();
6866 amit.gupta 199
      			String drilldownURL = "&fq=" +  
6998 amit.gupta 200
    				URLEncoder.encode( filtrableFacet + ":" + facet, "UTF-8"); 
354 rajveer 201
      			String[] afacet = new String[] { facet, 
6866 amit.gupta 202
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
354 rajveer 203
      			values.add(afacet);    
204
    		}
205
 
6998 amit.gupta 206
    		this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
354 rajveer 207
    	}
6866 amit.gupta 208
	}
517 rajveer 209
 
210
 
6866 amit.gupta 211
	public Map<String, String> getSnippets() throws Exception {
3273 vikas 212
        if(results != null){
213
            snippets = new HashMap<String, String>();   
214
            for(String docId: results){
6949 amit.gupta 215
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3273 vikas 216
                if (snippet != null) {
217
                    snippets.put(docId, snippet);
218
                }
219
            }
220
        }
221
        return snippets;
517 rajveer 222
    }
223
 
224
    /**
225
     * 
226
     * @return
227
     */
228
    public String getQuery() {
229
    	return this.query;
230
    }
339 rajveer 231
 
517 rajveer 232
 
339 rajveer 233
    /**
234
     * 
235
     * @return
236
     */
790 vikas 237
    public List<String> getResults() {
339 rajveer 238
    	return this.results;
239
    }
240
 
241
    /**
242
     * 
243
     * @return
244
     */
245
    public Map<String, List<String[]>> getFacets() {
246
    	return this.facets;
247
    }
248
 
545 rajveer 249
    public long getTotalResults(){
250
    	return totalResults;
251
    }
252
 
253
    public String getUrl(){
254
    	return this.url;
255
    }
256
 
257
    public String getPriceUrl(){
258
    	return this.priceUrl;
259
    }
260
 
569 rajveer 261
    public String getSortUrl(){
262
    	return this.sortUrl;
263
    }
264
 
545 rajveer 265
	public long getBeginIndex(){
266
		if(totalResults>0)
267
			return beginIndex+1;
268
		return beginIndex;
269
	}
270
 
271
	public long getTotalPages() {
1376 rajveer 272
		return 1 + (totalResults-1)/windowSize;
545 rajveer 273
	}
274
 
275
	public long getCurrentPage() {
276
		return this.page;
277
	}
278
 
354 rajveer 279
    public Double getMinPrice() {
280
    	return this.minPrice;
281
    }
282
 
283
    public Double getMaxPrice() {
284
    	return this.maxPrice;
285
    }
286
 
6866 amit.gupta 287
    public Map<String, List<List<String>>> getCrumbs() {
339 rajveer 288
    	return this.crumbs;
289
    }
569 rajveer 290
 
786 rajveer 291
    public String getSortOrder(){
292
    	return this.sortOrder;
293
    }
820 rajveer 294
 
295
    public String getLocation(){
296
    	return this.location;
297
    }
6866 amit.gupta 298
 
299
    public List<String> getChildren(String categoryLabel) {
300
    	return getCategoriesChildren().get(categoryLabel);
301
    }
302
 
303
    public static Map<String, List<String>> getCategoriesChildren(){
304
    	Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
305
    	if (categoriesChildren == null) {
306
    		categoriesChildren = new HashMap<String, List<String>>();
307
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
308
    			idLabelMap.put(category.getId(), category.getLabel());
309
    		}
310
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
311
    			if(category.getParent_category_id() != 10000 && category.getParent_category_id() != 0 &&
312
    					category.getParent_category_id() != Utils.MOBILE_PHONES_CATEGORY){
313
    				String parentLabel = idLabelMap.get(category.getParent_category_id());
314
    				if(!categoriesChildren.containsKey(parentLabel)){
315
    					categoriesChildren.put(parentLabel, new ArrayList<String>());
316
    				}
317
    				categoriesChildren.get(parentLabel).add(category.getLabel());
318
    			}
319
    		}
320
    	}
321
 
322
    	return categoriesChildren;
323
    }
6931 amit.gupta 324
 
6998 amit.gupta 325
    public static String getCategoryLabel(Long id){
326
    	if(categoryLabelMap == null){
327
    		Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
328
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
329
    			idLabelMap.put(category.getId(), category.getLabel());
330
    		}
331
    		categoryLabelMap = idLabelMap;
332
    	} 
333
    	return categoryLabelMap.get(id);
334
    }
335
 
6931 amit.gupta 336
	public String getDynamicSearchMap(){
337
    	if (this.dynamicSearchMap == null) {
338
    		return "{}";
339
    	} else {
340
    		return new Gson().toJson(this.dynamicSearchMap);
341
    	}
342
    }
339 rajveer 343
}