Subversion Repositories SmartDukaan

Rev

Rev 3263 | Rev 5729 | 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;
8
import in.shop2020.thrift.clients.config.ConfigClient;
2070 rajveer 9
import in.shop2020.utils.CategoryManager;
1698 chandransh 10
 
354 rajveer 11
import java.util.Arrays;
12
import java.util.HashMap;
790 vikas 13
import java.util.LinkedList;
14
import java.util.List;
354 rajveer 15
import java.util.TreeMap;
16
 
17
 
317 ashish 18
import javax.xml.xpath.XPath;
19
import javax.xml.xpath.XPathConstants;
20
import javax.xml.xpath.XPathExpressionException;
21
import javax.xml.xpath.XPathFactory;
22
 
832 rajveer 23
import org.apache.log4j.Logger;
317 ashish 24
import org.w3c.dom.Node;
25
import org.w3c.dom.NodeList;
26
import org.xml.sax.InputSource;
27
 
354 rajveer 28
 
317 ashish 29
/**
545 rajveer 30
 * @author rajveer
317 ashish 31
 *
32
 */
33
public class SolrSearchService {
34
	/**
35
	 * 
36
	 */
2147 chandransh 37
	private static Logger log = Logger.getLogger(Class.class);
545 rajveer 38
 
39
	/**
40
	 * 
41
	 */
1698 chandransh 42
	public static final String SOLR_URL;
317 ashish 43
 
1698 chandransh 44
	static {
45
		String solr_url = null;
46
		try {
47
			solr_url = ConfigClient.getClient().get("solr_url");
48
		}catch(ConfigException cex){
2949 chandransh 49
		    log.error("Unable to get the solr URL from the config server. Setting the default value.", cex);
1698 chandransh 50
			solr_url = "http://localhost:8983/solr/select/";
51
		}
52
		SOLR_URL = solr_url;
53
	}
54
 
317 ashish 55
	/**
56
	 * 
57
	 */
58
	private XPath xpath;
59
 
60
	/**
61
	 * 
62
	 */
63
	private InputSource inputSource;
64
 
354 rajveer 65
	TreeMap<String,HashMap<String,Integer>> facetMap;
66
 
790 vikas 67
	List<String> resultMap;
545 rajveer 68
 
69
	long numberOfResults=0;
3561 rajveer 70
 
71
	String priceFacetName = "F_50002";
72
 
317 ashish 73
	/**
74
	 * 
75
	 * @param query
76
	 * @param facetDefinitionIDs
77
	 */
3561 rajveer 78
	public SolrSearchService(String query, String[] facetqueries, String[] facetDefinitionIDs, long start, long rows,  Double minPrice, Double maxPrice, long categoryId, String sortOrder, long sourceId) {
354 rajveer 79
 
3561 rajveer 80
		if(sourceId != -1){
81
			priceFacetName = priceFacetName + "_" + sourceId;
82
		}
83
 
317 ashish 84
		this.xpath = XPathFactory.newInstance().newXPath();
545 rajveer 85
 
2606 rajveer 86
		query = query.trim().replaceAll("\\s+", " ");
545 rajveer 87
    	log.info("query=" + query);
88
 
317 ashish 89
		String uri = SOLR_URL + "?wt=xml&q=" + query;
90
 
3561 rajveer 91
		uri += "&stats=on&stats.field=" + priceFacetName;
545 rajveer 92
 
93
 
94
		if(minPrice != null || maxPrice != null){
95
			String minString = "0";
96
			String maxString = "*";  
97
			if(minPrice != null){
98
				minString = minPrice.toString();
99
			}
100
			if(maxPrice != null){
101
				maxString = maxPrice.toString();
102
			}
3561 rajveer 103
			uri += "&fq=" + priceFacetName + ":["+  minString + " " + maxString + "]";
354 rajveer 104
		}
105
 
569 rajveer 106
		if(categoryId != 10000){
107
			uri += "&fq=F_50010:\"" + CategoryManager.getCategoryManager().getCategoryLabel(categoryId) + "\"";
108
		}
354 rajveer 109
 
569 rajveer 110
		if(sortOrder != null){
3561 rajveer 111
			//replace the price facet name, so that it can pick price for the source.
112
			sortOrder = sortOrder.replace("F_50002", priceFacetName);
569 rajveer 113
			uri += "&sort=" + sortOrder;
114
		}
545 rajveer 115
 
317 ashish 116
		if(facetqueries != null) {
117
			for(int i=0; i<facetqueries.length; i++) {
3262 rajveer 118
				if(facetqueries[i].contains(" ") && !(facetqueries[i].contains("F_50002") || facetqueries[i].contains(" OR "))){
536 rajveer 119
					String[] tokens = facetqueries[i].split(":");
545 rajveer 120
					uri += "&fq=" + tokens[0] + ":\"" + tokens[1] + "\"";
536 rajveer 121
				}else{
122
					uri += "&fq=" + facetqueries[i] + "";
123
				}
317 ashish 124
			}
125
		}
545 rajveer 126
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows;
2435 rajveer 127
		if(facetDefinitionIDs != null){
128
			for(int i=0; i<facetDefinitionIDs.length; i++) {
129
				uri += "&facet.field=" + facetDefinitionIDs[i];
130
			}
317 ashish 131
		}
3262 rajveer 132
		log.info("uri=" + uri);
317 ashish 133
 
134
		this.inputSource = new InputSource(uri);
517 rajveer 135
 
545 rajveer 136
		this.facetMap = getFacetMap();
354 rajveer 137
	}
138
 
545 rajveer 139
	public TreeMap<String,HashMap<String,Integer>> removeUnwantedFacets(TreeMap<String,HashMap<String,Integer>> facetMap, long numberOfResults){
140
		TreeMap<String,HashMap<String,Integer>> tempFacets = new TreeMap<String, HashMap<String,Integer>>(); 
354 rajveer 141
		for(String facet : facetMap.keySet()){
545 rajveer 142
			if(facetMap.get(facet).size() > 1){
143
				HashMap<String,Integer> tempMap = new HashMap<String, Integer>();
144
 
354 rajveer 145
				for(String facetValueName : facetMap.get(facet).keySet()){
545 rajveer 146
					if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
147
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
354 rajveer 148
					}
149
				}
545 rajveer 150
				if(!tempMap.isEmpty()){
151
					tempFacets.put(facet, tempMap);
354 rajveer 152
				}
545 rajveer 153
			}	
354 rajveer 154
		}
550 rajveer 155
		if(tempFacets.containsKey("F_50010")){
156
			tempFacets.remove("F_50011");
157
		}
354 rajveer 158
 
159
		return tempFacets;
160
	}
