Subversion Repositories SmartDukaan

Rev

Rev 6931 | Rev 7132 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6931 Rev 6998
Line 3... Line 3...
3
 */
3
 */
4
package in.shop2020.serving.services;
4
package in.shop2020.serving.services;
5
 
5
 
6
 
6
 
7
import in.shop2020.config.ConfigException;
7
import in.shop2020.config.ConfigException;
-
 
8
import in.shop2020.serving.controllers.SearchController;
8
import in.shop2020.serving.utils.Utils;
9
import in.shop2020.serving.utils.Utils;
9
import in.shop2020.thrift.clients.config.ConfigClient;
10
import in.shop2020.thrift.clients.config.ConfigClient;
10
import in.shop2020.utils.CategoryManager;
-
 
11
 
11
 
12
import java.util.ArrayList;
12
import java.util.ArrayList;
13
import java.util.Arrays;
13
import java.util.Arrays;
14
import java.util.Collections;
14
import java.util.Collections;
15
import java.util.HashMap;
15
import java.util.HashMap;
Line 27... Line 27...
27
import javax.xml.xpath.XPath;
27
import javax.xml.xpath.XPath;
28
import javax.xml.xpath.XPathConstants;
28
import javax.xml.xpath.XPathConstants;
29
import javax.xml.xpath.XPathExpressionException;
29
import javax.xml.xpath.XPathExpressionException;
30
import javax.xml.xpath.XPathFactory;
30
import javax.xml.xpath.XPathFactory;
31
 
31
 
-
 
32
import org.apache.commons.collections.ListUtils;
32
import org.apache.log4j.Logger;
33
import org.apache.log4j.Logger;
33
import org.w3c.dom.Node;
34
import org.w3c.dom.Node;
34
import org.w3c.dom.NodeList;
35
import org.w3c.dom.NodeList;
35
import org.xml.sax.InputSource;
36
import org.xml.sax.InputSource;
36
 
37
 
Line 57... Line 58...
57
			new HashMap<String, List<String>>(){
58
			new HashMap<String, List<String>>(){
58
				/**
59
				/**
59
				 * 
60
				 * 
60
				 */
61
				 */
61
				private static final long serialVersionUID = 1L;
62
				private static final long serialVersionUID = 1L;
62
 
-
 
63
				{
63
				{
64
					put("F_50007", Arrays.asList("Upto 2 Mpx", "2 - 5 Mpx", "5 - 10 Mpx", "10 Mpx and above"));
64
					put("F_50007", Arrays.asList("Upto 2 Mpx", "2 - 5 Mpx", "5 - 10 Mpx", "10 Mpx and above"));
65
					put("F_50024", Arrays.asList("Upto 10 Mpx", "10 - 12 Mpx", "12 - 15 Mpx", "15 - 18 Mpx", "18 Mpx and above"));
65
					put("F_50024", Arrays.asList("Upto 10 Mpx", "10 - 12 Mpx", "12 - 15 Mpx", "15 - 18 Mpx", "18 Mpx and above"));
66
					put("F_50025", Arrays.asList("Upto 4x", "4 - 6x", "6 - 10x", "10 - 14x", "14 - 18x", "18x and above"));
66
					put("F_50025", Arrays.asList("Upto 4x", "4 - 6x", "6 - 10x", "10 - 14x", "14 - 18x", "18x and above"));
67
					put("F_50026", Arrays.asList("Below 2 in.", "2 to 2.9 in.", "3 to 3.9 in.", "4 in. and above"));
67
					put("F_50026", Arrays.asList("Below 2 in.", "2 to 2.9 in.", "3 to 3.9 in.", "4 in. and above"));
68
					put("F_50032", Arrays.asList("Below 3 in.", "3 to 3.9 in.", "4 to 4.9 in.", "5 in. and above"));
68
					put("F_50032", Arrays.asList("Below 3 in.", "3 to 3.9 in.", "4 to 4.9 in.", "5 in. and above"));
69
					put("F_50027", Arrays.asList("Upto 10 Mpx", "10 - 15 Mpx", "15 - 20 Mpx", "20 Mpx and above"));
69
					put("F_50027", Arrays.asList("Upto 10 Mpx", "10 - 15 Mpx", "15 - 20 Mpx", "20 Mpx and above"));
70
				}
70
				}
71
		});
71
		});
-
 
