Subversion Repositories SmartDukaan

Rev

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