Subversion Repositories SmartDukaan

Rev

Rev 11728 | Rev 12143 | 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
11730 amit.gupta 158
		if(facetqueries!=null) {
159
			for(int i=0;i<facetqueries.length;i++){
160
				try {
161
					facetqueries[i] = URLDecoder.decode(facetqueries[i], "UTF-8");
162
				} catch (UnsupportedEncodingException e) {
163
					facetqueries = null;
164
					e.printStackTrace();
165
				}
11728 amit.gupta 166
			}
167
		}
9406 amit.gupta 168
		this.filtrableFacets.remove(Utils.availabilityFacet);
169
		this.filtrableFacets.add(Utils.availabilityFacet);
9103 anupam.sin 170
 
171
 
172
		this.xpath = XPathFactory.newInstance().newXPath();
173
 
174
		query = query.trim().replaceAll("\\s+", " ");
175
    	log.info("query=" + query);
176
 
177
		String uri = SOLR_URL + "?wt=xml&q=" + this.query;
178
 
179
		uri += "&stats=on&stats.field=" + priceFacetName;
180
 
181
		if(sortOrder != null){
182
			//replace the price facet name, so that it can pick price for the source.
183
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
184
			uri += "&sort=" + sortOrder;
185
		}
186
 
187
		if(facetqueries != null) {
188
			//sorting will guarantee all similar facets together so that we can assume or between all similar items without fail.
189
			Arrays.sort(facetqueries);
190
			String fq="";
191
			for(int i=0; i<facetqueries.length; i++) {
192
				String value = "";
193
				String[] tokens = facetqueries[i].split(":");
194
				try {
195
					value = URLEncoder.encode(tokens[1], "UTF-8");
196
				} catch (UnsupportedEncodingException e) {
197
					// TODO Auto-generated catch block
198
					e.printStackTrace();
199
				}
200
				if(facetsQueried.contains(tokens[0])) {
201
					uri += " OR  " + tokens[0] + ":\"" + value + "\"";
202
 
203
				} else {
204
					fq = "{!tag=dt" + facetsQueried.size() + "}";
205
					facetsQueried.add(tokens[0]);
206
					fq +=  tokens[0] + ":\"" + value + "\"";
207
					uri += "&fq=" + fq;
208
				}
209
			}
210
		}
9887 amit.gupta 211
		uri = uri.replaceAll(" ", "%20");
9103 anupam.sin 212
		String minString = "0";
213
		String maxString = "*";  
214
		if(minPrice != null || maxPrice != null){
215
			try {
9886 amit.gupta 216
				log.info("Uri:" + uri);
9103 anupam.sin 217
				dynamicPriceMap = getPriceStatsMap(new InputSource(uri)); 
218
			} catch (Exception e){
219
				e.printStackTrace();
220
			}
221
			if(minPrice != null){
222
				minString = minPrice.toString();
223
			}
224
			if(maxPrice != null){
225
				maxString = maxPrice.toString();
226
			}
227
		}
9269 amit.gupta 228
		uri += "&fq=" + priceFacetName + ":["+  minString + "%20" + maxString + "]";
9103 anupam.sin 229
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
230
		for(String facetDefinitionID : filtrableFacets) {
231
				if(facetsQueried.contains(facetDefinitionID)){
232
					uri += "&facet.field={!ex=dt" + facetsQueried.indexOf(facetDefinitionID)+ "}"+ facetDefinitionID; 
233
				} else {
234
					uri += "&facet.field=" + facetDefinitionID;
235
				}
236
		}
237
		log.info("uri=" + uri);
238
 
239
		this.inputSource = new InputSource(uri);
240
 
241
		this.facetMap = getFacetMap();
242
	}
243
 
244
	@SuppressWarnings("unchecked")
245
	private void setFilterableFacets(String[] facetqueries) {
246
		List<String> queriedFacets = getAllMatches(this.query);
9406 amit.gupta 247
		if(facetqueries != null && facetqueries.length!=0) {
9103 anupam.sin 248
			String facetString = Arrays.toString(facetqueries);
249
			List<String> filteredFacets = getAllMatches(facetString);
250
			if(filteredFacets.contains("F_50011")){
251
				for(String facetQuery : facetqueries) {
252
					if(facetQuery.contains("F_50011")){
253
						String facetVal = facetQuery.split(":")[1];
254
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
9406 amit.gupta 255
							this.filtrableFacets.addAll(CATEGORY_FACET_MAP.get(facetVal));
9103 anupam.sin 256
							return;
257
						} else {
258
							break;
259
						}
260
					}
261
				}
262
			}
263
			if(filteredFacets.contains("F_50010")){
264
				for(String facetQuery : facetqueries) {
265
					if(facetQuery.contains("F_50010")){
266
						String facetVal = facetQuery.split(":")[1];
267
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
268
							return;
269
						} else {
270
							break;
271
						}
272
					}
273
				}
274
			}
