Subversion Repositories SmartDukaan

Rev

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