Subversion Repositories SmartDukaan

Rev

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