72
	public static final Map<String, List<String>> CATEGORY_FACET_MAP = Collections.unmodifiableMap(
-
 
73
			new HashMap<String, List<String>>(){
-
 
74
				/**
-
 
75
				 * 
-
 
76
				 */
-
 
77
				private static final long serialVersionUID = 1L;
-
 
78
				//Data Connectivity, Camera Resolution,Operating System,Screen Size
-
 
79
				List<String> mobileFacets = Arrays.asList("F_50006", "F_50007", "F_50031", "F_50032");
-
 
80
				//Camera Resolution,Operating System,Screen Size
-
 
81
				List<String> tabletFacets = Arrays.asList("F_50007", "F_50031", "F_50034");
-
 
82
				//Operating System, Processor, Storage, RAM, Screen Size
-
 
83
				List<String> laptopFacets = Arrays.asList("F_50013", "F_50014", "F_50015","F_50017", "F_50033");
-
 
84
				//Resolution, Optical Zoon, Display Size
-
 
85
				List<String> compactCameras = Arrays.asList("F_50024", "F_50025", "F_50026");
-
 
86
				//Display Size
-
 
87
				List<String> dslrCameras = Arrays.asList("F_50026");
-
 
88
				//Capacity, Class
-
 
89
				List<String> memoryCards = Arrays.asList("F_50018", "F_50019");
-
 
90
				//Capacity
-
 
91
				List<String> penDrives = Arrays.asList("F_50020");
-
 
92
				//Capacity, Type, Interface
-
 
93
				List<String> externalHardDisks = Arrays.asList("F_50021", "F_50022", "F_50023");
-
 
94
				{					
-
 
95
					put(SearchController.getCategoryLabel(10001l), mobileFacets);
-
 
96
					put(SearchController.getCategoryLabel(10002l), mobileFacets);
-
 
97
					put(SearchController.getCategoryLabel(10003l), mobileFacets);
-
 
98
					put(SearchController.getCategoryLabel(10004l), mobileFacets);
-
 
99
					put(SearchController.getCategoryLabel(10005l), mobileFacets);
-
 
100
					put(SearchController.getCategoryLabel(10009l), tabletFacets);
-
 
101
					put(SearchController.getCategoryLabel(10010l), tabletFacets);
-
 
102
					put(SearchController.getCategoryLabel(10013l), memoryCards);
-
 
103
					put(SearchController.getCategoryLabel(10017l), penDrives);
-
 
104
					put(SearchController.getCategoryLabel(10049l), laptopFacets);
-
 
105
					put(SearchController.getCategoryLabel(10050l), laptopFacets);
-
 
106
					put(SearchController.getCategoryLabel(10073l), externalHardDisks);
-
 
107
					put(SearchController.getCategoryLabel(11002l), compactCameras);
-
 
108
					put(SearchController.getCategoryLabel(11003l), dslrCameras);
-
 
109
				}
-
 
110
			});
