Subversion Repositories SmartDukaan

Rev

Rev 7132 | Rev 8280 | 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
7132 amit.gupta 83
				List<String> tabletFacets = Arrays.asList("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);
98
					put(SearchController.getCategoryLabel(10002l), mobileFacets);
99
					put(SearchController.getCategoryLabel(10003l), mobileFacets);
100
					put(SearchController.getCategoryLabel(10004l), mobileFacets);
101
					put(SearchController.getCategoryLabel(10005l), mobileFacets);
102
					put(SearchController.getCategoryLabel(10009l), tabletFacets);
103
					put(SearchController.getCategoryLabel(10010l), tabletFacets);
104
					put(SearchController.getCategoryLabel(10013l), memoryCards);
105
					put(SearchController.getCategoryLabel(10017l), penDrives);
106
					put(SearchController.getCategoryLabel(10049l), laptopFacets);
107
					put(SearchController.getCategoryLabel(10050l), laptopFacets);
108
					put(SearchController.getCategoryLabel(10073l), externalHardDisks);
109
					put(SearchController.getCategoryLabel(11002l), compactCameras);
110
					put(SearchController.getCategoryLabel(11003l), dslrCameras);
111
				}
112
			});
1698 chandransh 113
	static {
114
		String solr_url = null;
115
		try {
116
			solr_url = ConfigClient.getClient().get("solr_url");
117
		}catch(ConfigException cex){
2949 chandransh 118
		    log.error("Unable to get the solr URL from the config server. Setting the default value.", cex);
1698 chandransh 119
			solr_url = "http://localhost:8983/solr/select/";
120
		}
121
		SOLR_URL = solr_url;
122
	}
123
 
317 ashish 124
	/**
125
	 * 
126
	 */
127
	private XPath xpath;
128
 
129
	/**
130
	 * 
131
	 */
132
	private InputSource inputSource;
133
 
5729 amit.gupta 134
	Map<String,Map<String,Integer>> facetMap;
354 rajveer 135
 
6866 amit.gupta 136
	private String 	query;
137
 
790 vikas 138
	List<String> resultMap;
545 rajveer 139
 
140
	long numberOfResults=0;
3561 rajveer 141
 
142
	String priceFacetName = "F_50002";
143
 
6998 amit.gupta 144
	List<String> filtrableFacets;
145
 
317 ashish 146
	/**
147
	 * 
148
	 * @param query
149
	 * @param facetDefinitionIDs
150
	 */
6998 amit.gupta 151
	public SolrSearchService(String query, String[] facetqueries, long start, long rows,  Double minPrice, Double maxPrice, String sortOrder, long sourceId) {
6866 amit.gupta 152
		this.query = query;
354 rajveer 153
 
6998 amit.gupta 154
		List<String> facetsQueried = new ArrayList<String>();
3561 rajveer 155
		if(sourceId != -1){
156
			priceFacetName = priceFacetName + "_" + sourceId;
157
		}
158
 
6998 amit.gupta 159
		setFilterableFacets(facetqueries);
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
 
317 ashish 167
		String uri = SOLR_URL + "?wt=xml&q=" + query;
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])) {
6866 amit.gupta 191
					uri += " OR ";
8275 amit.gupta 192
					if(facetqueries[i].contains(" OR ")){
193
						fq +=  tokens[0] + ":\"" + tokens[1] + "\"";
6866 amit.gupta 194
					}else{
8275 amit.gupta 195
						fq += tokens[0] + ":" + value;
6866 amit.gupta 196
					}
8275 amit.gupta 197
					uri += "&fq=" + fq;
6866 amit.gupta 198
 
199
				} else {
6998 amit.gupta 200
					fq = "{!tag=dt" + facetsQueried.size() + "}";
201
					facetsQueried.add(tokens[0]);
8275 amit.gupta 202
					if(facetqueries[i].contains(" OR ")){
6998 amit.gupta 203
						fq +=  tokens[0] + ":\"" + tokens[1] + "\"";
204
					}else{
8275 amit.gupta 205
						fq += tokens[0] + ":" + value;
6866 amit.gupta 206
					}
207
					uri += "&fq=" + fq;
536 rajveer 208
				}
317 ashish 209
			}
210
		}
6931 amit.gupta 211
		String minString = "0";
212
		String maxString = "*";  
