Subversion Repositories SmartDukaan

Rev

Rev 3273 | Rev 3561 | 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;
3273 vikas 4
import in.shop2020.serving.cache.SearchSnippetCacheWrapper;
339 rajveer 5
import in.shop2020.serving.services.SolrSearchService;
637 rajveer 6
import in.shop2020.serving.utils.Utils;
2511 vikas 7
import in.shop2020.utils.DataLogger;
339 rajveer 8
 
9
import java.io.UnsupportedEncodingException;
10
import java.net.URLEncoder;
11
import java.util.ArrayList;
12
import java.util.Arrays;
354 rajveer 13
import java.util.HashMap;
339 rajveer 14
import java.util.LinkedHashMap;
15
import java.util.List;
16
import java.util.Map;
2948 chandransh 17
import java.util.Map.Entry;
339 rajveer 18
 
19
import org.apache.commons.lang.StringUtils;
832 rajveer 20
import org.apache.log4j.Logger;
820 rajveer 21
import org.apache.struts2.convention.annotation.Result;
339 rajveer 22
 
1044 chandransh 23
 
339 rajveer 24
/**
545 rajveer 25
 * @author rajveer
339 rajveer 26
 *
27
 */
820 rajveer 28
@Result(name="redirect", location="${location}", type="redirect")
517 rajveer 29
public class SearchController extends BaseController {
339 rajveer 30
 
1044 chandransh 31
	private static final long serialVersionUID = -8392433517042806559L;
1999 vikas 32
	private static Logger log = Logger.getLogger(Class.class);
1044 chandransh 33
 
790 vikas 34
	private List<String> results;
517 rajveer 35
 
339 rajveer 36
	/**
37
	 * 
38
	 */
517 rajveer 39
	private Map<String, String> snippets;
339 rajveer 40
 
41
	/**
42
	 * 
43
	 */
545 rajveer 44
	private Map<String, List<String[]>> facets = null;
339 rajveer 45
 
46
	/**
47
	 * 
48
	 */
49
	private List<String[]> crumbs;
50
 
517 rajveer 51
	private String  query;
569 rajveer 52
	private String sortOrder = null;
545 rajveer 53
	private Double minPrice = null;
54
	private Double maxPrice = null;
354 rajveer 55
 
545 rajveer 56
	private long windowSize = 10;
57
	private long page = 1;
58
	private long totalResults;
59
	private long beginIndex = 0;
60
	private String url;
1921 vikas 61
	private String priceUrl = "?";
569 rajveer 62
	private long categoryId=10000;
63
	private String sortUrl;
820 rajveer 64
	private String location;
339 rajveer 65
	/**
66
	 * 
67
	 * @return
68
	 * @throws UnsupportedEncodingException
69
	 */
70
    // GET /query
820 rajveer 71
 
72
    public String index() throws UnsupportedEncodingException {
517 rajveer 73
 
650 rajveer 74
    	log.info("this.request=" + this.request);
637 rajveer 75
 
545 rajveer 76
    	//url = this.request.getRequestURL();
77
		query = this.request.getParameter("q");
78
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 79
 
80
 
81
		if(this.request.getParameter("category") != null){
82
    		this.categoryId = Long.parseLong(this.request.getParameter("category"));
83
    		url= url + "&category=" + this.request.getParameter("category");
84
    	}
820 rajveer 85
		if(query.trim().isEmpty()){
2606 rajveer 86
			if(categoryId == Utils.MOBILE_PHONES_CATEGORY){
1472 rajveer 87
				location = "/all-mobile-phones/" + categoryId;
820 rajveer 88
				return "redirect";
2606 rajveer 89
			}else if(categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY){
1472 rajveer 90
				location = "/all-mobile-accessories/" + categoryId;
91
				return "redirect";
2606 rajveer 92
			}else if(categoryId == Utils.TABLETS_CATEGORY){
93
				location = "/all-tablets/" + categoryId;
94
				return "redirect";
820 rajveer 95
			}else{
96
				return "index";
97
			}
98
		}
569 rajveer 99
		sortUrl = url;
100
		if(this.request.getParameter("sort") != null){
101
    		url= url + "&sort=" + this.request.getParameter("sort");
102
    		sortOrder = this.request.getParameter("sort");
103
    	}
1921 vikas 104
		priceUrl += url;
545 rajveer 105
    	if(this.request.getParameter("page") != null){
106
    		this.page = Long.parseLong(this.request.getParameter("page"));
107
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 108
    	}
3173 rajveer 109
    	if(this.request.getParameter("minPrice") != null){
110
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
111
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 112
    	}
3173 rajveer 113
    	if(this.request.getParameter("maxPrice") != null){
114
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
115
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 116
    	}    	
339 rajveer 117
 
569 rajveer 118
 
339 rajveer 119
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 120
 
339 rajveer 121
    	this.crumbs = new ArrayList<String[]>();
122
 
569 rajveer 123
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
583 rajveer 124
    	String[] facetLabels = new String[] {"Category","Sub Category","Price",
569 rajveer 125
    	    	"Brand", "Data Connectivity", "Camera Resolution"
126
        	};
127
 
128
    	String urlCrumb = "";
129
    	if(query != null) {
130
	    	urlCrumb = url; 
131
	    }else {
132
 
133
    	}
134
 
339 rajveer 135
    	if(fqrys != null) {
136
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 137
	    	List<String> arrList = Arrays.asList(facetDefIDs);
138
	    	for(int i=0; i<fqrys.length; i++){
139
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
140
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
141
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
2948 chandransh 142
	    		StringBuilder filterUrl = new StringBuilder(url);
545 rajveer 143
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 144
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 145
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
146
	    		for(int j=0; j<fqrys.length; j++) {
147
	    			if(i==j){
148
	    				continue;
149
	    			}
2948 chandransh 150
	    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j], "UTF-8"));
569 rajveer 151
		    	}
2948 chandransh 152
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
569 rajveer 153
	    		log.info("acrumb=" + Arrays.toString(acrumb));
154
	    		this.crumbs.add(acrumb);
155
	    	}
