Subversion Repositories SmartDukaan

Rev

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