213
		if(minPrice != null || maxPrice != null){
214
			try {
215
				dynamicPriceMap = getPriceStatsMap(new InputSource(uri)); 
216
			} catch (Exception e){
217
				e.printStackTrace();
218
			}
219
			if(minPrice != null){
220
				minString = minPrice.toString();
221
			}
222
			if(maxPrice != null){
223
				maxString = maxPrice.toString();
224
			}
225
		}
226
		uri += "&fq=" + priceFacetName + ":["+  minString + " " + maxString + "]";
6866 amit.gupta 227
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows + "&facet.mincount=1";
6998 amit.gupta 228
		for(String facetDefinitionID : filtrableFacets) {
229
				if(facetsQueried.contains(facetDefinitionID)){
230
					uri += "&facet.field={!ex=dt" + facetsQueried.indexOf(facetDefinitionID)+ "}"+ facetDefinitionID; 
6866 amit.gupta 231
				} else {
6998 amit.gupta 232
					uri += "&facet.field=" + facetDefinitionID;
6866 amit.gupta 233
				}
317 ashish 234
		}
3262 rajveer 235
		log.info("uri=" + uri);
317 ashish 236
 
237
		this.inputSource = new InputSource(uri);
517 rajveer 238
 
545 rajveer 239
		this.facetMap = getFacetMap();
354 rajveer 240
	}
241
 
6998 amit.gupta 242
	@SuppressWarnings("unchecked")
243
	private void setFilterableFacets(String[] facetqueries) {
244
		List<String> queriedFacets = getAllMatches(this.query);
245
		if(facetqueries != null) {
246
			String facetString = Arrays.toString(facetqueries);
247
			List<String> filteredFacets = getAllMatches(facetString);
248
			if(filteredFacets.contains("F_50011")){
249
				for(String facetQuery : facetqueries) {
250
					if(facetQuery.contains("F_50011")){
251
						String facetVal = facetQuery.split(":")[1];
252
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
253
							this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
254
							return;
255
						} else {
256
							break;
257
						}
258
					}
259
				}
260
			}
261
			if(filteredFacets.contains("F_50010")){
262
				for(String facetQuery : facetqueries) {
263
					if(facetQuery.contains("F_50010")){
264
						String facetVal = facetQuery.split(":")[1];
265
						if(CATEGORY_FACET_MAP.containsKey(facetVal)){
266
							this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
267
							return;
268
						} else {
269
							break;
270
						}
271
					}
272
				}
273
			}
274
		}
275
		if(queriedFacets.contains("F_50011")) {
276
			String facetVal = this.query.split("F_50011:")[1];
277
			if (facetVal.contains(" OR ")) {
278
				this.filtrableFacets = Utils.rootfacetDefIDs;
279
				return;
280
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
281
				facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
282
				if(CATEGORY_FACET_MAP.containsKey(facetVal)){
283
					this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
284
					return;
285
				}
286
			} 
287
		}
288
		if(queriedFacets.contains("F_50010")){
289
			String facetVal = this.query.split("F_50010:")[1];
290
			if (facetVal.contains(" OR ")) {
291
				this.filtrableFacets = Utils.rootfacetDefIDs;
292
				return;
293
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
294
				facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
295
				if(CATEGORY_FACET_MAP.containsKey(facetVal)){
296
					this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
297
					return;
298
				}
299
			}
300
		}
301
		this.filtrableFacets = Utils.rootfacetDefIDs;
302
	}
303
 
304
	public List<String> getFilterableFacets() {
305
		return this.filtrableFacets; 
306
	}
307
 
5729 amit.gupta 308
	public Map<String,Map<String,Integer>> removeUnwantedFacets(Map<String,Map<String,Integer>> facetMap, long numberOfResults){
6866 amit.gupta 309
 
310
		Set<String> facetsInQuery = new HashSet<String>(getAllMatches(this.query));
5729 amit.gupta 311
		Map<String,Map<String,Integer>> tempFacets = new TreeMap<String, Map<String,Integer>>(); 
354 rajveer 312
		for(String facet : facetMap.keySet()){
6866 amit.gupta 313
			if(facetMap.get(facet).size() > 0 && !facetsInQuery.contains(facet)){
5729 amit.gupta 314
				Map<String,Integer> tempMap = new LinkedHashMap<String, Integer>();
545 rajveer 315
 
354 rajveer 316
				for(String facetValueName : facetMap.get(facet).keySet()){
6866 amit.gupta 317
					//if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
545 rajveer 318
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
6866 amit.gupta 319
					//}
354 rajveer 320
				}
545 rajveer 321
				if(!tempMap.isEmpty()){
322
					tempFacets.put(facet, tempMap);
354 rajveer 323
				}
545 rajveer 324
			}	
354 rajveer 325
		}
6866 amit.gupta 326
		/*if(tempFacets.containsKey("F_50010")){
550 rajveer 327
			tempFacets.remove("F_50011");
6866 amit.gupta 328
		}*/
354 rajveer 329
 
330
		return tempFacets;
331
	}
