Subversion Repositories SmartDukaan

Rev

Rev 8275 | Rev 8326 | 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])) {
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
		}
216
		uri += "&fq=" + priceFacetName + ":["+  minString + " " + 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);
235
		if(facetqueries != null) {
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)){
243
							this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
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
							this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
257
							return;
258
						} else {
259
							break;
260
						}
261
					}
262
				}
263
			}
264
		}
265
		if(queriedFacets.contains("F_50011")) {
266
			String facetVal = this.query.split("F_50011:")[1];
267
			if (facetVal.contains(" OR ")) {
268
				this.filtrableFacets = Utils.rootfacetDefIDs;
269
				return;
270
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
271
				facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
272
				if(CATEGORY_FACET_MAP.containsKey(facetVal)){
273
					this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
274
					return;
275
				}
276
			} 
277
		}
278
		if(queriedFacets.contains("F_50010")){
279
			String facetVal = this.query.split("F_50010:")[1];
280
			if (facetVal.contains(" OR ")) {
281
				this.filtrableFacets = Utils.rootfacetDefIDs;
282
				return;
283
			} else if(CATEGORY_FACET_MAP.containsKey(facetVal)){
284
				facetVal = facetVal.split("&")[0].replaceAll("[\"()]", "");
285
				if(CATEGORY_FACET_MAP.containsKey(facetVal)){
286
					this.filtrableFacets = ListUtils.sum(Utils.rootfacetDefIDs, CATEGORY_FACET_MAP.get(facetVal));
287
					return;
288
				}
289
			}
290
		}
291
		this.filtrableFacets = Utils.rootfacetDefIDs;
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
}