Subversion Repositories SmartDukaan

Rev

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