332
 
5729 amit.gupta 333
	public Map<String,Integer> getFacetDetails(String facetName){
2606 rajveer 334
		if(facetMap != null){
335
			return facetMap.get(facetName);
336
		}else{
337
			return null;
338
		}
354 rajveer 339
	}
340
 
5729 amit.gupta 341
	public Map<String,Map<String,Integer>> getFacetMap() {
342
		facetMap = new TreeMap<String,Map<String,Integer>>();
354 rajveer 343
 
344
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
545 rajveer 345
 
354 rajveer 346
		NodeList nodes = null;
347
		try {
348
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
349
		}
350
		catch (XPathExpressionException xpee) {
351
			return null;
352
		}
353
 
354
		if(nodes.getLength() == 0) {
355
			return null;
356
		}
357
 
358
		NodeList subNodes = null;
359
 
360
		for(int i=0; i<nodes.getLength(); i++) {
361
			Node node = nodes.item(i);
2946 chandransh 362
			String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
354 rajveer 363
			subNodes = node.getChildNodes();
5729 amit.gupta 364
			Map<String,Integer> facetValueCountMap = new LinkedHashMap<String,Integer>();
354 rajveer 365
			for(int j=0; j<subNodes.getLength(); j++) {
366
				Node subNode = subNodes.item(j);
367
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
368
			}
5729 amit.gupta 369
			if(SORTED_FACET_VALUE_MAP.containsKey(facetName)){
370
				List<String> orderedValues = SORTED_FACET_VALUE_MAP.get(facetName);
371
				Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
372
			    for (Iterator<String> it = orderedValues.iterator(); it.hasNext();) {
373
			    	String val = it.next();
374
			        if(facetValueCountMap.containsKey(val)) {
375
			        	sortedMap.put(val, facetValueCountMap.get(val));
376
			        }
377
			    }
378
			    facetMap.put(facetName, sortedMap);
379
			} else {
380
				facetMap.put(facetName, facetValueCountMap);
354 rajveer 381
			}
5729 amit.gupta 382
		}
545 rajveer 383
		this.numberOfResults  = this.getTotalResults();
517 rajveer 384
 
354 rajveer 385
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
386
		return facetMap;
5729 amit.gupta 387
	}
354 rajveer 388
 
790 vikas 389
	public List<String> getResultMap() {
390
		resultMap = new LinkedList<String>();
354 rajveer 391
 
392
		String resultDocsPath = "/response/result/doc";
393
 
394
 
395
		NodeList nodes = null;
396
		try {
397
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
398
		}
399
		catch (XPathExpressionException xpee) {
400
			return null;
401
		}
402
 
403
		if(nodes.getLength() == 0) {
404
			return null;
405
		}
406
 
407
		for(int i=0; i<nodes.getLength(); i++) {
408
			Node node = nodes.item(i);
409
			String docID = node.getFirstChild().getTextContent();
790 vikas 410
			resultMap.add(docID);	
354 rajveer 411
 		}
412
		return resultMap;
413
	}
414
 
415
	public HashMap<String, Double> getPriceStatsMap() {
6931 amit.gupta 416
		return this.getPriceStatsMap(this.inputSource);
417
	}
418
 
419
	public HashMap<String, Double> getPriceStatsMap(InputSource inputSource) {
354 rajveer 420
		HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
421
 
3561 rajveer 422
		String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = '" + priceFacetName + "']";
354 rajveer 423
 
424
 
425
		NodeList nodes = null;
426
		try {
6931 amit.gupta 427
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, inputSource, XPathConstants.NODESET);
354 rajveer 428
		}
429
		catch (XPathExpressionException xpee) {
430
			return null;
431
		}
432
 
433
		if(nodes.getLength() == 0) {
434
			return null;
435
		}
436
 
