Subversion Repositories SmartDukaan

Rev

Rev 3278 | Rev 3656 | 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";
2606 rajveer 93
			}else if(categoryId == Utils.TABLETS_CATEGORY){
94
				location = "/all-tablets/" + categoryId;
95
				return "redirect";
820 rajveer 96
			}else{
97
				return "index";
98
			}
99
		}
569 rajveer 100
		sortUrl = url;
101
		if(this.request.getParameter("sort") != null){
102
    		url= url + "&sort=" + this.request.getParameter("sort");
103
    		sortOrder = this.request.getParameter("sort");
104
    	}
1921 vikas 105
		priceUrl += url;
545 rajveer 106
    	if(this.request.getParameter("page") != null){
107
    		this.page = Long.parseLong(this.request.getParameter("page"));
108
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 109
    	}
3173 rajveer 110
    	if(this.request.getParameter("minPrice") != null){
111
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
112
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 113
    	}
3173 rajveer 114
    	if(this.request.getParameter("maxPrice") != null){
115
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
116
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 117
    	}    	
339 rajveer 118
 
569 rajveer 119
 
339 rajveer 120
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 121
 
339 rajveer 122
    	this.crumbs = new ArrayList<String[]>();
123
 
569 rajveer 124
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
583 rajveer 125
    	String[] facetLabels = new String[] {"Category","Sub Category","Price",
569 rajveer 126
    	    	"Brand", "Data Connectivity", "Camera Resolution"
127
        	};
128
 
129
    	String urlCrumb = "";
130
    	if(query != null) {
131
	    	urlCrumb = url; 
132
	    }else {
133
 
134
    	}
135
 
339 rajveer 136
    	if(fqrys != null) {
137
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 138
	    	List<String> arrList = Arrays.asList(facetDefIDs);
139
	    	for(int i=0; i<fqrys.length; i++){
140
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
141
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
142
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
2948 chandransh 143
	    		StringBuilder filterUrl = new StringBuilder(url);
545 rajveer 144
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 145
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 146
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
147
	    		for(int j=0; j<fqrys.length; j++) {
148
	    			if(i==j){
149
	    				continue;
150
	    			}
2948 chandransh 151
	    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j], "UTF-8"));
569 rajveer 152
		    	}
2948 chandransh 153
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
569 rajveer 154
	    		log.info("acrumb=" + Arrays.toString(acrumb));
155
	    		this.crumbs.add(acrumb);
156
	    	}
157
    	}
158
    	url = urlCrumb;
517 rajveer 159
 
160
 
339 rajveer 161
 
3561 rajveer 162
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder, sourceId);
790 vikas 163
 
354 rajveer 164
    	this.results =  search.getResultMap(); 
339 rajveer 165
 
545 rajveer 166
    	// Facets
339 rajveer 167
 
168
    	String qryString = this.request.getQueryString();
169
    	log.info("qryString=" + qryString);
170
 
545 rajveer 171
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 172
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 173
    		String facetDefID = facetDefIDs[i];
174
    		String facetLabel = facetLabels[i];
175
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
176
    		if(facetDetailMap==null)
177
    			continue;
178
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 179
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
180
    		    String facet = facetEntry.getKey();
545 rajveer 181
    			String drilldownURL = url;  
182
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 183
    				URLEncoder.encode(facet, "UTF-8"); 
184
      			String[] afacet = new String[] { facet, 
2948 chandransh 185
      			      facetEntry.getValue().toString(), drilldownURL  };
354 rajveer 186
      			values.add(afacet);    
187
 
188
    		}
189
 
190
    		this.facets.put(facetLabel, values);
191
    	}
192
 
193
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 194
    	if(priceMap != null){
195
    		this.minPrice = priceMap.get("min");
196
    		this.maxPrice = priceMap.get("max");
197
    	}else{
198
    		this.minPrice = 0.0;
199
    		this.maxPrice = 0.0;
200
    	}
354 rajveer 201
 
545 rajveer 202
    	this.totalResults = search.getTotalResults();
3185 vikas 203
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 204
                query, Long.toString(categoryId), Long.toString(totalResults));
820 rajveer 205
    	return "index";
339 rajveer 206
    }
517 rajveer 207
 
208
 
209
    public Map<String, String> getSnippets() throws Exception {
3273 vikas 210
        if(results != null){
211
            snippets = new HashMap<String, String>();   
212
            for(String docId: results){
3561 rajveer 213
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.SEARCH_SNIPPET_CACHE_KEY, docId, sourceId);
3273 vikas 214
                if (snippet != null) {
215
                    snippets.put(docId, snippet);
216
                }
217
            }
218
        }
219
        return snippets;
517 rajveer 220
    }
221
 
222
    /**
223
     * 
224
     * @return
225
     */
226
    public String getQuery() {
227
    	return this.query;
228
    }
339 rajveer 229
 
517 rajveer 230
 
339 rajveer 231
    /**
232
     * 
233
     * @return
234
     */
790 vikas 235
    public List<String> getResults() {
339 rajveer 236
    	return this.results;
237
    }
238
 
239
    /**
240
     * 
241
     * @return
242
     */
243
    public Map<String, List<String[]>> getFacets() {
244
    	return this.facets;
245
    }
246
 
545 rajveer 247
    public long getTotalResults(){
248
    	return totalResults;
249
    }
250
 
251
    public String getUrl(){
252
    	return this.url;
253
    }
254
 
255
    public String getPriceUrl(){
256
    	return this.priceUrl;
257
    }
258
 
569 rajveer 259
    public String getSortUrl(){
260
    	return this.sortUrl;
261
    }
262
 
545 rajveer 263
	public long getBeginIndex(){
264
		if(totalResults>0)
265
			return beginIndex+1;
266
		return beginIndex;
267
	}
268
 
269
	public long getTotalPages() {
1376 rajveer 270
		return 1 + (totalResults-1)/windowSize;
545 rajveer 271
	}
272
 
273
	public long getCurrentPage() {
274
		return this.page;
275
	}
276
 
354 rajveer 277
    public Double getMinPrice() {
278
    	return this.minPrice;
279
    }
280
 
281
    public Double getMaxPrice() {
282
    	return this.maxPrice;
283
    }
284
 
339 rajveer 285
    public List<String[]> getCrumbs() {
286
    	return this.crumbs;
287
    }
569 rajveer 288
 
786 rajveer 289
    public String getSortOrder(){
290
    	return this.sortOrder;
291
    }
820 rajveer 292
 
293
    public String getLocation(){
294
    	return this.location;
295
    }
339 rajveer 296
}