Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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