Subversion Repositories SmartDukaan

Rev

Rev 16291 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
317 ashish 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.services;
5
 
6
 
1698 chandransh 7
import in.shop2020.config.ConfigException;
6998 amit.gupta 8
import in.shop2020.serving.controllers.SearchController;
6866 amit.gupta 9
import in.shop2020.serving.utils.Utils;
1698 chandransh 10
import in.shop2020.thrift.clients.config.ConfigClient;
11
 
8275 amit.gupta 12
import java.io.UnsupportedEncodingException;
16291 amit.gupta 13
import java.net.URI;
14
import java.net.URL;
15
import java.net.URLDecoder;
8275 amit.gupta 16
import java.net.URLEncoder;
6866 amit.gupta 17
import java.util.ArrayList;
354 rajveer 18
import java.util.Arrays;
5729 amit.gupta 19
import java.util.Collections;
354 rajveer 20
import java.util.HashMap;
6866 amit.gupta 21
import java.util.HashSet;
5729 amit.gupta 22
import java.util.Iterator;
23
import java.util.LinkedHashMap;
790 vikas 24
import java.util.LinkedList;
25
import java.util.List;
5729 amit.gupta 26
import java.util.Map;
6866 amit.gupta 27
import java.util.Set;
354 rajveer 28
import java.util.TreeMap;
6866 amit.gupta 29
import java.util.regex.Matcher;
30
import java.util.regex.Pattern;
354 rajveer 31
 
317 ashish 32
import javax.xml.xpath.XPath;
33
import javax.xml.xpath.XPathConstants;
34
import javax.xml.xpath.XPathExpressionException;
35
import javax.xml.xpath.XPathFactory;
36
 
16291 amit.gupta 37
import org.apache.commons.codec.net.URLCodec;
832 rajveer 38
import org.apache.log4j.Logger;
317 ashish 39
import org.w3c.dom.Node;
40
import org.w3c.dom.NodeList;
41
import org.xml.sax.InputSource;
42
 
354 rajveer 43
 
317 ashish 44
/**
545 rajveer 45
 * @author rajveer
317 ashish 46
 *
47
 */
