Subversion Repositories SmartDukaan

Rev

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