Subversion Repositories SmartDukaan

Rev

Rev 2435 | Rev 2946 | 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){
49
			cex.printStackTrace();
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;
317 ashish 70
	/**
71
	 * 
72
	 * @param query
73
	 * @param facetDefinitionIDs
74
	 */
569 rajveer 75
	public SolrSearchService(String query, String[] facetqueries, String[] facetDefinitionIDs, long start, long rows,  Double minPrice, Double maxPrice, long categoryId, String sortOrder) {
354 rajveer 76
 
317 ashish 77
		this.xpath = XPathFactory.newInstance().newXPath();
545 rajveer 78
 
79
    	// if multiple words are given, it should do AND for all of them
2606 rajveer 80
		query = query.trim().replaceAll("\\s+", " ");
545 rajveer 81
    	if(query.contains(" ")){
82
    		String[] tokens = query.split("\\s+");
83
    		query = tokens[0];
84
    		for(int i = 1; i < tokens.length; i++){
85
    			query = query + " AND " + tokens[i];
86
    		}
87
    	}
88
    	log.info("query=" + query);
89
 
317 ashish 90
		String uri = SOLR_URL + "?wt=xml&q=" + query;
91
 
545 rajveer 92
		uri += "&stats=on&stats.field=F_50002";
93
 
94
 
95
		if(minPrice != null || maxPrice != null){
96
			String minString = "0";
97
			String maxString = "*";  
98
			if(minPrice != null){
99
				minString = minPrice.toString();
100
			}
101
			if(maxPrice != null){
102
				maxString = maxPrice.toString();
103
			}
104
			uri += "&fq=F_50002:["+  minString + " " + maxString + "]";
354 rajveer 105
		}
106
 
569 rajveer 107
		if(categoryId != 10000){
108
			uri += "&fq=F_50010:\"" + CategoryManager.getCategoryManager().getCategoryLabel(categoryId) + "\"";
109
		}
354 rajveer 110
 
569 rajveer 111
		if(sortOrder != null){
112
			uri += "&sort=" + sortOrder;
113
		}
545 rajveer 114
 
317 ashish 115
		if(facetqueries != null) {
116
			for(int i=0; i<facetqueries.length; i++) {
536 rajveer 117
				if(facetqueries[i].contains(" ") && !facetqueries[i].contains("F_50002")){
118
					String[] tokens = facetqueries[i].split(":");
545 rajveer 119
					uri += "&fq=" + tokens[0] + ":\"" + tokens[1] + "\"";
536 rajveer 120
				}else{
121
					uri += "&fq=" + facetqueries[i] + "";
122
				}
317 ashish 123
			}
124
		}
545 rajveer 125
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows;
2435 rajveer 126
		if(facetDefinitionIDs != null){
127
			for(int i=0; i<facetDefinitionIDs.length; i++) {
128
				uri += "&facet.field=" + facetDefinitionIDs[i];
129
			}
317 ashish 130
		}
2435 rajveer 131
		log.debug("uri=" + uri);
317 ashish 132
 
133
		this.inputSource = new InputSource(uri);
517 rajveer 134
 
545 rajveer 135
		this.facetMap = getFacetMap();
354 rajveer 136
	}
137
 
545 rajveer 138
	public TreeMap<String,HashMap<String,Integer>> removeUnwantedFacets(TreeMap<String,HashMap<String,Integer>> facetMap, long numberOfResults){
139
		TreeMap<String,HashMap<String,Integer>> tempFacets = new TreeMap<String, HashMap<String,Integer>>(); 
354 rajveer 140
		for(String facet : facetMap.keySet()){
545 rajveer 141
			if(facetMap.get(facet).size() > 1){
142
				HashMap<String,Integer> tempMap = new HashMap<String, Integer>();
143
 
354 rajveer 144
				for(String facetValueName : facetMap.get(facet).keySet()){
545 rajveer 145
					if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
146
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
354 rajveer 147
					}
148
				}
545 rajveer 149
				if(!tempMap.isEmpty()){
150
					tempFacets.put(facet, tempMap);
354 rajveer 151
				}
545 rajveer 152
			}	
354 rajveer 153
		}
550 rajveer 154
		if(tempFacets.containsKey("F_50010")){
155
			tempFacets.remove("F_50011");
156
		}
354 rajveer 157
 
158
		return tempFacets;
159
	}
160
 
545 rajveer 161
/*
162
	int sumOfFacetCounts = 0;
163
	for(String facetValueName : facetMap.get(facet).keySet()){
164
		if(facetMap.get(facet).get(facetValueName) == 0 || facetMap.get(facet).get(facetValueName) == numberOfResults){
165
			tempFacets.get(facet).remove(facetValueName);
166
			// FIXME
167
			/
168
			HashMap<String, Integer> tmp = tempFacets.get(facet);
169
			tmp.remove(facetValueName);
170
			tempFacets.put(facet, tmp);
171
			/
172
		}else{
173
			sumOfFacetCounts += facetMap.get(facet).get(facetValueName);
174
		}
175
	}
176
	if(sumOfFacetCounts < numberOfResults*PERCENTAGE_OF_TOTAL_RESULTS/100){
177
		tempFacets.remove(facet);
178
	}
179
}
180
 
181
*/
354 rajveer 182
	public HashMap<String,Integer> getFacetDetails(String facetName){
2606 rajveer 183
		if(facetMap != null){
184
			return facetMap.get(facetName);
185
		}else{
186
			return null;
187
		}
354 rajveer 188
	}
189
 
190
	public TreeMap<String,HashMap<String,Integer>> getFacetMap() {
191
		facetMap = new TreeMap<String,HashMap<String,Integer>>();
192
 
193
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
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" };
2147 chandransh 537
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
545 rajveer 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
}