Subversion Repositories SmartDukaan

Rev

Rev 3173 | Rev 3273 | 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;
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 = "?";
569 rajveer 63
	private long categoryId=10000;
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
 
569 rajveer 162
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);
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 {
210
    	if(results != null){
211
    		snippets = new HashMap<String, String>();	
790 vikas 212
	    	for(String docId: results){
517 rajveer 213
				snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"SearchSnippet.html"));
214
			}
215
    	}
216
		return snippets;
217
    }
218
 
219
    /**
220
     * 
221
     * @return
222
     */
223
    public String getQuery() {
224
    	return this.query;
225
    }
339 rajveer 226
 
517 rajveer 227
 
339 rajveer 228
    /**
229
     * 
230
     * @return
231
     */
790 vikas 232
    public List<String> getResults() {
339 rajveer 233
    	return this.results;
234
    }
235
 
236
    /**
237
     * 
238
     * @return
239
     */
240
    public Map<String, List<String[]>> getFacets() {
241
    	return this.facets;
242
    }
243
 
545 rajveer 244
    public long getTotalResults(){
245
    	return totalResults;
246
    }
247
 
248
    public String getUrl(){
249
    	return this.url;
250
    }
251
 
252
    public String getPriceUrl(){
253
    	return this.priceUrl;
254
    }
255
 
569 rajveer 256
    public String getSortUrl(){
257
    	return this.sortUrl;
258
    }
259
 
545 rajveer 260
	public long getBeginIndex(){
261
		if(totalResults>0)
262
			return beginIndex+1;
263
		return beginIndex;
264
	}
265
 
266
	public long getTotalPages() {
1376 rajveer 267
		return 1 + (totalResults-1)/windowSize;
545 rajveer 268
	}
269
 
270
	public long getCurrentPage() {
271
		return this.page;
272
	}
273
 
354 rajveer 274
    public Double getMinPrice() {
275
    	return this.minPrice;
276
    }
277
 
278
    public Double getMaxPrice() {
279
    	return this.maxPrice;
280
    }
281
 
339 rajveer 282
    public List<String[]> getCrumbs() {
283
    	return this.crumbs;
284
    }
569 rajveer 285
 
786 rajveer 286
    public String getSortOrder(){
287
    	return this.sortOrder;
288
    }
820 rajveer 289
 
290
    public String getLocation(){
291
    	return this.location;
292
    }
339 rajveer 293
}