48
public class SolrSearchService {
49
	/**
50
	 * 
51
	 */
2147 chandransh 52
	private static Logger log = Logger.getLogger(Class.class);
6931 amit.gupta 53
	private HashMap<String, Double> dynamicPriceMap = null;
545 rajveer 54
 
55
	/**
56
	 * 
57
	 */
1698 chandransh 58
	public static final String SOLR_URL;
317 ashish 59
 
6866 amit.gupta 60
	private static final Pattern FACET_PATTERN = Pattern.compile("(?=(F_\\d{5}))");
61
 
5729 amit.gupta 62
	private static final Map<String, List<String>> SORTED_FACET_VALUE_MAP = Collections.unmodifiableMap(
63
			new HashMap<String, List<String>>(){
64
				/**
65
				 * 
66
				 */
67
				private static final long serialVersionUID = 1L;
68
				{
8997 amit.gupta 69
					put("F_50007", Arrays.asList("Upto 2 Mpx", "2 - 5 Mpx", "5 - 10 Mpx", "Above 10 Mpx"));
70
					put("F_50024", Arrays.asList("Upto 10 Mpx", "10 - 12 Mpx", "12 - 15 Mpx", "15 - 18 Mpx", "Above 18 Mpx"));
71
					put("F_50025", Arrays.asList("Upto 4x", "4 - 6x", "6 - 10x", "10 - 14x", "14 - 18x", "Above 18x"));
72
					put("F_50026", Arrays.asList("Below 2 in.", "2 to 2.9 in.", "3 to 3.9 in.", "Above 4 in."));
73
					put("F_50032", Arrays.asList("Below 3 in.", "3 to 3.9 in.", "4 to 4.9 in.", "Above 5 in."));
74
					put("F_50027", Arrays.asList("Upto 10 Mpx", "10 - 15 Mpx", "15 - 20 Mpx", "Above 20 Mpx"));
5729 amit.gupta 75
				}
76
		});
6998 amit.gupta 77
	public static final Map<String, List<String>> CATEGORY_FACET_MAP = Collections.unmodifiableMap(
78
			new HashMap<String, List<String>>(){
79
				/**
80
				 * 
81
				 */
82
				private static final long serialVersionUID = 1L;
83
				//Data Connectivity, Camera Resolution,Operating System,Screen Size
8977 amit.gupta 84
				List<String> mobileFacets = Arrays.asList("F_50038", "F_50006", "F_50007", "F_50031", "F_50032");
6998 amit.gupta 85
				//Camera Resolution,Operating System,Screen Size
8879 amit.gupta 86
				List<String> tabletFacets = Arrays.asList("F_50037","F_50036", "F_50035", "F_50034");
6998 amit.gupta 87
				//Operating System, Processor, Storage, RAM, Screen Size
88
				List<String> laptopFacets = Arrays.asList("F_50013", "F_50014", "F_50015","F_50017", "F_50033");
89
				//Resolution, Optical Zoon, Display Size
90
				List<String> compactCameras = Arrays.asList("F_50024", "F_50025", "F_50026");
91
				//Display Size
92
				List<String> dslrCameras = Arrays.asList("F_50026");
93
				//Capacity, Class
94
				List<String> memoryCards = Arrays.asList("F_50018", "F_50019");
95
				//Capacity
96
				List<String> penDrives = Arrays.asList("F_50020");
97
				//Capacity, Type, Interface
98
				List<String> externalHardDisks = Arrays.asList("F_50021", "F_50022", "F_50023");
99
				{					
100
					put(SearchController.getCategoryLabel(10001l), mobileFacets);
8564 amit.gupta 101
					put(SearchController.getCategoryLabel(10006l), mobileFacets);
6998 amit.gupta 102
					put(SearchController.getCategoryLabel(10009l), tabletFacets);
103
					put(SearchController.getCategoryLabel(10010l), tabletFacets);
104
					put(SearchController.getCategoryLabel(10013l), memoryCards);
105
					put(SearchController.getCategoryLabel(10017l), penDrives);
106
					put(SearchController.getCategoryLabel(10049l), laptopFacets);
107
					put(SearchController.getCategoryLabel(10050l), laptopFacets);
108
					put(SearchController.getCategoryLabel(10073l), externalHardDisks);
109
					put(SearchController.getCategoryLabel(11002l), compactCameras);
110
					put(SearchController.getCategoryLabel(11003l), dslrCameras);
111
				}
112
			});
1698 chandransh 113
	static {
114
		String solr_url = null;
115
		try {
116
			solr_url = ConfigClient.getClient().get("solr_url");
117
		}catch(ConfigException cex){
2949 chandransh 118
		    log.error("Unable to get the solr URL from the config server. Setting the default value.", cex);
1698 chandransh 119
			solr_url = "http://localhost:8983/solr/select/";
120
		}
121
		SOLR_URL = solr_url;
122
	}
123
 
317 ashish 124
	/**
125
	 * 
126
	 */
127
	private XPath xpath;
128
 
129
	/**
130
	 * 
131
	 */
132
	private InputSource inputSource;
133
 
12106 amit.gupta 134
	private boolean isPrivateDealUser = false;
135
 
5729 amit.gupta 136
	Map<String,Map<String,Integer>> facetMap;
354 rajveer 137
 
6866 amit.gupta 138
	private String 	query;
139
 
790 vikas 140
	List<String> resultMap;
12139 amit.gupta 141
	List<String> dealMap = new ArrayList<String>();
545 rajveer 142
 
12132 amit.gupta 143
	public List<String> getDealMap() {
144
		return dealMap;
145
	}
146
 
147
	public void setDealMap(List<String> dealMap) {
148
		this.dealMap = dealMap;
149
	}
150
 
545 rajveer 151
	long numberOfResults=0;
3561 rajveer 152
 
153
	String priceFacetName = "F_50002";
154
 
8882 amit.gupta 155
	List<String> filtrableFacets = new ArrayList<String>(Utils.rootfacetDefIDs);
6998 amit.gupta 156
 
317 ashish 157
	/**
158
	 * 
159
	 * @param query
160
	 * @param facetDefinitionIDs
161
	 */
12108 amit.gupta 162
	public SolrSearchService(String query, String[] facetqueries, long start, long rows,  Double minPrice, Double maxPrice, String sortOrder, long sourceId, boolean isPrivateDealUser) {
8328 amit.gupta 163
 
164
		this.query = query;
12108 amit.gupta 165
		this.isPrivateDealUser = isPrivateDealUser;
6998 amit.gupta 166
		List<String> facetsQueried = new ArrayList<String>();
3561 rajveer 167
		if(sourceId != -1){
168
			priceFacetName = priceFacetName + "_" + sourceId;
169
		}
170
 
6998 amit.gupta 171
		setFilterableFacets(facetqueries);
8884 amit.gupta 172
		this.filtrableFacets.remove(Utils.availabilityFacet);
173
		this.filtrableFacets.add(Utils.availabilityFacet);
12106 amit.gupta 174
		//Make it to the top
175
		if(isPrivateDealUser) {
176
			this.filtrableFacets.add(0, "F_50039");
177
		}
6998 amit.gupta 178
 
179
 
317 ashish 180
		this.xpath = XPathFactory.newInstance().newXPath();
545 rajveer 181
 
2606 rajveer 182
		query = query.trim().replaceAll("\\s+", " ");
545 rajveer 183
    	log.info("query=" + query);
184
 
8358 amit.gupta 185
		String uri = SOLR_URL + "?wt=xml&q=" + this.query;
317 ashish 186
 
3561 rajveer 187
		uri += "&stats=on&stats.field=" + priceFacetName;
354 rajveer 188
 
569 rajveer 189
		if(sortOrder != null){
3561 rajveer 190
			//replace the price facet name, so that it can pick price for the source.
191
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
569 rajveer 192
			uri += "&sort=" + sortOrder;
193
		}
545 rajveer 194
 
317 ashish 195
		if(facetqueries != null) {
12106 amit.gupta 196
			//sorting will guarantee all similar facets together so that we can assume "or" between all similar items without fail.
6866 amit.gupta 197
			Arrays.sort(facetqueries);
198
			String fq="";
317 ashish 199
			for(int i=0; i<facetqueries.length; i++) {
8275 amit.gupta 200
				String value = "";
6866 amit.gupta 201
				String[] tokens = facetqueries[i].split(":");
8275 amit.gupta 202
				try {
203
					value = URLEncoder.encode(tokens[1], "UTF-8");
204
				} catch (UnsupportedEncodingException e) {
205
					// TODO Auto-generated catch block
206
					e.printStackTrace();
207
				}
6998 amit.gupta 208
				if(facetsQueried.contains(tokens[0])) {
8280 amit.gupta 209
					uri += " OR  " + tokens[0] + ":\"" + value + "\"";
6866 amit.gupta 210
 
211
				} else {
6998 amit.gupta 212
					fq = "{!tag=dt" + facetsQueried.size() + "}";
213
					facetsQueried.add(tokens[0]);
8280 amit.gupta 214
					fq +=  tokens[0] + ":\"" + value + "\"";
6866 amit.gupta 215
					uri += "&fq=" + fq;
536 rajveer 216
				}
317 ashish 217
			}
218
		}
6931 amit.gupta 219
		String minString = "0";
220
		String maxString = "*";  
221
		if(minPrice != null || maxPrice != null){
222
			try {
16291 amit.gupta 223
				dynamicPriceMap = getPriceStatsMap(new InputSource(uri)); 
6931 amit.gupta 224
			} catch (Exception e){
225
				e.printStackTrace();
226
			}
227
			if(minPrice != null){
228
				minString = minPrice.toString();
229
			}
230
			if(maxPrice != null){
231
				maxString = maxPrice.toString();
232
			}
233
		}
9840 amit.gupta 234
		uri += "&fq=" + priceFacetName + ":["+  minString + "%20" + maxString + "]";
12132 amit.gupta 235
		uri += "&fl=ID" + (isPrivateDealUser ? ",F_50039" : "") + "&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
6998 amit.gupta 236
		for(String facetDefinitionID : filtrableFacets) {
237
				if(facetsQueried.contains(facetDefinitionID)){
238
					uri += "&facet.field={!ex=dt" + facetsQueried.indexOf(facetDefinitionID)+ "}"+ facetDefinitionID; 
6866 amit.gupta 239
				} else {
6998 amit.gupta 240
					uri += "&facet.field=" + facetDefinitionID;
6866 amit.gupta 241
				}
317 ashish 242
		}
3262 rajveer 243
		log.info("uri=" + uri);
317 ashish 244
 
16290 amit.gupta 245
		try{
16291 amit.gupta 246
			this.inputSource = new InputSource(toUri(uri));
16290 amit.gupta 247
		}catch(Exception e){
248
			log.info("Could not make it");
249
		}
517 rajveer 250
 
545 rajveer 251
		this.facetMap = getFacetMap();
354 rajveer 252
	}
253
 
6998 amit.gupta 254
	@SuppressWarnings("unchecked")
255
	private void setFilterableFacets(String[] facetqueries) {
256
		List<String> queriedFacets = getAllMatches(this.query);
9124 amit.gupta 257
		if(facetqueries != null && facetqueries.length!=0) {
6998 amit.gupta 258
			String facetString = Arrays.toString(facetqueries);
259
			List<String> filteredFacets = getAllMatches(facetString);
260
			if(filteredFacets.contains("F_50011")){
261
				for(String facetQuery : facetqueries) {
262
					if(facetQuery.contains("F_50011")){
263
						String facetVal = facetQuery.split(":")[1];
264
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
8882 amit.gupta 265
							this.filtrableFacets.addAll(CATEGORY_FACET_MAP.get(facetVal));
6998 amit.gupta 266
							return;
267
						} else {
268
							break;
269
						}
270
					}
271
				}
272
			}
273
			if(filteredFacets.contains("F_50010")){
274
				for(String facetQuery : facetqueries) {
275
					if(facetQuery.contains("F_50010")){
276
						String facetVal = facetQuery.split(":")[1];
277
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
278
							return;
279
						} else {
280
							break;
281
						}
282
					}
283
				}
284
			}
285
		}