161
 
162
	public HashMap<String,Integer> getFacetDetails(String facetName){
2606 rajveer 163
		if(facetMap != null){
164
			return facetMap.get(facetName);
165
		}else{
166
			return null;
167
		}
354 rajveer 168
	}
169
 
170
	public TreeMap<String,HashMap<String,Integer>> getFacetMap() {
171
		facetMap = new TreeMap<String,HashMap<String,Integer>>();
172
 
173
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
545 rajveer 174
 
354 rajveer 175
		NodeList nodes = null;
176
		try {
177
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
178
		}
179
		catch (XPathExpressionException xpee) {
180
			return null;
181
		}
182
 
183
		if(nodes.getLength() == 0) {
184
			return null;
185
		}
186
 
187
		NodeList subNodes = null;
188
 
189
		for(int i=0; i<nodes.getLength(); i++) {
190
			Node node = nodes.item(i);
2946 chandransh 191
			String facetName = node.getAttributes().getNamedItem("name").getNodeValue();
354 rajveer 192
			subNodes = node.getChildNodes();
193
			HashMap<String,Integer> facetValueCountMap = new HashMap<String,Integer>();
194
			for(int j=0; j<subNodes.getLength(); j++) {
195
				Node subNode = subNodes.item(j);
196
				if(Integer.parseInt(subNode.getTextContent())==0)
197
						continue;
198
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
199
			}
200
			facetMap.put(facetName, facetValueCountMap);
201
			}
545 rajveer 202
		this.numberOfResults  = this.getTotalResults();
517 rajveer 203
 
354 rajveer 204
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
205
		return facetMap;
206
		}
207
 
790 vikas 208
	public List<String> getResultMap() {
209
		resultMap = new LinkedList<String>();
354 rajveer 210
 
211
		String resultDocsPath = "/response/result/doc";
212
 
213
 
214
		NodeList nodes = null;
215
		try {
216
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
217
		}
218
		catch (XPathExpressionException xpee) {
219
			return null;
220
		}
221
 
222
		if(nodes.getLength() == 0) {
223
			return null;
224
		}
225
 
226
		for(int i=0; i<nodes.getLength(); i++) {
227
			Node node = nodes.item(i);
228
			String docID = node.getFirstChild().getTextContent();
790 vikas 229
			resultMap.add(docID);	
354 rajveer 230
 		}
231
		return resultMap;
232
	}
