Subversion Repositories SmartDukaan

Rev

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