286
		if(queriedFacets.contains("F_50011")) {
9124 amit.gupta 287
			String facetVal = this.query.split("F_50011:")[1].split("&")[0].replaceAll("[\"()]", "");
6998 amit.gupta 288
			if (facetVal.contains(" OR ")) {
289
				return;
290
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
9124 amit.gupta 291
				//facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
8882 amit.gupta 292
					this.filtrableFacets.addAll(CATEGORY_FACET_MAP.get(facetVal));
6998 amit.gupta 293
					return;
9124 amit.gupta 294
			} else {
295
				return;
296
			}
6998 amit.gupta 297
		}
298
		if(queriedFacets.contains("F_50010")){
9124 amit.gupta 299
			String facetVal = this.query.split("F_50010:")[1].split("&")[0].replaceAll("[\"()]", "");
6998 amit.gupta 300
			if (facetVal.contains(" OR ")) {
301
				return;
302
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
8882 amit.gupta 303
					this.filtrableFacets.addAll(CATEGORY_FACET_MAP.get(facetVal));
6998 amit.gupta 304
					return;
9124 amit.gupta 305
			} else {
306
				return;
6998 amit.gupta 307
			}
308
		}
309
	}
310
 
311
	public List<String> getFilterableFacets() {
312
		return this.filtrableFacets; 
313
	}
