Subversion Repositories SmartDukaan

Rev

Rev 11315 | Rev 11730 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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