Subversion Repositories SmartDukaan

Rev

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