Subversion Repositories SmartDukaan

Rev

Rev 9124 | Rev 12106 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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