Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
339 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
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;
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;
20
import org.apache.juli.logging.Log;
21
import org.apache.juli.logging.LogFactory;
832 rajveer 22
import org.apache.log4j.Logger;
820 rajveer 23
import org.apache.struts2.convention.annotation.Result;
24
import org.apache.struts2.convention.annotation.Results;
339 rajveer 25
import org.apache.struts2.rest.DefaultHttpHeaders;
26
import org.apache.struts2.rest.HttpHeaders;
27
 
28
/**
545 rajveer 29
 * @author rajveer
339 rajveer 30
 *
31
 */
820 rajveer 32
@Result(name="redirect", location="${location}", type="redirect")
517 rajveer 33
public class SearchController extends BaseController {
339 rajveer 34
 
35
	/**
36
	 * 
37
	 */
832 rajveer 38
	private static Logger log = Logger.getLogger(Class.class);	
790 vikas 39
	private List<String> results;
517 rajveer 40
 
339 rajveer 41
	/**
42
	 * 
43
	 */
517 rajveer 44
	private Map<String, String> snippets;
339 rajveer 45
 
46
	/**
47
	 * 
48
	 */
545 rajveer 49
	private Map<String, List<String[]>> facets = null;
339 rajveer 50
 
51
	/**
52
	 * 
53
	 */
54
	private List<String[]> crumbs;
55
 
517 rajveer 56
	private String  query;
569 rajveer 57
	private String sortOrder = null;
545 rajveer 58
	private Double minPrice = null;
59
	private Double maxPrice = null;
354 rajveer 60
 
545 rajveer 61
	private long windowSize = 10;
62
	private long page = 1;
63
	private long totalResults;
64
	private long beginIndex = 0;
65
	private String url;
66
	private String priceUrl;
569 rajveer 67
	private long categoryId=10000;
68
	private String sortUrl;
820 rajveer 69
	private String location;
339 rajveer 70
	/**
71
	 * 
72
	 * @return
73
	 * @throws UnsupportedEncodingException
74
	 */
75
    // GET /query
820 rajveer 76
 
77
    public String index() throws UnsupportedEncodingException {
517 rajveer 78
 
650 rajveer 79
    	log.info("this.request=" + this.request);
637 rajveer 80
 
545 rajveer 81
    	//url = this.request.getRequestURL();
82
		query = this.request.getParameter("q");
83
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 84
 
85
 
86
		if(this.request.getParameter("category") != null){
87
    		this.categoryId = Long.parseLong(this.request.getParameter("category"));
88
    		url= url + "&category=" + this.request.getParameter("category");
89
    	}
820 rajveer 90
		if(query.trim().isEmpty()){
91
			if(categoryId != 10000){
92
				location = "/category/" + categoryId;
93
				return "redirect";
94
			}else{
95
				return "index";
96
			}
97
		}
569 rajveer 98
		sortUrl = url;
99
		if(this.request.getParameter("sort") != null){
100
    		url= url + "&sort=" + this.request.getParameter("sort");
101
    		sortOrder = this.request.getParameter("sort");
102
    	}
545 rajveer 103
		priceUrl = url;
104
    	if(this.request.getParameter("page") != null){
105
    		this.page = Long.parseLong(this.request.getParameter("page"));
106
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 107
    	}
545 rajveer 108
    	if(this.request.getParameter("min-price") != null){
109
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
110
    		url= url + "&min-price=" + this.request.getParameter("min-price");
111
    	}
112
    	if(this.request.getParameter("max-price") != null){
113
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
114
    		url= url + "&max-price=" + this.request.getParameter("max-price");
115
    	}    	
339 rajveer 116
 
569 rajveer 117
 
339 rajveer 118
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 119
 
339 rajveer 120
    	this.crumbs = new ArrayList<String[]>();
121
 
569 rajveer 122
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
583 rajveer 123
    	String[] facetLabels = new String[] {"Category","Sub Category","Price",
569 rajveer 124
    	    	"Brand", "Data Connectivity", "Camera Resolution"
125
        	};
126
 
127
    	String urlCrumb = "";
128
    	if(query != null) {
129
	    	urlCrumb = url; 
130
	    }else {
131
 
132
    	}
133
 
339 rajveer 134
    	if(fqrys != null) {
135
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 136
	    	List<String> arrList = Arrays.asList(facetDefIDs);
137
	    	for(int i=0; i<fqrys.length; i++){
138
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
139
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
140
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
141
	    		String filterUrl = url;
545 rajveer 142
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 143
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 144
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
145
	    		for(int j=0; j<fqrys.length; j++) {
146
	    			if(i==j){
147
	    				continue;
148
	    			}
149
	    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");
150
		    	}
151
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
152
	    		log.info("acrumb=" + Arrays.toString(acrumb));
153
	    		this.crumbs.add(acrumb);
154
	    	}
155
    	}
156
    	url = urlCrumb;
517 rajveer 157
 
158
 
339 rajveer 159
 
569 rajveer 160
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);
790 vikas 161
 
354 rajveer 162
    	this.results =  search.getResultMap(); 
339 rajveer 163
 
545 rajveer 164
    	// Facets
339 rajveer 165
 
166
    	String qryString = this.request.getQueryString();
167
    	log.info("qryString=" + qryString);
168
 
545 rajveer 169
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 170
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 171
    		String facetDefID = facetDefIDs[i];
172
    		String facetLabel = facetLabels[i];
173
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
174
    		if(facetDetailMap==null)
175
    			continue;
176
    		List<String[]> values = new ArrayList<String[]>();
177
    		for(String facet: facetDetailMap.keySet()){
545 rajveer 178
    			String drilldownURL = url;  
179
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 180
    				URLEncoder.encode(facet, "UTF-8"); 
181
      			String[] afacet = new String[] { facet, 
182
      					facetDetailMap.get(facet).toString(), drilldownURL  };
183
      			values.add(afacet);    
184
 
185
    		}
186
 
187
    		this.facets.put(facetLabel, values);
188
    	}
189
 
190
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 191
    	if(priceMap != null){
192
    		this.minPrice = priceMap.get("min");
193
    		this.maxPrice = priceMap.get("max");
194
    	}else{
195
    		this.minPrice = 0.0;
196
    		this.maxPrice = 0.0;
197
    	}
354 rajveer 198
 
545 rajveer 199
    	this.totalResults = search.getTotalResults();
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() {
262
		return 1 + totalResults/windowSize;
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
}