314
 
5729 amit.gupta 315
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
6866 amit.gupta 316
 
317
		Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
5729 amit.gupta 318
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
354 rajveer 319
		for(String facet : facetMap.keySet()){
6866 amit.gupta 320
			if(facetMap.get(facet).size() > 0 && !facetsInQuery.contains(facet)){
5729 amit.gupta 321
				Map<String,Integer> tempMap = new LinkedHashMap<String, Integer>();
545 rajveer 322
 
354 rajveer 323
				for(String facetValueName : facetMap.get(facet).keySet()){
6866 amit.gupta 324
					//if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
545 rajveer 325
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
6866 amit.gupta 326
					//}
354 rajveer 327
				}
545 rajveer 328
				if(!tempMap.isEmpty()){
329
					tempFacets.put(facet, tempMap);
354 rajveer 330
				}
545 rajveer 331
			}	
354 rajveer 332
		}
6866 amit.gupta 333
		/*if(tempFacets.containsKey("F_50010")){
550 rajveer 334
			tempFacets.remove("F_50011");
6866 amit.gupta 335
		}*/
354 rajveer 336
 
337
		return tempFacets;
338
	}
339
 
5729 amit.gupta 340
	public Map<String,Integer> getFacetDetails(String facetName){
2606 rajveer 341
		if(facetMap != null){
342
			return facetMap.get(facetName);
343
		}else{
344
			return null;
345
		}
354 rajveer 346
	}
347
 
