Subversion Repositories SmartDukaan

Rev

Rev 2511 | Rev 2948 | 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;
339 rajveer 4
import in.shop2020.serving.services.SolrSearchService;
517 rajveer 5
import in.shop2020.serving.utils.FileUtils;
637 rajveer 6
import in.shop2020.serving.utils.Utils;
2511 vikas 7
import in.shop2020.utils.DataLogger;
339 rajveer 8
 
517 rajveer 9
import java.io.File;
339 rajveer 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;
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
    	}
545 rajveer 109
    	if(this.request.getParameter("min-price") != null){
110
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
111
    		url= url + "&min-price=" + this.request.getParameter("min-price");
112
    	}
113
    	if(this.request.getParameter("max-price") != null){
114
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
115
    		url= url + "&max-price=" + this.request.getParameter("max-price");
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");
142
	    		String filterUrl = 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
	    			}
150
	    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");
151
		    	}
152
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
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[]>();
178
    		for(String facet: facetDetailMap.keySet()){
545 rajveer 179
    			String drilldownURL = url;  
180
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 181
    				URLEncoder.encode(facet, "UTF-8"); 
182
      			String[] afacet = new String[] { facet, 
183
      					facetDetailMap.get(facet).toString(), drilldownURL  };
184
      			values.add(afacet);    
185
 
186
    		}
187
 
188
    		this.facets.put(facetLabel, values);
189
    	}
190
 
191
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 192
    	if(priceMap != null){
193
    		this.minPrice = priceMap.get("min");
194
    		this.maxPrice = priceMap.get("max");
195
    	}else{
196
    		this.minPrice = 0.0;
197
    		this.maxPrice = 0.0;
198
    	}
354 rajveer 199
 
545 rajveer 200
    	this.totalResults = search.getTotalResults();
2419 vikas 201
        DataLogger.logData(EventType.PRODUCT_SEARCH, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 202
                query, Long.toString(categoryId), Long.toString(totalResults));
820 rajveer 203
    	return "index";
339 rajveer 204
    }
517 rajveer 205
 
206
 
207
    public Map<String, String> getSnippets() throws Exception {
208
    	if(results != null){
209
    		snippets = new HashMap<String, String>();	
790 vikas 210
	    	for(String docId: results){
517 rajveer 211
				snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"SearchSnippet.html"));
212
			}
213
    	}
214
		return snippets;
215
    }
216
 
217
    /**
218
     * 
219
     * @return
220
     */
221
    public String getQuery() {
222
    	return this.query;
223
    }
339 rajveer 224
 
517 rajveer 225
 
339 rajveer 226
    /**
227
     * 
228
     * @return
229
     */
790 vikas 230
    public List<String> getResults() {
339 rajveer 231
    	return this.results;
232
    }
233
 
234
    /**
235
     * 
236
     * @return
237
     */
238
    public Map<String, List<String[]>> getFacets() {
239
    	return this.facets;
240
    }
241
 
545 rajveer 242
    public long getTotalResults(){
243
    	return totalResults;
244
    }
245
 
246
    public String getUrl(){
247
    	return this.url;
248
    }
249
 
250
    public String getPriceUrl(){
251
    	return this.priceUrl;
252
    }
253
 
569 rajveer 254
    public String getSortUrl(){
255
    	return this.sortUrl;
256
    }
257
 
545 rajveer 258
	public long getBeginIndex(){
259
		if(totalResults>0)
260
			return beginIndex+1;
261
		return beginIndex;
262
	}
263
 
264
	public long getTotalPages() {
1376 rajveer 265
		return 1 + (totalResults-1)/windowSize;
545 rajveer 266
	}
267
 
268
	public long getCurrentPage() {
269
		return this.page;
270
	}
271
 
354 rajveer 272
    public Double getMinPrice() {
273
    	return this.minPrice;
274
    }
275
 
276
    public Double getMaxPrice() {
277
    	return this.maxPrice;
278
    }
279
 
339 rajveer 280
    public List<String[]> getCrumbs() {
281
    	return this.crumbs;
282
    }
569 rajveer 283
 
786 rajveer 284
    public String getSortOrder(){
285
    	return this.sortOrder;
286
    }
820 rajveer 287
 
288
    public String getLocation(){
289
    	return this.location;
290
    }
339 rajveer 291
}