275
		}
276
		if(queriedFacets.contains("F_50011")) {
9406 amit.gupta 277
			String facetVal = this.query.split("F_50011:")[1].split("&")[0].replaceAll("[\"()]", "");
9103 anupam.sin 278
			if (facetVal.contains(" OR ")) {
279
				return;
280
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
9406 amit.gupta 281
				//facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
282
					this.filtrableFacets.addAll(CATEGORY_FACET_MAP.get(facetVal));
9103 anupam.sin 283
					return;
9406 amit.gupta 284
			} else {
285
				return;
286
			}
9103 anupam.sin 287
		}
288
		if(queriedFacets.contains("F_50010")){
9406 amit.gupta 289
			String facetVal = this.query.split("F_50010:")[1].split("&")[0].replaceAll("[\"()]", "");
9103 anupam.sin 290
			if (facetVal.contains(" OR ")) {
291
				return;
292
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
9406 amit.gupta 293
					this.filtrableFacets.addAll(CATEGORY_FACET_MAP.get(facetVal));
9103 anupam.sin 294
					return;
9406 amit.gupta 295
			} else {
296
				return;
9103 anupam.sin 297
			}
298
		}
299
	}
300
 
301
	public List<String> getFilterableFacets() {
302
		return this.filtrableFacets; 
303
	}
304
 
305
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
306
 
307
		Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
308
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
309
		for(String facet : facetMap.keySet()){
310
			if(facetMap.get(facet).size() > 0 && !facetsInQuery.contains(facet)){
311
				Map<String,Integer> tempMap = new LinkedHashMap<String, Integer>();
312
 
313
				for(String facetValueName : facetMap.get(facet).keySet()){
314
					//if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
315
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
316
					//}
317
				}
318
				if(!tempMap.isEmpty()){
319
					tempFacets.put(facet, tempMap);
320
				}
321
			}	
322
		}
323
		/*if(tempFacets.containsKey("F_50010")){
324
			tempFacets.remove("F_50011");
325
		}*/
326
 
327
		return tempFacets;
328
	}
329
 
330
	public Map<String,Integer> getFacetDetails(String facetName){
331
		if(facetMap != null){
332
			return facetMap.get(facetName);
333
		}else{
334
			return null;
335
		}
336
	}
337
 
338
	public Map<String,Map<String,Integer>> getFacetMap() {
339
		facetMap = new TreeMap<String,Map<String,Integer>>();
340
 
341
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
342
 
343
		NodeList nodes = null;
344
		try {
345
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
346
		}
347
		catch (XPathExpressionException xpee) {
348
			return null;
349
		}
350
 
351
		if(nodes.getLength() == 0) {
352
			return null;
353
		}
354
 
355
		NodeList subNodes = null;
356
 
357
		for(int i=0; i<nodes.getLength(); i++) {
358
			Node node = nodes.item(i);
359
			String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
360
			subNodes = node.getChildNodes();
361
			Map<String,Integer> facetValueCountMap = new LinkedHashMap<String,Integer>();
362
			for(int j=0; j<subNodes.getLength(); j++) {
363
				Node subNode = subNodes.item(j);
364
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
365
			}
366
			if(SORTED_FACET_VALUE_MAP.containsKey(facetName)){
367
				List<String> orderedValues = SORTED_FACET_VALUE_MAP.get(facetName);
368
				Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
369
			    for (Iterator<String> it = orderedValues.iterator(); it.hasNext();) {
370
			    	String val = it.next();
371
			        if(facetValueCountMap.containsKey(val)) {
372
			        	sortedMap.put(val, facetValueCountMap.get(val));
373
			        }
374
			    }
375
			    facetMap.put(facetName, sortedMap);
376
			} else {
377
				facetMap.put(facetName, facetValueCountMap);
378
			}
379
		}
380
		this.numberOfResults  = this.getTotalResults();
11306 amit.gupta 381
		log.info("FacetMap before:" + facetMap);
9103 anupam.sin 382
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
11306 amit.gupta 383
		log.info("FacetMap after:" + facetMap);
9103 anupam.sin 384
		return facetMap;
385
	}
386
 
387
	public List<String> getResultMap() {
388
		resultMap = new LinkedList<String>();
389
 
390
		String resultDocsPath = "/response/result/doc";
391
 
392
 
393
		NodeList nodes = null;
394
		try {
395
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
396
		}
397
		catch (XPathExpressionException xpee) {
11315 amit.gupta 398
			return resultMap;
9103 anupam.sin 399
		}
400
 
401
		if(nodes.getLength() == 0) {
11315 amit.gupta 402
			return resultMap;
9103 anupam.sin 403
		}
404
 
405
		for(int i=0; i<nodes.getLength(); i++) {
406
			Node node = nodes.item(i);
407
			String docID = node.getFirstChild().getTextContent();
408
			resultMap.add(docID);	
409
 		}
410
		return resultMap;
411
	}
