Subversion Repositories SmartDukaan

Rev

Rev 9840 | Rev 10169 | 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);
10113 amit.gupta 148
    	query= "\"" + query + "\"" + " OR " + query;
8354 amit.gupta 149
    	SolrSearchService search = new SolrSearchService(URLEncoder.encode(query, "UTF-8"), fqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, sortOrder, sourceId);
790 vikas 150
 
354 rajveer 151
    	this.results =  search.getResultMap(); 
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){
214
            snippets = new HashMap<String, String>();   
215
            for(String docId: results){
6949 amit.gupta 216
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3273 vikas 217
                if (snippet != null) {
218
                    snippets.put(docId, snippet);
219
                }
220
            }
221
        }
222
        return snippets;
517 rajveer 223
    }
224
 
225
    /**
226
     * 
227
     * @return
228
     */
229
    public String getQuery() {
230
    	return this.query;
231
    }
339 rajveer 232
 
517 rajveer 233
 
339 rajveer 234
    /**
235
     * 
236
     * @return
237
     */
790 vikas 238
    public List<String> getResults() {
339 rajveer 239
    	return this.results;
240
    }
241
 
242
    /**
243
     * 
244
     * @return
245
     */
246
    public Map<String, List<String[]>> getFacets() {
247
    	return this.facets;
248
    }
249
 
545 rajveer 250
    public long getTotalResults(){
251
    	return totalResults;
252
    }
253
 
254
    public String getUrl(){
255
    	return this.url;
256
    }
257
 
258
    public String getPriceUrl(){
259
    	return this.priceUrl;
260
    }
261
 
569 rajveer 262
    public String getSortUrl(){
263
    	return this.sortUrl;
264
    }
265
 
545 rajveer 266
	public long getBeginIndex(){
267
		if(totalResults>0)
268
			return beginIndex+1;
269
		return beginIndex;
270
	}
271
 
272
	public long getTotalPages() {
1376 rajveer 273
		return 1 + (totalResults-1)/windowSize;
545 rajveer 274
	}
275
 
276
	public long getCurrentPage() {
277
		return this.page;
278
	}
279
 
354 rajveer 280
    public Double getMinPrice() {
281
    	return this.minPrice;
282
    }
283
 
284
    public Double getMaxPrice() {
285
    	return this.maxPrice;
286
    }
287
 
6866 amit.gupta 288
    public Map<String, List<List<String>>> getCrumbs() {
339 rajveer 289
    	return this.crumbs;
290
    }
569 rajveer 291
 
786 rajveer 292
    public String getSortOrder(){
293
    	return this.sortOrder;
294
    }
820 rajveer 295
 
296
    public String getLocation(){
297
    	return this.location;
298
    }
6866 amit.gupta 299
 
300
    public List<String> getChildren(String categoryLabel) {
301
    	return getCategoriesChildren().get(categoryLabel);
302
    }
303
 
304
    public static Map<String, List<String>> getCategoriesChildren(){
305
    	Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
306
    	if (categoriesChildren == null) {
307
    		categoriesChildren = new HashMap<String, List<String>>();
308
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
309
    			idLabelMap.put(category.getId(), category.getLabel());
310
    		}
311
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
312
    			if(category.getParent_category_id() != 10000 && category.getParent_category_id() != 0 &&
313
    					category.getParent_category_id() != Utils.MOBILE_PHONES_CATEGORY){
314
    				String parentLabel = idLabelMap.get(category.getParent_category_id());
315
    				if(!categoriesChildren.containsKey(parentLabel)){
316
    					categoriesChildren.put(parentLabel, new ArrayList<String>());
317
    				}
318
    				categoriesChildren.get(parentLabel).add(category.getLabel());
319
    			}
320
    		}
321
    	}
322
 
323
    	return categoriesChildren;
324
    }
6931 amit.gupta 325
 
6998 amit.gupta 326
    public static String getCategoryLabel(Long id){
327
    	if(categoryLabelMap == null){
328
    		Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
329
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
330
    			idLabelMap.put(category.getId(), category.getLabel());
331
    		}
332
    		categoryLabelMap = idLabelMap;
333
    	} 
334
    	return categoryLabelMap.get(id);
335
    }
336
 
6931 amit.gupta 337
	public String getDynamicSearchMap(){
338
    	if (this.dynamicSearchMap == null) {
339
    		return "{}";
340
    	} else {
341
    		return new Gson().toJson(this.dynamicSearchMap);
342
    	}
343
    }
339 rajveer 344
}