Subversion Repositories SmartDukaan

Rev

Rev 3561 | Rev 4137 | 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;
3561 rajveer 4
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
5
import in.shop2020.serving.cache.SnippetCacheWrapper;
339 rajveer 6
import in.shop2020.serving.services.SolrSearchService;
637 rajveer 7
import in.shop2020.serving.utils.Utils;
2511 vikas 8
import in.shop2020.utils.DataLogger;
339 rajveer 9
 
10
import java.io.UnsupportedEncodingException;
11
import java.net.URLEncoder;
12
import java.util.ArrayList;
13
import java.util.Arrays;
354 rajveer 14
import java.util.HashMap;
339 rajveer 15
import java.util.LinkedHashMap;
16
import java.util.List;
17
import java.util.Map;
2948 chandransh 18
import java.util.Map.Entry;
339 rajveer 19
 
20
import org.apache.commons.lang.StringUtils;
832 rajveer 21
import org.apache.log4j.Logger;
820 rajveer 22
import org.apache.struts2.convention.annotation.Result;
339 rajveer 23
 
1044 chandransh 24
 
339 rajveer 25
/**
545 rajveer 26
 * @author rajveer
339 rajveer 27
 *
28
 */
820 rajveer 29
@Result(name="redirect", location="${location}", type="redirect")
517 rajveer 30
public class SearchController extends BaseController {
339 rajveer 31
 
1044 chandransh 32
	private static final long serialVersionUID = -8392433517042806559L;
1999 vikas 33
	private static Logger log = Logger.getLogger(Class.class);
1044 chandransh 34
 
790 vikas 35
	private List<String> results;
517 rajveer 36
 
339 rajveer 37
	/**
38
	 * 
39
	 */
517 rajveer 40
	private Map<String, String> snippets;
339 rajveer 41
 
42
	/**
43
	 * 
44
	 */
545 rajveer 45
	private Map<String, List<String[]>> facets = null;
339 rajveer 46
 
47
	/**
48
	 * 
49
	 */
50
	private List<String[]> crumbs;
51
 
517 rajveer 52
	private String  query;
569 rajveer 53
	private String sortOrder = null;
545 rajveer 54
	private Double minPrice = null;
55
	private Double maxPrice = null;
354 rajveer 56
 
545 rajveer 57
	private long windowSize = 10;
58
	private long page = 1;
59
	private long totalResults;
60
	private long beginIndex = 0;
61
	private String url;
1921 vikas 62
	private String priceUrl = "?";
3561 rajveer 63
	private long categoryId = Utils.ROOT_CATEGORY;
569 rajveer 64
	private String sortUrl;
820 rajveer 65
	private String location;
339 rajveer 66
	/**
67
	 * 
68
	 * @return
69
	 * @throws UnsupportedEncodingException
70
	 */
71
    // GET /query
820 rajveer 72
 
73
    public String index() throws UnsupportedEncodingException {
517 rajveer 74
 
650 rajveer 75
    	log.info("this.request=" + this.request);
637 rajveer 76
 
545 rajveer 77
    	//url = this.request.getRequestURL();
78
		query = this.request.getParameter("q");
79
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 80
 
81
 
82
		if(this.request.getParameter("category") != null){
83
    		this.categoryId = Long.parseLong(this.request.getParameter("category"));
84
    		url= url + "&category=" + this.request.getParameter("category");
85
    	}
820 rajveer 86
		if(query.trim().isEmpty()){
2606 rajveer 87
			if(categoryId == Utils.MOBILE_PHONES_CATEGORY){
1472 rajveer 88
				location = "/all-mobile-phones/" + categoryId;
820 rajveer 89
				return "redirect";
2606 rajveer 90
			}else if(categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY){
1472 rajveer 91
				location = "/all-mobile-accessories/" + categoryId;
92
				return "redirect";
3656 mandeep.dh 93
			} else if(categoryId == Utils.TABLETS_CATEGORY){
2606 rajveer 94
				location = "/all-tablets/" + categoryId;
95
				return "redirect";
3656 mandeep.dh 96
			} else if(categoryId == Utils.LAPTOPS_CATEGORY){
97
                location = "/all-laptops/" + categoryId;
98
                return "redirect";
99
            }else{
820 rajveer 100
				return "index";
101
			}
102
		}
569 rajveer 103
		sortUrl = url;
104
		if(this.request.getParameter("sort") != null){
105
    		url= url + "&sort=" + this.request.getParameter("sort");
106
    		sortOrder = this.request.getParameter("sort");
107
    	}
1921 vikas 108
		priceUrl += url;
545 rajveer 109
    	if(this.request.getParameter("page") != null){
110
    		this.page = Long.parseLong(this.request.getParameter("page"));
111
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 112
    	}
3173 rajveer 113
    	if(this.request.getParameter("minPrice") != null){
114
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
115
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 116
    	}
3173 rajveer 117
    	if(this.request.getParameter("maxPrice") != null){
118
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
119
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 120
    	}    	
339 rajveer 121
 
569 rajveer 122
 
339 rajveer 123
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 124
 
339 rajveer 125
    	this.crumbs = new ArrayList<String[]>();
126
 
569 rajveer 127
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
583 rajveer 128
    	String[] facetLabels = new String[] {"Category","Sub Category","Price",
569 rajveer 129
    	    	"Brand", "Data Connectivity", "Camera Resolution"
130
        	};
131
 
132
    	String urlCrumb = "";
133
    	if(query != null) {
134
	    	urlCrumb = url; 
135
	    }else {
136
 
137
    	}
138
 
339 rajveer 139
    	if(fqrys != null) {
140
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 141
	    	List<String> arrList = Arrays.asList(facetDefIDs);
142
	    	for(int i=0; i<fqrys.length; i++){
143
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
144
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
145
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
2948 chandransh 146
	    		StringBuilder filterUrl = new StringBuilder(url);
545 rajveer 147
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 148
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 149
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
150
	    		for(int j=0; j<fqrys.length; j++) {
151
	    			if(i==j){
152
	    				continue;
153
	    			}
2948 chandransh 154
	    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j], "UTF-8"));
569 rajveer 155
		    	}
2948 chandransh 156
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
569 rajveer 157
	    		log.info("acrumb=" + Arrays.toString(acrumb));
158
	    		this.crumbs.add(acrumb);
159
	    	}
