Subversion Repositories SmartDukaan

Rev

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