233
 
234
	public HashMap<String, Double> getPriceStatsMap() {
235
		HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
236
 
3561 rajveer 237
		String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = '" + priceFacetName + "']";
354 rajveer 238
 
239
 
240
		NodeList nodes = null;
241
		try {
242
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
243
		}
244
		catch (XPathExpressionException xpee) {
245
			return null;
246
		}
247
 
248
		if(nodes.getLength() == 0) {
249
			return null;
250
		}
251
 
252
		NodeList subNodes = nodes.item(0).getChildNodes();
253
 
254
		for(int i=0; i<subNodes.getLength(); i++) {
255
			Node node = subNodes.item(i);
256
 
257
			String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
258
			String value = node.getTextContent();
259
			priceStatsMap.put(parameter, Double.parseDouble(value));	
260
 		}
261
		return priceStatsMap;
262
	}
263
 
264
	public HashMap<String,Integer> getRangeQueryResultMap() {
265
		HashMap<String, Integer> rangeQueryResultMap = new HashMap<String,Integer>();
266
 
267
		String resultDocsPath = "/response/lst[@name = 'facet_counts']/lst[@name = 'facet_queries']/int";
268
 
269
 
270
		NodeList nodes = null;
271
		try {
272
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
273
		}
274
		catch (XPathExpressionException xpee) {
275
			return null;
276
		}
277
 
278
		if(nodes.getLength() == 0) {
279
			return null;
280
		}
281
 
282
 
283
		for(int i=0; i<nodes.getLength(); i++) {
284
			Node node = nodes.item(i);
285
 
286
			String query = node.getAttributes().getNamedItem("name").getNodeValue();
287
			String docCount = node.getTextContent();
288
 
289
			rangeQueryResultMap.put(query,Integer.parseInt(docCount));	
290
 		}
291
		return rangeQueryResultMap;
292
 
293
	}
294
 
545 rajveer 295
	/**
296
	 * 
297
	 */
298
	public long getTotalResults(){
299
		String resultDocsPath = "/response/result";
300
		NodeList nodes = null;
301
		try {
302
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
303
		}
304
		catch (XPathExpressionException xpee) {
305
			return 0;
306
		}
307
 
308
		Node node = nodes.item(0);
309
 
310
		return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
311
 
312
	}
354 rajveer 313
		/**
317 ashish 314
	 * 
315
	 * @return
316
	 */
317
	public long[] getResultEntityIDs() {
318
		String expression = "/response/result/doc/long";
319
 
320
		NodeList nodes = null;
321
		try {
322
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
323
					XPathConstants.NODESET);
324
		} 
325
		catch(XPathExpressionException xpee) {
326
			return null;
327
		}
328
 
329
		if(nodes.getLength() == 0) {
330
			return null;
331
		}
332
 
333
		long[] values = new long[nodes.getLength()];
334
		for(int i=0; i<nodes.getLength(); i++) {
335
			Node node = nodes.item(i);
336
			String value = node.getTextContent();
337
			values[i] = Long.parseLong(value);
338
 		}
339
 
340
		return values;
341
	}
342
 
343
	/**
344
	 * 
345
	 * @return
346
	 */