5729 amit.gupta 348
	public Map<String,Map<String,Integer>> getFacetMap() {
349
		facetMap = new TreeMap<String,Map<String,Integer>>();
354 rajveer 350
 
351
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
545 rajveer 352
 
354 rajveer 353
		NodeList nodes = null;
354
		try {
355
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
356
		}
357
		catch (XPathExpressionException xpee) {
358
			return null;
359
		}
360
 
361
		if(nodes.getLength() == 0) {
362
			return null;
363
		}
364
 
365
		NodeList subNodes = null;
366
 
367
		for(int i=0; i<nodes.getLength(); i++) {
368
			Node node = nodes.item(i);
2946 chandransh 369
			String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
354 rajveer 370
			subNodes = node.getChildNodes();
5729 amit.gupta 371
			Map<String,Integer> facetValueCountMap = new LinkedHashMap<String,Integer>();
354 rajveer 372
			for(int j=0; j<subNodes.getLength(); j++) {
373
				Node subNode = subNodes.item(j);
374
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
375
			}
5729 amit.gupta 376
			if(SORTED_FACET_VALUE_MAP.containsKey(facetName)){
377
				List<String> orderedValues = SORTED_FACET_VALUE_MAP.get(facetName);
378
				Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
379
			    for (Iterator<String> it = orderedValues.iterator(); it.hasNext();) {
380
			    	String val = it.next();
381
			        if(facetValueCountMap.containsKey(val)) {
382
			        	sortedMap.put(val, facetValueCountMap.get(val));
383
			        }
384
			    }
385
			    facetMap.put(facetName, sortedMap);
386
			} else {
387
				facetMap.put(facetName, facetValueCountMap);
354 rajveer 388
			}
5729 amit.gupta 389
		}
545 rajveer 390
		this.numberOfResults  = this.getTotalResults();
517 rajveer 391
 
354 rajveer 392
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
393
		return facetMap;
5729 amit.gupta 394
	}
354 rajveer 395
 
790 vikas 396
	public List<String> getResultMap() {
397
		resultMap = new LinkedList<String>();
354 rajveer 398
 
399
		String resultDocsPath = "/response/result/doc";
400
 
401
 
402
		NodeList nodes = null;
403
		try {
404
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
405
		}
406
		catch (XPathExpressionException xpee) {
16289 amit.gupta 407
			xpee.printStackTrace();
354 rajveer 408
			return null;
409
		}
410
 
411
		if(nodes.getLength() == 0) {
412
			return null;
413
		}
414
 
415
		for(int i=0; i<nodes.getLength(); i++) {
416
			Node node = nodes.item(i);
12132 amit.gupta 417
			String docID = node.getLastChild().getTextContent();
418
			if(isPrivateDealUser && node.getChildNodes().getLength()==2) {
419
				dealMap.add(docID);
420
			}
790 vikas 421
			resultMap.add(docID);	
354 rajveer 422
 		}
423
		return resultMap;
424
	}
425
 
426
	public HashMap<String, Double> getPriceStatsMap() {
6931 amit.gupta 427
		return this.getPriceStatsMap(this.inputSource);
428
	}
429
 
430
	public HashMap<String, Double> getPriceStatsMap(InputSource inputSource) {
354 rajveer 431
		HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
432
 
3561 rajveer 433
		String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = '" + priceFacetName + "']";
354 rajveer 434
 
435
 
436
		NodeList nodes = null;
437
		try {
6931 amit.gupta 438
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, inputSource, XPathConstants.NODESET);
354 rajveer 439
		}
440
		catch (XPathExpressionException xpee) {
441
			return null;
442
		}
443
 
444
		if(nodes.getLength() == 0) {
445
			return null;
446
		}
447
 
448
		NodeList subNodes = nodes.item(0).getChildNodes();
449
 
450
		for(int i=0; i<subNodes.getLength(); i++) {
451
			Node node = subNodes.item(i);
452
 
453
			String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
454
			String value = node.getTextContent();
455
			priceStatsMap.put(parameter, Double.parseDouble(value));	
456
 		}
457
		return priceStatsMap;
458
	}
459
 
