Rev 1921 | Rev 2183 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.serving.services.SolrSearchService;import in.shop2020.serving.utils.DataLogger;import in.shop2020.serving.utils.FileUtils;import in.shop2020.serving.utils.Utils;import in.shop2020.serving.utils.DataLogger.Event;import java.io.File;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.Result;/*** @author rajveer**/@Result(name="redirect", location="${location}", type="redirect")public class SearchController extends BaseController {private static final long serialVersionUID = -8392433517042806559L;private static Logger log = Logger.getLogger(Class.class);private static Logger dataLog = DataLogger.getLogger();private List<String> results;/****/private Map<String, String> snippets;/****/private Map<String, List<String[]>> facets = null;/****/private List<String[]> crumbs;private String query;private String sortOrder = null;private Double minPrice = null;private Double maxPrice = null;private long windowSize = 10;private long page = 1;private long totalResults;private long beginIndex = 0;private String url;private String priceUrl = "?";private long categoryId=10000;private String sortUrl;private String location;/**** @return* @throws UnsupportedEncodingException*/// GET /querypublic String index() throws UnsupportedEncodingException {log.info("this.request=" + this.request);//url = this.request.getRequestURL();query = this.request.getParameter("q");url = "q="+ URLEncoder.encode(query, "UTF-8");if(this.request.getParameter("category") != null){this.categoryId = Long.parseLong(this.request.getParameter("category"));url= url + "&category=" + this.request.getParameter("category");}if(query.trim().isEmpty()){if(categoryId == 10001){location = "/all-mobile-phones/" + categoryId;return "redirect";}else if(categoryId == 10011){location = "/all-mobile-accessories/" + categoryId;return "redirect";}else{return "index";}}sortUrl = url;if(this.request.getParameter("sort") != null){url= url + "&sort=" + this.request.getParameter("sort");sortOrder = this.request.getParameter("sort");}priceUrl += url;if(this.request.getParameter("page") != null){this.page = Long.parseLong(this.request.getParameter("page"));this.beginIndex = this.windowSize * (this.page-1);}if(this.request.getParameter("min-price") != null){this.minPrice = (new Double(this.request.getParameter("min-price")));url= url + "&min-price=" + this.request.getParameter("min-price");}if(this.request.getParameter("max-price") != null){this.maxPrice = (new Double(this.request.getParameter("max-price")));url= url + "&max-price=" + this.request.getParameter("max-price");}String[] fqrys = this.request.getParameterValues("fq");this.crumbs = new ArrayList<String[]>();String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001", "F_50006", "F_50007" };String[] facetLabels = new String[] {"Category","Sub Category","Price","Brand", "Data Connectivity", "Camera Resolution"};String urlCrumb = "";if(query != null) {urlCrumb = url;}else {}if(fqrys != null) {log.info("fqrys=" + Arrays.toString(fqrys));List<String> arrList = Arrays.asList(facetDefIDs);for(int i=0; i<fqrys.length; i++){urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");String filterUrl = url;String facetName = StringUtils.split(fqrys[i], ":")[0];String facetValue = StringUtils.split(fqrys[i], ":")[1];String facetLabel = facetLabels[arrList.indexOf(facetName)];for(int j=0; j<fqrys.length; j++) {if(i==j){continue;}filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");}String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };log.info("acrumb=" + Arrays.toString(acrumb));this.crumbs.add(acrumb);}}url = urlCrumb;SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);this.results = search.getResultMap();// FacetsString qryString = this.request.getQueryString();log.info("qryString=" + qryString);this.facets = new LinkedHashMap<String, List<String[]>>();for (int i=0; i<facetDefIDs.length; i++) {String facetDefID = facetDefIDs[i];String facetLabel = facetLabels[i];HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);if(facetDetailMap==null)continue;List<String[]> values = new ArrayList<String[]>();for(String facet: facetDetailMap.keySet()){String drilldownURL = url;drilldownURL += "&fq=" + facetDefID + ":" +URLEncoder.encode(facet, "UTF-8");String[] afacet = new String[] { facet,facetDetailMap.get(facet).toString(), drilldownURL };values.add(afacet);}this.facets.put(facetLabel, values);}Map<String, Double> priceMap = search.getPriceStatsMap();if(priceMap != null){this.minPrice = priceMap.get("min");this.maxPrice = priceMap.get("max");}else{this.minPrice = 0.0;this.maxPrice = 0.0;}this.totalResults = search.getTotalResults();dataLog.info(StringUtils.join(new String[] { Event.PRODUCT_SEARCH.name(),userinfo.getEmail(), query, Long.toString(categoryId),Long.toString(totalResults) }, ", "));return "index";}public Map<String, String> getSnippets() throws Exception {if(results != null){snippets = new HashMap<String, String>();for(String docId: results){snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"SearchSnippet.html"));}}return snippets;}/**** @return*/public String getQuery() {return this.query;}/**** @return*/public List<String> getResults() {return this.results;}/**** @return*/public Map<String, List<String[]>> getFacets() {return this.facets;}public long getTotalResults(){return totalResults;}public String getUrl(){return this.url;}public String getPriceUrl(){return this.priceUrl;}public String getSortUrl(){return this.sortUrl;}public long getBeginIndex(){if(totalResults>0)return beginIndex+1;return beginIndex;}public long getTotalPages() {return 1 + (totalResults-1)/windowSize;}public long getCurrentPage() {return this.page;}public Double getMinPrice() {return this.minPrice;}public Double getMaxPrice() {return this.maxPrice;}public List<String[]> getCrumbs() {return this.crumbs;}public String getSortOrder(){return this.sortOrder;}public String getLocation(){return this.location;}}