Subversion Repositories SmartDukaan

Rev

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