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