Subversion Repositories SmartDukaan

Rev

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