156
    	}
157
    	url = urlCrumb;
517 rajveer 158
 
159
 
339 rajveer 160
 
569 rajveer 161
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);
790 vikas 162
 
354 rajveer 163
    	this.results =  search.getResultMap(); 
339 rajveer 164
 
545 rajveer 165
    	// Facets
339 rajveer 166
 
167
    	String qryString = this.request.getQueryString();
168
    	log.info("qryString=" + qryString);
169
 
545 rajveer 170
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 171
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 172
    		String facetDefID = facetDefIDs[i];
173
    		String facetLabel = facetLabels[i];
174
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
175
    		if(facetDetailMap==null)
176
    			continue;
177
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 178
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
179
    		    String facet = facetEntry.getKey();
545 rajveer 180
    			String drilldownURL = url;  
181
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 182
    				URLEncoder.encode(facet, "UTF-8"); 
183
      			String[] afacet = new String[] { facet, 
2948 chandransh 184
      			      facetEntry.getValue().toString(), drilldownURL  };
354 rajveer 185
      			values.add(afacet);    
186
 
187
    		}
188
 
189
    		this.facets.put(facetLabel, values);
190
    	}
191
 
192
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 193
    	if(priceMap != null){
194
    		this.minPrice = priceMap.get("min");
195
    		this.maxPrice = priceMap.get("max");
196
    	}else{
197
    		this.minPrice = 0.0;
198
    		this.maxPrice = 0.0;
199
    	}
354 rajveer 200
 
545 rajveer 201
    	this.totalResults = search.getTotalResults();
3185 vikas 202
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 203
                query, Long.toString(categoryId), Long.toString(totalResults));
820 rajveer 204
    	return "index";
339 rajveer 205
    }
517 rajveer 206
 
207
 
208
    public Map<String, String> getSnippets() throws Exception {
3273 vikas 209
        if(results != null){
210
            snippets = new HashMap<String, String>();   
211
            for(String docId: results){
212
                String snippet = SearchSnippetCacheWrapper.getSnippet(docId);
213
                if (snippet != null) {
214
                    snippets.put(docId, snippet);
215
                }
216
            }
217
        }
218
        return snippets;
517 rajveer 219
    }
220
 
221
    /**
222
     * 
223
     * @return
224
     */
225
    public String getQuery() {
226
    	return this.query;
227
    }
339 rajveer 228
 
517 rajveer 229
 
339 rajveer 230
    /**
231
     * 
232
     * @return
233
     */
790 vikas 234
    public List<String> getResults() {
339 rajveer 235
    	return this.results;
236
    }
237
 
238
    /**
239
     * 
240
     * @return
241
     */
242
    public Map<String, List<String[]>> getFacets() {
243
    	return this.facets;
244
    }
245
 
545 rajveer 246
    public long getTotalResults(){
247
    	return totalResults;
248
    }
249
 
250
    public String getUrl(){
251
    	return this.url;
252
    }
253
 
254
    public String getPriceUrl(){
255
    	return this.priceUrl;
256
    }
257
 
569 rajveer 258
    public String getSortUrl(){
259
    	return this.sortUrl;
260
    }
261
 
545 rajveer 262
	public long getBeginIndex(){
263
		if(totalResults>0)
264
			return beginIndex+1;
265
		return beginIndex;
266
	}
267
 
268
	public long getTotalPages() {
1376 rajveer 269
		return 1 + (totalResults-1)/windowSize;
545 rajveer 270
	}
271
 
272
	public long getCurrentPage() {
273
		return this.page;
274
	}
275
 
354 rajveer 276
    public Double getMinPrice() {
277
    	return this.minPrice;
278
    }
279
 
280
    public Double getMaxPrice() {
281
    	return this.maxPrice;
282
    }
283
 
339 rajveer 284
    public List<String[]> getCrumbs() {
285
    	return this.crumbs;
286
    }
569 rajveer 287
 
786 rajveer 288
    public String getSortOrder(){
289
    	return this.sortOrder;
290
    }
820 rajveer 291
 
292
    public String getLocation(){
293
    	return this.location;
294
    }
339 rajveer 295
}