Subversion Repositories SmartDukaan

Rev

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