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