Subversion Repositories SmartDukaan

Rev

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