Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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