437
		NodeList subNodes = nodes.item(0).getChildNodes();
438
 
439
		for(int i=0; i<subNodes.getLength(); i++) {
440
			Node node = subNodes.item(i);
441
 
442
			String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
443
			String value = node.getTextContent();
444
			priceStatsMap.put(parameter, Double.parseDouble(value));	
445
 		}
446
		return priceStatsMap;
447
	}
448
 
449
	public HashMap<String,Integer> getRangeQueryResultMap() {
450
		HashMap<String, Integer> rangeQueryResultMap = new HashMap<String,Integer>();
451
 
452
		String resultDocsPath = "/response/lst[@name = 'facet_counts']/lst[@name = 'facet_queries']/int";
453
 
454
 
455
		NodeList nodes = null;
456
		try {
457
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
458
		}
459
		catch (XPathExpressionException xpee) {
460
			return null;
461
		}
462
 
463
		if(nodes.getLength() == 0) {
464
			return null;
465
		}
466
 
467
 
468
		for(int i=0; i<nodes.getLength(); i++) {
469
			Node node = nodes.item(i);
470
 
471
			String query = node.getAttributes().getNamedItem("name").getNodeValue();
472
			String docCount = node.getTextContent();
473
 
474
			rangeQueryResultMap.put(query,Integer.parseInt(docCount));	
475
 		}
476
		return rangeQueryResultMap;
477
 
478
	}
479
 
545 rajveer 480
	/**
481
	 * 
482
	 */
483
	public long getTotalResults(){
484
		String resultDocsPath = "/response/result";
485
		NodeList nodes = null;
486
		try {
487
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
488
		}
489
		catch (XPathExpressionException xpee) {
490
			return 0;
491
		}
492
 
493
		Node node = nodes.item(0);
494
 
495
		return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
496
 
497
	}
354 rajveer 498
		/**
317 ashish 499
	 * 
500
	 * @return
501
	 */
502
	public long[] getResultEntityIDs() {
503
		String expression = "/response/result/doc/long";
504
 
505
		NodeList nodes = null;
506
		try {
507
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
508
					XPathConstants.NODESET);
509
		} 
510
		catch(XPathExpressionException xpee) {
511
			return null;
512
		}
513
 
514
		if(nodes.getLength() == 0) {
515
			return null;
516
		}
517
 
518
		long[] values = new long[nodes.getLength()];
519
		for(int i=0; i<nodes.getLength(); i++) {
520
			Node node = nodes.item(i);
521
			String value = node.getTextContent();
522
			values[i] = Long.parseLong(value);
523
 		}
524
 
525
		return values;
526
	}
527
 
528
	/**
529
	 * 
530
	 * @return
531
	 */
532
	public String[] getResultCategoryNames() {
533
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
534
		expression += "lst[@name = 'Category']/int/@name";
535
 
536
		NodeList nodes = null;
537
		try {
538
			nodes = (NodeList) this.xpath.evaluate(expression, 
539
				this.inputSource, XPathConstants.NODESET);
540
		}
541
		catch (XPathExpressionException xpee) {
542
			return null;
543
		}
544
 
545
		if(nodes.getLength() == 0) {
546
			return null;
547
		}
548
 
549
		String[] values = new String[nodes.getLength()];
550
		for(int i=0; i<nodes.getLength(); i++) {
551
			Node node = nodes.item(i);
552
			values[i] = node.getTextContent();
553
 		}
554
 
555
		return values;
556
	}
557
 
558
	/**
559
	 * 
560
	 * @return
561
	 */
562
	public int[] getResultCategoryCounts() {
563
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
564
		expression += "lst[@name = 'Category']/int";
565
 
566
		NodeList nodes = null;
567
		try {
568
			nodes = (NodeList) this.xpath.evaluate(expression, 
569
				this.inputSource, XPathConstants.NODESET);
570
		}
571
		catch (XPathExpressionException xpee) {
572
			return null;
573
		}
574
 
575
		if(nodes.getLength() == 0) {
576
			return null;
577
		}
578
 
579
		int[] values = new int[nodes.getLength()];
580
		for(int i=0; i<nodes.getLength(); i++) {
581
			Node node = nodes.item(i);
582
			values[i] = Integer.parseInt(node.getTextContent());
583
 		}
584
 
585
		return values;
586
	}
587
 
588
	/**
589
	 * 
590
	 * @return
591
	 */