412
 
413
	public HashMap<String, Double> getPriceStatsMap() {
414
		return this.getPriceStatsMap(this.inputSource);
415
	}
416
 
417
	public HashMap<String, Double> getPriceStatsMap(InputSource inputSource) {
418
		HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
419
 
420
		String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = '" + priceFacetName + "']";
421
 
422
 
423
		NodeList nodes = null;
424
		try {
425
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, inputSource, XPathConstants.NODESET);
426
		}
427
		catch (XPathExpressionException xpee) {
428
			return null;
429
		}
430
 
431
		if(nodes.getLength() == 0) {
432
			return null;
433
		}
434
 
435
		NodeList subNodes = nodes.item(0).getChildNodes();
436
 
437
		for(int i=0; i<subNodes.getLength(); i++) {
438
			Node node = subNodes.item(i);
439
 
440
			String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
441
			String value = node.getTextContent();
442
			priceStatsMap.put(parameter, Double.parseDouble(value));	
443
 		}
444
		return priceStatsMap;
445
	}
446
 
447
	public HashMap<String,Integer> getRangeQueryResultMap() {
448
		HashMap<String, Integer> rangeQueryResultMap = new HashMap<String,Integer>();
449
 
450
		String resultDocsPath = "/response/lst[@name = 'facet_counts']/lst[@name = 'facet_queries']/int";
451
 
452
 
453
		NodeList nodes = null;
454
		try {
455
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
456
		}
457
		catch (XPathExpressionException xpee) {
458
			return null;
459
		}
460
 
461
		if(nodes.getLength() == 0) {
462
			return null;
463
		}
464
 
465
 
466
		for(int i=0; i<nodes.getLength(); i++) {
467
			Node node = nodes.item(i);
468
 
469
			String query = node.getAttributes().getNamedItem("name").getNodeValue();
470
			String docCount = node.getTextContent();
471
 
472
			rangeQueryResultMap.put(query,Integer.parseInt(docCount));	
473
 		}
474
		return rangeQueryResultMap;
475
 
476
	}
477
 
478
	/**
479
	 * 
480
	 */
481
	public long getTotalResults(){
482
		String resultDocsPath = "/response/result";
483
		NodeList nodes = null;
484
		try {
485
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
486
		}
487
		catch (XPathExpressionException xpee) {
488
			return 0;
489
		}
490
 
491
		Node node = nodes.item(0);
492
 
493
		return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
494
 
495
	}
496
		/**
497
	 * 
498
	 * @return
499
	 */
500
	public long[] getResultEntityIDs() {
501
		String expression = "/response/result/doc/long";
502
 
503
		NodeList nodes = null;
504
		try {
505
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
506
					XPathConstants.NODESET);
507
		} 
508
		catch(XPathExpressionException xpee) {
509
			return null;
510
		}
511
 
512
		if(nodes.getLength() == 0) {
513
			return null;
514
		}
515
 
516
		long[] values = new long[nodes.getLength()];
517
		for(int i=0; i<nodes.getLength(); i++) {
518
			Node node = nodes.item(i);
519
			String value = node.getTextContent();
520
			values[i] = Long.parseLong(value);
521
 		}
522
 
523
		return values;
524
	}
525
 
526
	/**
527
	 * 
528
	 * @return
529
	 */
530
	public String[] getResultCategoryNames() {
531
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
532
		expression += "lst[@name = 'Category']/int/@name";
533
 
534
		NodeList nodes = null;
535
		try {
536
			nodes = (NodeList) this.xpath.evaluate(expression, 
537
				this.inputSource, XPathConstants.NODESET);
538
		}
539
		catch (XPathExpressionException xpee) {
540
			return null;
541
		}
542
 
543
		if(nodes.getLength() == 0) {
544
			return null;
545
		}
546
 
547
		String[] values = new String[nodes.getLength()];
548
		for(int i=0; i<nodes.getLength(); i++) {
549
			Node node = nodes.item(i);
550
			values[i] = node.getTextContent();
551
 		}
552
 
553
		return values;
554
	}
555
 
556
	/**
557
	 * 
558
	 * @return
559
	 */