72
	static {
111
	static {
73
		String solr_url = null;
112
		String solr_url = null;
74
		try {
113
		try {
75
			solr_url = ConfigClient.getClient().get("solr_url");
114
			solr_url = ConfigClient.getClient().get("solr_url");
76
		}catch(ConfigException cex){
115
		}catch(ConfigException cex){
Line 98... Line 137...
98
	
137
	
99
	long numberOfResults=0;
138
	long numberOfResults=0;
100
	
139
	
101
	String priceFacetName = "F_50002";
140
	String priceFacetName = "F_50002";
102
	
141
	
-
 
142
	List<String> filtrableFacets;
-
 
143
	
103
	/**
144
	/**
104
	 * 
145
	 * 
105
	 * @param query
146
	 * @param query
106
	 * @param facetDefinitionIDs
147
	 * @param facetDefinitionIDs
107
	 */
148
	 */
108
	public SolrSearchService(String query, String[] facetqueries, String[] facetDefinitionIDs, long start, long rows,  Double minPrice, Double maxPrice, long categoryId, String sortOrder, long sourceId) {
149
	public SolrSearchService(String query, String[] facetqueries, long start, long rows,  Double minPrice, Double maxPrice, String sortOrder, long sourceId) {
109
		this.query = query;
150
		this.query = query;
110
		
151
		
111
		List<String> rootFacetsQueried = new ArrayList<String>();
152
		List<String> facetsQueried = new ArrayList<String>();
112
		if(sourceId != -1){
153
		if(sourceId != -1){
113
			priceFacetName = priceFacetName + "_" + sourceId;
154
			priceFacetName = priceFacetName + "_" + sourceId;
114
		}
155
		}
115
		
156
		
-
 
157
		setFilterableFacets(facetqueries);
-
 
158
		
-
 
159
		
116
		this.xpath = XPathFactory.newInstance().newXPath();
160
		this.xpath = XPathFactory.newInstance().newXPath();
117
		
161
		
118
		query = query.trim().replaceAll("\\s+", " ");
162
		query = query.trim().replaceAll("\\s+", " ");
119
    	log.info("query=" + query);
163
    	log.info("query=" + query);
120
    	
164
    	
121
		String uri = SOLR_URL + "?wt=xml&q=" + query;
165
		String uri = SOLR_URL + "?wt=xml&q=" + query;
122
		
166
		
123
		uri += "&stats=on&stats.field=" + priceFacetName;
167
		uri += "&stats=on&stats.field=" + priceFacetName;
124
 
-
 
125
 
-
 
126
		if(categoryId != 10000){
-
 
127
			uri += "&fq=F_50010:\"" + CategoryManager.getCategoryManager().getCategoryLabel(categoryId) + "\"";
-
 
128
		}
-
 
129
		
168
		
130
		if(sortOrder != null){
169
		if(sortOrder != null){
131
			//replace the price facet name, so that it can pick price for the source.
170
			//replace the price facet name, so that it can pick price for the source.
132
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
171
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
133
			uri += "&sort=" + sortOrder;
172
			uri += "&sort=" + sortOrder;
Line 137... Line 176...
137
			//sorting will guarantee all similar facets together so that we can assume or between all similar items without fail.
176
			//sorting will guarantee all similar facets together so that we can assume or between all similar items without fail.
138
			Arrays.sort(facetqueries);
177
			Arrays.sort(facetqueries);
139
			String fq="";
178
			String fq="";
140
			for(int i=0; i<facetqueries.length; i++) {
179
			for(int i=0; i<facetqueries.length; i++) {
141
				String[] tokens = facetqueries[i].split(":");
180
				String[] tokens = facetqueries[i].split(":");
142
				if(rootFacetsQueried.contains(tokens[0])) {
181
				if(facetsQueried.contains(tokens[0])) {
143
					uri += " OR ";
182
					uri += " OR ";
144
					if(facetqueries[i].contains(" ")){
183
					if(facetqueries[i].contains(" ")){
145
						uri +=  "\"" + tokens[1] + "\"";
184
						uri +=  "\"" + tokens[1] + "\"";
146
					}else{
185
					}else{
147
						uri += facetqueries[i];
186
						uri += facetqueries[i];
148
					}
187
					}
149
					
188
					
150
				} else {
189
				} else {
151
					if(Arrays.asList(Utils.facetDefIDs).contains(tokens[0])) {
-
 
152
						fq = "{!tag=dt" + rootFacetsQueried.size() + "}";
190
					fq = "{!tag=dt" + facetsQueried.size() + "}";
153
						rootFacetsQueried.add(tokens[0]);
191
					facetsQueried.add(tokens[0]);
154
						if(facetqueries[i].contains(" ") && !(facetqueries[i].contains(" OR "))){
192
					if(facetqueries[i].contains(" ") && !(facetqueries[i].contains(" OR "))){
155
							fq +=  tokens[0] + ":\"" + tokens[1] + "\"";
193
						fq +=  tokens[0] + ":\"" + tokens[1] + "\"";
156
						}else{
194
					}else{
157
							fq += facetqueries[i] + "";
195
						fq += facetqueries[i] + "";
158
						}
-
 
159
					}
196
					}
160
					uri += "&fq=" + fq;
197
					uri += "&fq=" + fq;
161
				}
198
				}
162
			}
199
			}
163
		}
200
		}
Line 176... Line 213...
176
				maxString = maxPrice.toString();
213
				maxString = maxPrice.toString();
177
			}
214
			}
178
		}
215
		}
179
		uri += "&fq=" + priceFacetName + ":["+  minString + " " + maxString + "]";
216
		uri += "&fq=" + priceFacetName + ":["+  minString + " " + maxString + "]";
180
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
217
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
181
		if(facetDefinitionIDs != null){
-
 
182
			for(int i=0; i<facetDefinitionIDs.length; i++) {
218
		for(String facetDefinitionID : filtrableFacets) {
183
				if(rootFacetsQueried.contains(facetDefinitionIDs[i])){
219
				if(facetsQueried.contains(facetDefinitionID)){
184
					uri += "&facet.field={!ex=dt" + rootFacetsQueried.indexOf(facetDefinitionIDs[i])+ "}"+ facetDefinitionIDs[i]; 
220
					uri += "&facet.field={!ex=dt" + facetsQueried.indexOf(facetDefinitionID)+ "}"+ facetDefinitionID; 
185
				} else {
221
				} else {
186
					uri += "&facet.field=" + facetDefinitionIDs[i];
222
					uri += "&facet.field=" + facetDefinitionID;
187
				}
223
				}
188
			}
-
 
189
		}
224
		}
190
		log.info("uri=" + uri);
225
		log.info("uri=" + uri);
191
		
226
		
192
		this.inputSource = new InputSource(uri);
227
		this.inputSource = new InputSource(uri);
193
 
228
 
194
		this.facetMap = getFacetMap();
229
		this.facetMap = getFacetMap();
195
	}
230
	}
