Subversion Repositories SmartDukaan

Rev

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