460
	public HashMap<String,Integer> getRangeQueryResultMap() {
461
		HashMap<String, Integer> rangeQueryResultMap = new HashMap<String,Integer>();
462
 
463
		String resultDocsPath = "/response/lst[@name = 'facet_counts']/lst[@name = 'facet_queries']/int";
464
 
465
 
466
		NodeList nodes = null;
467
		try {
468
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
469
		}
470
		catch (XPathExpressionException xpee) {
471
			return null;
472
		}
473
 
474
		if(nodes.getLength() == 0) {
475
			return null;
476
		}
477
 
478
 
479
		for(int i=0; i<nodes.getLength(); i++) {
480
			Node node = nodes.item(i);
481
 
482
			String query = node.getAttributes().getNamedItem("name").getNodeValue();
483
			String docCount = node.getTextContent();
484
 
485
			rangeQueryResultMap.put(query,Integer.parseInt(docCount));	
486
 		}
487
		return rangeQueryResultMap;
488
 
489
	}
490
 
545 rajveer 491
	/**
492
	 * 
493
	 */
494
	public long getTotalResults(){
495
		String resultDocsPath = "/response/result";
496
		NodeList nodes = null;
497
		try {
498
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
499
		}
500
		catch (XPathExpressionException xpee) {
501
			return 0;
502
		}
503
 
504
		Node node = nodes.item(0);
505
 
506
		return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
507
 
508
	}
354 rajveer 509
		/**
317 ashish 510
	 * 
511
	 * @return
512
	 */
513
	public long[] getResultEntityIDs() {
514
		String expression = "/response/result/doc/long";
515
 
516
		NodeList nodes = null;
517
		try {
518
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
519
					XPathConstants.NODESET);
520
		} 
521
		catch(XPathExpressionException xpee) {
522
			return null;
523
		}
524
 
525
		if(nodes.getLength() == 0) {
526
			return null;
527
		}
528
 
529
		long[] values = new long[nodes.getLength()];
530
		for(int i=0; i<nodes.getLength(); i++) {
531
			Node node = nodes.item(i);
532
			String value = node.getTextContent();
533
			values[i] = Long.parseLong(value);
534
 		}
535
 
536
		return values;
537
	}
538
 
539
	/**
540
	 * 
541
	 * @return
542
	 */
543
	public String[] getResultCategoryNames() {
544
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
545
		expression += "lst[@name = 'Category']/int/@name";
546
 
547
		NodeList nodes = null;
548
		try {
549
			nodes = (NodeList) this.xpath.evaluate(expression, 
550
				this.inputSource, XPathConstants.NODESET);
551
		}
552
		catch (XPathExpressionException xpee) {
553
			return null;
554
		}
555
 
556
		if(nodes.getLength() == 0) {
557
			return null;
558
		}
559
 
560
		String[] values = new String[nodes.getLength()];
561
		for(int i=0; i<nodes.getLength(); i++) {
562
			Node node = nodes.item(i);
563
			values[i] = node.getTextContent();
564
 		}
565
 
566
		return values;
567
	}
568
 
569
	/**
570
	 * 
571
	 * @return
572
	 */
573
	public int[] getResultCategoryCounts() {
574
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
575
		expression += "lst[@name = 'Category']/int";
576
 
577
		NodeList nodes = null;
578
		try {
579
			nodes = (NodeList) this.xpath.evaluate(expression, 
580
				this.inputSource, XPathConstants.NODESET);
581
		}
582
		catch (XPathExpressionException xpee) {
583
			return null;
584
		}
585
 
586
		if(nodes.getLength() == 0) {
587
			return null;
588
		}
589
 
590
		int[] values = new int[nodes.getLength()];
591
		for(int i=0; i<nodes.getLength(); i++) {
592
			Node node = nodes.item(i);
593
			values[i] = Integer.parseInt(node.getTextContent());
594
 		}
595
 
596
		return values;
597
	}
598
 
599
	/**
600
	 * 
601
	 * @return
602
	 */
603
	public String[]  getResultEntityNames() {
604
		String expression = "/response/result/doc/str";
605
 
606
		NodeList nodes = null;
607
		try {
608
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
609
					XPathConstants.NODESET);
610
		} 
611
		catch(XPathExpressionException xpee) {
612
			return null;
613
		}
614
 
615
		if(nodes.getLength() == 0) {
616
			return null;
617
		}
618
 
619
		String[] values = new String[nodes.getLength()];
620
		for(int i=0; i<nodes.getLength(); i++) {
621
			Node node = nodes.item(i);
622
			String value = node.getTextContent();
623
			values[i] = value;
624
 		}
625
 
626
		return values;
627
	}
628
 
629
	/**
630
	 * 
631
	 * @param facetDefinitionID
632
	 * @return
633
	 */
