Subversion Repositories SmartDukaan

Rev

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