Subversion Repositories SmartDukaan

Rev

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