Subversion Repositories SmartDukaan

Rev

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