160
    	}
161
    	url = urlCrumb;
517 rajveer 162
 
163
 
339 rajveer 164
 
3561 rajveer 165
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder, sourceId);
790 vikas 166
 
354 rajveer 167
    	this.results =  search.getResultMap(); 
339 rajveer 168
 
545 rajveer 169
    	// Facets
339 rajveer 170
 
171
    	String qryString = this.request.getQueryString();
172
    	log.info("qryString=" + qryString);
173
 
545 rajveer 174
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 175
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 176
    		String facetDefID = facetDefIDs[i];
177
    		String facetLabel = facetLabels[i];
178
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
179
    		if(facetDetailMap==null)
180
    			continue;
181
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 182
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
183
    		    String facet = facetEntry.getKey();
545 rajveer 184
    			String drilldownURL = url;  
185
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 186
    				URLEncoder.encode(facet, "UTF-8"); 
187
      			String[] afacet = new String[] { facet, 
2948 chandransh 188
      			      facetEntry.getValue().toString(), drilldownURL  };
354 rajveer 189
      			values.add(afacet);    
190
 
191
    		}
192
 
193
    		this.facets.put(facetLabel, values);
194
    	}
195
 
196
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 197
    	if(priceMap != null){
198
    		this.minPrice = priceMap.get("min");
199
    		this.maxPrice = priceMap.get("max");
200
    	}else{
201
    		this.minPrice = 0.0;
202
    		this.maxPrice = 0.0;
203
    	}
354 rajveer 204
 
545 rajveer 205
    	this.totalResults = search.getTotalResults();
3185 vikas 206
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 207
                query, Long.toString(categoryId), Long.toString(totalResults));
820 rajveer 208
    	return "index";
339 rajveer 209
    }
517 rajveer 210
 
211
 
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){
3561 rajveer 216
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.SEARCH_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
 
339 rajveer 288
    public List<String[]> getCrumbs() {
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
    }
339 rajveer 299
}