Subversion Repositories SmartDukaan

Rev

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