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