Subversion Repositories SmartDukaan

Rev

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