560
	public int[] getResultCategoryCounts() {
561
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
562
		expression += "lst[@name = 'Category']/int";
563
 
564
		NodeList nodes = null;
565
		try {
566
			nodes = (NodeList) this.xpath.evaluate(expression, 
567
				this.inputSource, XPathConstants.NODESET);
568
		}
569
		catch (XPathExpressionException xpee) {
570
			return null;
571
		}
572
 
573
		if(nodes.getLength() == 0) {
574
			return null;
575
		}
576
 
577
		int[] values = new int[nodes.getLength()];
578
		for(int i=0; i<nodes.getLength(); i++) {
579
			Node node = nodes.item(i);
580
			values[i] = Integer.parseInt(node.getTextContent());
581
 		}
582
 
583
		return values;
584
	}
585
 
586
	/**
587
	 * 
588
	 * @return
589
	 */
590
	public String[]  getResultEntityNames() {
591
		String expression = "/response/result/doc/str";
592
 
593
		NodeList nodes = null;
594
		try {
595
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
596
					XPathConstants.NODESET);
597
		} 
598
		catch(XPathExpressionException xpee) {
599
			return null;
600
		}
601
 
602
		if(nodes.getLength() == 0) {
603
			return null;
604
		}
605
 
606
		String[] values = new String[nodes.getLength()];
607
		for(int i=0; i<nodes.getLength(); i++) {
608
			Node node = nodes.item(i);
609
			String value = node.getTextContent();
610
			values[i] = value;
611
 		}
612
 
613
		return values;
614
	}
615
 
616
	/**
617
	 * 
618
	 * @param facetDefinitionID
619
	 * @return
620
	 */
621
	public String[] getFacetValues(String facetDefinitionID) {
622
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
623
		expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
624
 
625
		NodeList nodes = null;
626
		try {
627
			nodes = (NodeList) this.xpath.evaluate(expression, 
628
				this.inputSource, XPathConstants.NODESET);
629
		}
630
		catch (XPathExpressionException xpee) {
631
			return null;
632
		}
633
 
634
		if(nodes.getLength() == 0) {
635
			return null;
636
		}
637
 
638
		String[] values = new String[nodes.getLength()];
639
		for(int i=0; i<nodes.getLength(); i++) {
640
			Node node = nodes.item(i);
641
			values[i] = node.getTextContent();
642
		}
643
 
644
		return values;
645
	}
646
 
647
	/**
648
	 * 
649
	 * @param facetDefinitionID
650
	 * @return
651
	 */
652
	public String[] getFacetCounts(String facetDefinitionID) {
653
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
654
		expression += "lst[@name = '" + facetDefinitionID + "']/int";
655
 
656
		NodeList nodes = null;
657
		try {
658
			nodes = (NodeList) this.xpath.evaluate(expression, 
659
				this.inputSource, XPathConstants.NODESET);
660
		}
661
		catch (XPathExpressionException xpee) {
662
			return null;
663
		}
664
 
665
		if(nodes.getLength() == 0) {
666
			return null;
667
		}
668
 
669
		String[] values = new String[nodes.getLength()];
670
		for(int i=0; i<nodes.getLength(); i++) {
671
			Node node = nodes.item(i);
672
			values[i] = node.getTextContent();
673
 		}
674
 
675
		return values;
676
	}
677
 
678
	public static void main(String[] args){
679
		/*
680
    	// Hard coded for now
681
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
682
 
683
    	// Hard-coded for now
684
    	String[] facetLabels = new String[] {
685
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
686
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
687
	    	"Talk time"
688
    	};
689
 
690
		 */
691
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
692
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
693
 
694
 
695
    	String[] fqrys = {};
696
		SolrSearchService search = new SolrSearchService("nokia", fqrys, 0 , 20, null, null, null, -1);
697
 
698
    	long[] entityIDs = search.getResultEntityIDs();
699
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
700
 
701
    	String[] entityNames = search.getResultEntityNames();
702
    	log.info("entityNames=" + Arrays.toString(entityNames));
703
    	search.getFacetMap();
704
 
705
    	search.getResultMap();
706
    	search.getRangeQueryResultMap();
707
    	search.getPriceStatsMap(new InputSource());
708
    	search.getTotalResults();
709
       	for (int i=0; i<facetDefIDs.length; i++) {
710
       		search.getFacetCounts(facetDefIDs[i]);
711
       		search.getFacetValues(facetDefIDs[i]);
712
       	}
713
 
714
	}
715
 
716
 
717
    public static List<String> getAllMatches(String text) {
718
        List<String> matches = new ArrayList<String>();
719
        Matcher m = FACET_PATTERN.matcher(text);
720
        while(m.find()) {
721
            matches.add(m.group(1));
722
        }
723
        return matches;
724
    }
725
 
726
    public Map<String, Double> getDynamicPriceMap() {
727
    	return this.dynamicPriceMap;
728
    }
729
 
730
}