354 rajveer 634
	public String[] getFacetValues(String facetDefinitionID) {
317 ashish 635
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 636
		expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
317 ashish 637
 
638
		NodeList nodes = null;
639
		try {
640
			nodes = (NodeList) this.xpath.evaluate(expression, 
641
				this.inputSource, XPathConstants.NODESET);
642
		}
643
		catch (XPathExpressionException xpee) {
644
			return null;
645
		}
646
 
647
		if(nodes.getLength() == 0) {
648
			return null;
649
		}
650
 
651
		String[] values = new String[nodes.getLength()];
652
		for(int i=0; i<nodes.getLength(); i++) {
653
			Node node = nodes.item(i);
654
			values[i] = node.getTextContent();
545 rajveer 655
		}
317 ashish 656
 
657
		return values;
658
	}
659
 
660
	/**
661
	 * 
662
	 * @param facetDefinitionID
663
	 * @return
664
	 */
354 rajveer 665
	public String[] getFacetCounts(String facetDefinitionID) {
317 ashish 666
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 667
		expression += "lst[@name = '" + facetDefinitionID + "']/int";
317 ashish 668
 
669
		NodeList nodes = null;
670
		try {
671
			nodes = (NodeList) this.xpath.evaluate(expression, 
672
				this.inputSource, XPathConstants.NODESET);
673
		}
674
		catch (XPathExpressionException xpee) {
675
			return null;
676
		}
677
 
678
		if(nodes.getLength() == 0) {
679
			return null;
680
		}
681
 
682
		String[] values = new String[nodes.getLength()];
683
		for(int i=0; i<nodes.getLength(); i++) {
684
			Node node = nodes.item(i);
685
			values[i] = node.getTextContent();
686
 		}
687
 
688
		return values;
689
	}
545 rajveer 690
 
691
	public static void main(String[] args){
692
		/*
693
    	// Hard coded for now
694
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
695
 
696
    	// Hard-coded for now
697
    	String[] facetLabels = new String[] {
698
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
699
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
700
	    	"Talk time"
701
    	};
702
 
703
		 */
704
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
2147 chandransh 705
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
545 rajveer 706
 
707
 
16288 amit.gupta 708
    	String[] fqrys = {"F_50001:Nokia", "F_50001:Amzer"};
12108 amit.gupta 709
		SolrSearchService search = new SolrSearchService("nokia", fqrys, 0 , 20, null, null, null, -1, true);
545 rajveer 710
 
711
    	long[] entityIDs = search.getResultEntityIDs();
712
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
713
 
714
    	String[] entityNames = search.getResultEntityNames();
715
    	log.info("entityNames=" + Arrays.toString(entityNames));
716
    	search.getFacetMap();
717
 
16288 amit.gupta 718
    	System.out.println(search.getResultMap());
719
    	System.out.println(search.getRangeQueryResultMap());
720
    	System.out.println(search.getTotalResults());
545 rajveer 721
       	for (int i=0; i<facetDefIDs.length; i++) {
722
       		search.getFacetCounts(facetDefIDs[i]);
723
       		search.getFacetValues(facetDefIDs[i]);
724
       	}
725
 
726
	}
6866 amit.gupta 727
 
728
 
729
    public static List<String> getAllMatches(String text) {
730
        List<String> matches = new ArrayList<String>();
731
        Matcher m = FACET_PATTERN.matcher(text);
732
        while(m.find()) {
733
            matches.add(m.group(1));
734
        }
735
        return matches;
736
    }
6931 amit.gupta 737
 
738
    public Map<String, Double> getDynamicPriceMap() {
739
    	return this.dynamicPriceMap;
740
    }
12106 amit.gupta 741
 
742
	public SolrSearchService setPrivateDealUser(boolean isPrivateDealUser) {
743
		this.isPrivateDealUser = isPrivateDealUser;
744
		return this;
745
	}
746
 
747
	public boolean isPrivateDealUser() {
748
		return isPrivateDealUser;
749
	}
16291 amit.gupta 750
 
751
	public String toUri(String string){
752
	    try {
753
	        String decodedURL = URLDecoder.decode(string, "UTF-8");
754
	        URL url = new URL(decodedURL);
755
	        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 
756
	        return uri.toASCIIString(); 
757
	    } catch (Exception ex) {
758
	        ex.printStackTrace();
759
	        return null;
760
	    }
761
	}
6931 amit.gupta 762
 
317 ashish 763
}