Subversion Repositories SmartDukaan

Rev

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