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