347
	public String[] getResultCategoryNames() {
348
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
349
		expression += "lst[@name = 'Category']/int/@name";
350
 
351
		NodeList nodes = null;
352
		try {
353
			nodes = (NodeList) this.xpath.evaluate(expression, 
354
				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
		String[] values = new String[nodes.getLength()];
365
		for(int i=0; i<nodes.getLength(); i++) {
366
			Node node = nodes.item(i);
367
			values[i] = node.getTextContent();
368
 		}
369
 
370
		return values;
371
	}
372
 
373
	/**
374
	 * 
375
	 * @return
376
	 */
377
	public int[] getResultCategoryCounts() {
378
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
379
		expression += "lst[@name = 'Category']/int";
380
 
381
		NodeList nodes = null;
382
		try {
383
			nodes = (NodeList) this.xpath.evaluate(expression, 
384
				this.inputSource, XPathConstants.NODESET);
385
		}
386
		catch (XPathExpressionException xpee) {
387
			return null;
388
		}
389
 
390
		if(nodes.getLength() == 0) {
391
			return null;
392
		}
393
 
394
		int[] values = new int[nodes.getLength()];
395
		for(int i=0; i<nodes.getLength(); i++) {
396
			Node node = nodes.item(i);
397
			values[i] = Integer.parseInt(node.getTextContent());
398
 		}
399
 
400
		return values;
401
	}
402
 
403
	/**
404
	 * 
405
	 * @return
406
	 */
407
	public String[]  getResultEntityNames() {
408
		String expression = "/response/result/doc/str";
409
 
410
		NodeList nodes = null;
411
		try {
412
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
413
					XPathConstants.NODESET);
414
		} 
415
		catch(XPathExpressionException xpee) {
416
			return null;
417
		}
418
 
419
		if(nodes.getLength() == 0) {
420
			return null;
421
		}
422
 
423
		String[] values = new String[nodes.getLength()];
424
		for(int i=0; i<nodes.getLength(); i++) {
425
			Node node = nodes.item(i);
426
			String value = node.getTextContent();
427
			values[i] = value;
428
 		}
429
 
430
		return values;
431
	}
432
 
433
	/**
434
	 * 
435
	 * @param facetDefinitionID
436
	 * @return
437
	 */
354 rajveer 438
	public String[] getFacetValues(String facetDefinitionID) {
317 ashish 439
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 440
		expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
317 ashish 441
 
442
		NodeList nodes = null;
443
		try {
444
			nodes = (NodeList) this.xpath.evaluate(expression, 
445
				this.inputSource, XPathConstants.NODESET);
446
		}
447
		catch (XPathExpressionException xpee) {
448
			return null;
449
		}
450
 
451
		if(nodes.getLength() == 0) {
452
			return null;
453
		}
454
 
455
		String[] values = new String[nodes.getLength()];
456
		for(int i=0; i<nodes.getLength(); i++) {
457
			Node node = nodes.item(i);
458
			values[i] = node.getTextContent();
545 rajveer 459
		}
317 ashish 460
 
461
		return values;
462
	}
463
 
464
	/**
465
	 * 
466
	 * @param facetDefinitionID
467
	 * @return
468
	 */
354 rajveer 469
	public String[] getFacetCounts(String facetDefinitionID) {
317 ashish 470
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 471
		expression += "lst[@name = '" + facetDefinitionID + "']/int";
317 ashish 472
 
473
		NodeList nodes = null;
474
		try {
475
			nodes = (NodeList) this.xpath.evaluate(expression, 
476
				this.inputSource, XPathConstants.NODESET);
477
		}
478
		catch (XPathExpressionException xpee) {
479
			return null;
480
		}
481
 
482
		if(nodes.getLength() == 0) {
483
			return null;
484
		}
485
 
486
		String[] values = new String[nodes.getLength()];
487
		for(int i=0; i<nodes.getLength(); i++) {
488
			Node node = nodes.item(i);
489
			values[i] = node.getTextContent();
490
 		}
491
 
492
		return values;
493
	}
545 rajveer 494
 
495
	public static void main(String[] args){
496
		/*
497
    	// Hard coded for now
498
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
499
 
500
    	// Hard-coded for now
501
    	String[] facetLabels = new String[] {
502
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
503
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
504
	    	"Talk time"
505
    	};
506
 
507
		 */
508
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
2147 chandransh 509
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
545 rajveer 510
 
511
 
512
    	String[] fqrys = {};
3561 rajveer 513
		SolrSearchService search = new SolrSearchService("nokia", fqrys, facetDefIDs, 0 , 20, null, null, 10000, null, -1);
545 rajveer 514
 
515
    	long[] entityIDs = search.getResultEntityIDs();
516
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
517
 
518
    	String[] entityNames = search.getResultEntityNames();
519
    	log.info("entityNames=" + Arrays.toString(entityNames));
520
    	search.getFacetMap();
521
 
522
    	search.getResultMap();
523
    	search.getRangeQueryResultMap();
524
    	search.getPriceStatsMap();
525
    	search.getTotalResults();
526
       	for (int i=0; i<facetDefIDs.length; i++) {
527
       		search.getFacetCounts(facetDefIDs[i]);
528
       		search.getFacetValues(facetDefIDs[i]);
529
       	}
530
 
531
	}
317 ashish 532
}