Subversion Repositories SmartDukaan

Rev

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