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