196
 
231
 
-
 
232
	@SuppressWarnings("unchecked")
-
 
233
	private void setFilterableFacets(String[] facetqueries) {
-
 
234
		List<String> queriedFacets = getAllMatches(this.query);
-
 
235
		if(facetqueries != null) {
-
 
236
			String facetString = Arrays.toString(facetqueries);
-
 
237
			List<String> filteredFacets = getAllMatches(facetString);
-
 
238
			if(filteredFacets.contains("F_50011")){
-
 
239
				for(String facetQuery : facetqueries) {
-
 
240
					if(facetQuery.contains("F_50011")){
-
 
241
						String facetVal = facetQuery.split(":")[1];
-
 
242
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
-
 
243
							this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
-
 
244
							return;
-
 
245
						} else {
-
 
246
							break;
-
 
247
						}
-
 
248
					}
-
 
249
				}
-
 
250
			}
-
 
251
			if(filteredFacets.contains("F_50010")){
-
 
252
				for(String facetQuery : facetqueries) {
-
 
253
					if(facetQuery.contains("F_50010")){
-
 
254
						String facetVal = facetQuery.split(":")[1];
-
 
255
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
-
 
256
							this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
-
 
257
							return;
-
 
258
						} else {
-
 
259
							break;
-
 
260
						}
-
 
261
					}
-
 
262
				}
-
 
263
			}
-
 
264
		}
-
 
265
		if(queriedFacets.contains("F_50011")) {
-
 
266
			String facetVal = this.query.split("F_50011:")[1];
-
 
267
			if (facetVal.contains(" OR ")) {
-
 
268
				this.filtrableFacets = Utils.rootfacetDefIDs;
-
 
269
				return;
-
 
270
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
-
 
271
				facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
-
 
272
				if(CATEGORY_FACET_MAP.containsKey(facetVal)){
-
 
273
					this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
-
 
274
					return;
-
 
275
				}
-
 
276
			} 
-
 
277
		}
-
 
278
		if(queriedFacets.contains("F_50010")){
-
 
279
			String facetVal = this.query.split("F_50010:")[1];
-
 
280
			if (facetVal.contains(" OR ")) {
-
 
281
				this.filtrableFacets = Utils.rootfacetDefIDs;
-
 
282
				return;
-
 
283
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
-
 
284
				facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
-
 
285
				if(CATEGORY_FACET_MAP.containsKey(facetVal)){
-
 
286
					this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
-
 
287
					return;
-
 
288
				}
-
 
289
			}
-
 
290
		}
-
 
291
		this.filtrableFacets = Utils.rootfacetDefIDs;
-
 
292
	}
-
 
293
 
-
 
294
	public List<String> getFilterableFacets() {
-
 
295
		return this.filtrableFacets; 
-
 
296
	}
-
 
297
 
197
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
298
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
198
		
299
		
199
		Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
300
		Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
200
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
301
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
201
		for(String facet : facetMap.keySet()){
302
		for(String facet : facetMap.keySet()){
Line 582... Line 683...
582
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
683
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
583
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
684
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
584
 
685
 
585
    	
686
    	
586
    	String[] fqrys = {};
687
    	String[] fqrys = {};
587
		SolrSearchService search = new SolrSearchService("nokia", fqrys, facetDefIDs, 0 , 20, null, null, 10000, null, -1);
688
		SolrSearchService search = new SolrSearchService("nokia", fqrys, 0 , 20, null, null, null, -1);
588
    	
689
    	
589
    	long[] entityIDs = search.getResultEntityIDs();
690
    	long[] entityIDs = search.getResultEntityIDs();
590
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
691
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
591
    	
692
    	
592
    	String[] entityNames = search.getResultEntityNames();
693
    	String[] entityNames = search.getResultEntityNames();