Subversion Repositories SmartDukaan

Rev

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