Subversion Repositories SmartDukaan

Rev

Rev 1921 | Rev 2157 | 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
 
339 rajveer 3
import in.shop2020.serving.services.SolrSearchService;
1999 vikas 4
import in.shop2020.serving.utils.DataLogger;
517 rajveer 5
import in.shop2020.serving.utils.FileUtils;
637 rajveer 6
import in.shop2020.serving.utils.Utils;
1999 vikas 7
import in.shop2020.serving.utils.DataLogger.Event;
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);
33
	private static Logger dataLog = DataLogger.getLogger();
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()){
1472 rajveer 87
			if(categoryId == 10001){
88
				location = "/all-mobile-phones/" + categoryId;
820 rajveer 89
				return "redirect";
1472 rajveer 90
			}else if(categoryId == 10011){
91
				location = "/all-mobile-accessories/" + categoryId;
92
				return "redirect";
820 rajveer 93
			}else{
94
				return "index";
95
			}
96
		}
569 rajveer 97
		sortUrl = url;
98
		if(this.request.getParameter("sort") != null){
99
    		url= url + "&sort=" + this.request.getParameter("sort");
100
    		sortOrder = this.request.getParameter("sort");
101
    	}
1921 vikas 102
		priceUrl += url;
545 rajveer 103
    	if(this.request.getParameter("page") != null){
104
    		this.page = Long.parseLong(this.request.getParameter("page"));
105
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 106
    	}
545 rajveer 107
    	if(this.request.getParameter("min-price") != null){
108
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
109
    		url= url + "&min-price=" + this.request.getParameter("min-price");
110
    	}
111
    	if(this.request.getParameter("max-price") != null){
112
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
113
    		url= url + "&max-price=" + this.request.getParameter("max-price");
114
    	}    	
339 rajveer 115
 
569 rajveer 116
 
339 rajveer 117
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 118
 
339 rajveer 119
    	this.crumbs = new ArrayList<String[]>();
120
 
569 rajveer 121
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
583 rajveer 122
    	String[] facetLabels = new String[] {"Category","Sub Category","Price",
569 rajveer 123
    	    	"Brand", "Data Connectivity", "Camera Resolution"
124
        	};
125
 
126
    	String urlCrumb = "";
127
    	if(query != null) {
128
	    	urlCrumb = url; 
129
	    }else {
130
 
131
    	}
132
 
339 rajveer 133
    	if(fqrys != null) {
134
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 135
	    	List<String> arrList = Arrays.asList(facetDefIDs);
136
	    	for(int i=0; i<fqrys.length; i++){
137
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
138
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
139
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
140
	    		String filterUrl = url;
545 rajveer 141
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 142
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 143
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
144
	    		for(int j=0; j<fqrys.length; j++) {
145
	    			if(i==j){
146
	    				continue;
147
	    			}
148
	    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");
149
		    	}
150
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
151
	    		log.info("acrumb=" + Arrays.toString(acrumb));
152
	    		this.crumbs.add(acrumb);
153
	    	}
154
    	}
155
    	url = urlCrumb;
517 rajveer 156
 
157
 
339 rajveer 158
 
569 rajveer 159
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);
790 vikas 160
 
354 rajveer 161
    	this.results =  search.getResultMap(); 
339 rajveer 162
 
545 rajveer 163
    	// Facets
339 rajveer 164
 
165
    	String qryString = this.request.getQueryString();
166
    	log.info("qryString=" + qryString);
167
 
545 rajveer 168
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 169
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 170
    		String facetDefID = facetDefIDs[i];
171
    		String facetLabel = facetLabels[i];
172
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
173
    		if(facetDetailMap==null)
174
    			continue;
175
    		List<String[]> values = new ArrayList<String[]>();
176
    		for(String facet: facetDetailMap.keySet()){
545 rajveer 177
    			String drilldownURL = url;  
178
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 179
    				URLEncoder.encode(facet, "UTF-8"); 
180
      			String[] afacet = new String[] { facet, 
181
      					facetDetailMap.get(facet).toString(), drilldownURL  };
182
      			values.add(afacet);    
183
 
184
    		}
185
 
186
    		this.facets.put(facetLabel, values);
187
    	}
188
 
189
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 190
    	if(priceMap != null){
191
    		this.minPrice = priceMap.get("min");
192
    		this.maxPrice = priceMap.get("max");
193
    	}else{
194
    		this.minPrice = 0.0;
195
    		this.maxPrice = 0.0;
196
    	}
354 rajveer 197
 
545 rajveer 198
    	this.totalResults = search.getTotalResults();
1999 vikas 199
        dataLog.info(StringUtils.join(
200
                new String[] { Event.PRODUCT_SEARCH.name(),
201
                        userinfo.getEmail(), query, Long.toString(categoryId),
202
                        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
}