592
	public String[]  getResultEntityNames() {
593
		String expression = "/response/result/doc/str";
594
 
595
		NodeList nodes = null;
596
		try {
597
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
598
					XPathConstants.NODESET);
599
		} 
600
		catch(XPathExpressionException xpee) {
601
			return null;
602
		}
603
 
604
		if(nodes.getLength() == 0) {
605
			return null;
606
		}
607
 
608
		String[] values = new String[nodes.getLength()];
609
		for(int i=0; i<nodes.getLength(); i++) {
610
			Node node = nodes.item(i);
611
			String value = node.getTextContent();
612
			values[i] = value;
613
 		}
614
 
615
		return values;
616
	}
617
 
618
	/**
619
	 * 
620
	 * @param facetDefinitionID
621
	 * @return
622
	 */
354 rajveer 623
	public String[] getFacetValues(String facetDefinitionID) {
317 ashish 624
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 625
		expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
317 ashish 626
 
627
		NodeList nodes = null;
628
		try {
629
			nodes = (NodeList) this.xpath.evaluate(expression, 
630
				this.inputSource, XPathConstants.NODESET);
631
		}
632
		catch (XPathExpressionException xpee) {
633
			return null;
634
		}
635
 
636
		if(nodes.getLength() == 0) {
637
			return null;
638
		}
639
 
640
		String[] values = new String[nodes.getLength()];
641
		for(int i=0; i<nodes.getLength(); i++) {
642
			Node node = nodes.item(i);
643
			values[i] = node.getTextContent();
545 rajveer 644
		}
317 ashish 645
 
646
		return values;
647
	}
648
 
649
	/**
650
	 * 
651
	 * @param facetDefinitionID
652
	 * @return
653
	 */
354 rajveer 654
	public String[] getFacetCounts(String facetDefinitionID) {
317 ashish 655
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 656
		expression += "lst[@name = '" + facetDefinitionID + "']/int";
317 ashish 657
 
658
		NodeList nodes = null;
659
		try {
660
			nodes = (NodeList) this.xpath.evaluate(expression, 
661
				this.inputSource, XPathConstants.NODESET);
662
		}
663
		catch (XPathExpressionException xpee) {
664
			return null;
665
		}
666
 
667
		if(nodes.getLength() == 0) {
668
			return null;
669
		}
670
 
671
		String[] values = new String[nodes.getLength()];
672
		for(int i=0; i<nodes.getLength(); i++) {
673
			Node node = nodes.item(i);
674
			values[i] = node.getTextContent();
675
 		}
676
 
677
		return values;
678
	}
545 rajveer 679
 
680
	public static void main(String[] args){
681
		/*
682
    	// Hard coded for now
683
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
684
 
685
    	// Hard-coded for now
686
    	String[] facetLabels = new String[] {
687
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
688
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
689
	    	"Talk time"
690
    	};
691
 
692
		 */
693
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
2147 chandransh 694
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
545 rajveer 695
 
696
 
697
    	String[] fqrys = {};
6998 amit.gupta 698
		SolrSearchService search = new SolrSearchService("nokia", fqrys, 0 , 20, null, null, null, -1);
545 rajveer 699
 
700
    	long[] entityIDs = search.getResultEntityIDs();
701
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
702
 
703
    	String[] entityNames = search.getResultEntityNames();
704
    	log.info("entityNames=" + Arrays.toString(entityNames));
705
    	search.getFacetMap();
706
 
707
    	search.getResultMap();
708
    	search.getRangeQueryResultMap();
6931 amit.gupta 709
    	search.getPriceStatsMap(new InputSource());
545 rajveer 710
    	search.getTotalResults();
711
       	for (int i=0; i<facetDefIDs.length; i++) {
712
       		search.getFacetCounts(facetDefIDs[i]);
713
       		search.getFacetValues(facetDefIDs[i]);
714
       	}
715
 
716
	}
6866 amit.gupta 717
 
718
 
719
    public static List<String> getAllMatches(String text) {
720
        List<String> matches = new ArrayList<String>();
721
        Matcher m = FACET_PATTERN.matcher(text);
722
        while(m.find()) {
723
            matches.add(m.group(1));
724
        }
725
        return matches;
726
    }
6931 amit.gupta 727
 
728
    public Map<String, Double> getDynamicPriceMap() {
729
    	return this.dynamicPriceMap;
730
    }
731
 
317 ashish 732
}