Subversion Repositories SmartDukaan

Rev

Rev 2070 | Rev 2435 | 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;
317 ashish 125
 
126
		for(int i=0; i<facetDefinitionIDs.length; i++) {
354 rajveer 127
			uri += "&facet.field=" + facetDefinitionIDs[i];
317 ashish 128
		}
129
		log.info("uri=" + uri);
130
 
131
		this.inputSource = new InputSource(uri);
517 rajveer 132
 
545 rajveer 133
		this.facetMap = getFacetMap();
354 rajveer 134
	}
135
 
545 rajveer 136
	public TreeMap<String,HashMap<String,Integer>> removeUnwantedFacets(TreeMap<String,HashMap<String,Integer>> facetMap, long numberOfResults){
137
		TreeMap<String,HashMap<String,Integer>> tempFacets = new TreeMap<String, HashMap<String,Integer>>(); 
354 rajveer 138
		for(String facet : facetMap.keySet()){
545 rajveer 139
			if(facetMap.get(facet).size() > 1){
140
				HashMap<String,Integer> tempMap = new HashMap<String, Integer>();
141
 
354 rajveer 142
				for(String facetValueName : facetMap.get(facet).keySet()){
545 rajveer 143
					if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
144
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
354 rajveer 145
					}
146
				}
545 rajveer 147
				if(!tempMap.isEmpty()){
148
					tempFacets.put(facet, tempMap);
354 rajveer 149
				}
545 rajveer 150
			}	
354 rajveer 151
		}
550 rajveer 152
		if(tempFacets.containsKey("F_50010")){
153
			tempFacets.remove("F_50011");
154
		}
354 rajveer 155
 
156
		return tempFacets;
157
	}
158
 
545 rajveer 159
/*
160
	int sumOfFacetCounts = 0;
161
	for(String facetValueName : facetMap.get(facet).keySet()){
162
		if(facetMap.get(facet).get(facetValueName) == 0 || facetMap.get(facet).get(facetValueName) == numberOfResults){
163
			tempFacets.get(facet).remove(facetValueName);
164
			// FIXME
165
			/
166
			HashMap<String, Integer> tmp = tempFacets.get(facet);
167
			tmp.remove(facetValueName);
168
			tempFacets.put(facet, tmp);
169
			/
170
		}else{
171
			sumOfFacetCounts += facetMap.get(facet).get(facetValueName);
172
		}
173
	}
174
	if(sumOfFacetCounts < numberOfResults*PERCENTAGE_OF_TOTAL_RESULTS/100){
175
		tempFacets.remove(facet);
176
	}
177
}
178
 
179
*/
354 rajveer 180
	public HashMap<String,Integer> getFacetDetails(String facetName){
181
		return facetMap.get(facetName);
182
	}
183
 
184
	public TreeMap<String,HashMap<String,Integer>> getFacetMap() {
185
		facetMap = new TreeMap<String,HashMap<String,Integer>>();
186
 
187
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
545 rajveer 188
 
354 rajveer 189
		NodeList nodes = null;
190
		try {
191
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
192
		}
193
		catch (XPathExpressionException xpee) {
194
			return null;
195
		}
196
 
197
		if(nodes.getLength() == 0) {
198
			return null;
199
		}
200
 
201
		NodeList subNodes = null;
202
		String facetName = new String();
203
 
204
		for(int i=0; i<nodes.getLength(); i++) {
205
			Node node = nodes.item(i);
206
			facetName = node.getAttributes().getNamedItem("name").getNodeValue();
207
			subNodes = node.getChildNodes();
208
			HashMap<String,Integer> facetValueCountMap = new HashMap<String,Integer>();
209
			for(int j=0; j<subNodes.getLength(); j++) {
210
				Node subNode = subNodes.item(j);
211
				if(Integer.parseInt(subNode.getTextContent())==0)
212
						continue;
213
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
214
			}
215
			facetMap.put(facetName, facetValueCountMap);
216
			}
217
		System.out.println(facetMap);
517 rajveer 218
 
545 rajveer 219
		this.numberOfResults  = this.getTotalResults();
517 rajveer 220
 
354 rajveer 221
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
222
		System.out.println(facetMap);
223
		return facetMap;
224
		}
225
 
790 vikas 226
	public List<String> getResultMap() {
227
		resultMap = new LinkedList<String>();
354 rajveer 228
 
229
		String resultDocsPath = "/response/result/doc";
230
 
231
 
232
		NodeList nodes = null;
233
		try {
234
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
235
		}
236
		catch (XPathExpressionException xpee) {
237
			return null;
238
		}
239
 
240
		if(nodes.getLength() == 0) {
241
			return null;
242
		}
243
 
244
		for(int i=0; i<nodes.getLength(); i++) {
245
			Node node = nodes.item(i);
246
			String docID = node.getFirstChild().getTextContent();
790 vikas 247
			resultMap.add(docID);	
354 rajveer 248
 		}
249
		System.out.println("resultMap is " + resultMap);
250
		return resultMap;
251
	}
252
 
253
	public HashMap<String, Double> getPriceStatsMap() {
254
		HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
255
 
256
		String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = 'F_50002']";
257
 
258
 
259
		NodeList nodes = null;
260
		try {
261
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
262
		}
263
		catch (XPathExpressionException xpee) {
264
			return null;
265
		}
266
 
267
		if(nodes.getLength() == 0) {
268
			return null;
269
		}
270
 
271
		NodeList subNodes = nodes.item(0).getChildNodes();
272
 
273
		for(int i=0; i<subNodes.getLength(); i++) {
274
			Node node = subNodes.item(i);
275
 
276
			String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
277
			String value = node.getTextContent();
278
			priceStatsMap.put(parameter, Double.parseDouble(value));	
279
 		}
280
		System.out.println("priceStatsMap is " + priceStatsMap);
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
		System.out.println("rangeQueryResultMap is " + rangeQueryResultMap);
312
		return rangeQueryResultMap;
313
 
314
	}
315
 
545 rajveer 316
	/**
317
	 * 
318
	 */
319
	public long getTotalResults(){
320
		String resultDocsPath = "/response/result";
321
		NodeList nodes = null;
322
		try {
323
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
324
		}
325
		catch (XPathExpressionException xpee) {
326
			return 0;
327
		}
328
 
329
		Node node = nodes.item(0);
330
 
331
		return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
332
 
333
	}
354 rajveer 334
		/**
317 ashish 335
	 * 
336
	 * @return
337
	 */
338
	public long[] getResultEntityIDs() {
339
		String expression = "/response/result/doc/long";
340
 
341
		NodeList nodes = null;
342
		try {
343
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
344
					XPathConstants.NODESET);
345
		} 
346
		catch(XPathExpressionException xpee) {
347
			return null;
348
		}
349
 
350
		if(nodes.getLength() == 0) {
351
			return null;
352
		}
353
 
354
		long[] values = new long[nodes.getLength()];
355
		for(int i=0; i<nodes.getLength(); i++) {
356
			Node node = nodes.item(i);
357
			String value = node.getTextContent();
358
			values[i] = Long.parseLong(value);
359
 		}
360
 
361
		return values;
362
	}
363
 
364
	/**
365
	 * 
366
	 * @return
367
	 */
368
	public String[] getResultCategoryNames() {
369
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
370
		expression += "lst[@name = 'Category']/int/@name";
371
 
372
		NodeList nodes = null;
373
		try {
374
			nodes = (NodeList) this.xpath.evaluate(expression, 
375
				this.inputSource, XPathConstants.NODESET);
376
		}
377
		catch (XPathExpressionException xpee) {
378
			return null;
379
		}
380
 
381
		if(nodes.getLength() == 0) {
382
			return null;
383
		}
384
 
385
		String[] values = new String[nodes.getLength()];
386
		for(int i=0; i<nodes.getLength(); i++) {
387
			Node node = nodes.item(i);
388
			values[i] = node.getTextContent();
389
 		}
390
 
391
		return values;
392
	}
393
 
394
	/**
395
	 * 
396
	 * @return
397
	 */
398
	public int[] getResultCategoryCounts() {
399
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
400
		expression += "lst[@name = 'Category']/int";
401
 
402
		NodeList nodes = null;
403
		try {
404
			nodes = (NodeList) this.xpath.evaluate(expression, 
405
				this.inputSource, XPathConstants.NODESET);
406
		}
407
		catch (XPathExpressionException xpee) {
408
			return null;
409
		}
410
 
411
		if(nodes.getLength() == 0) {
412
			return null;
413
		}
414
 
415
		int[] values = new int[nodes.getLength()];
416
		for(int i=0; i<nodes.getLength(); i++) {
417
			Node node = nodes.item(i);
418
			values[i] = Integer.parseInt(node.getTextContent());
419
 		}
420
 
421
		return values;
422
	}
423
 
424
	/**
425
	 * 
426
	 * @return
427
	 */
428
	public String[]  getResultEntityNames() {
429
		String expression = "/response/result/doc/str";
430
 
431
		NodeList nodes = null;
432
		try {
433
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
434
					XPathConstants.NODESET);
435
		} 
436
		catch(XPathExpressionException xpee) {
437
			return null;
438
		}
439
 
440
		if(nodes.getLength() == 0) {
441
			return null;
442
		}
443
 
444
		String[] values = new String[nodes.getLength()];
445
		for(int i=0; i<nodes.getLength(); i++) {
446
			Node node = nodes.item(i);
447
			String value = node.getTextContent();
448
			values[i] = value;
449
 		}
450
 
451
		return values;
452
	}
453
 
454
	/**
455
	 * 
456
	 * @param facetDefinitionID
457
	 * @return
458
	 */
354 rajveer 459
	public String[] getFacetValues(String facetDefinitionID) {
317 ashish 460
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 461
		expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
317 ashish 462
 
463
		NodeList nodes = null;
464
		try {
465
			nodes = (NodeList) this.xpath.evaluate(expression, 
466
				this.inputSource, XPathConstants.NODESET);
467
		}
468
		catch (XPathExpressionException xpee) {
469
			return null;
470
		}
471
 
472
		if(nodes.getLength() == 0) {
473
			return null;
474
		}
475
 
476
		String[] values = new String[nodes.getLength()];
477
		for(int i=0; i<nodes.getLength(); i++) {
478
			Node node = nodes.item(i);
479
			values[i] = node.getTextContent();
545 rajveer 480
		}
317 ashish 481
 
482
		return values;
483
	}
484
 
485
	/**
486
	 * 
487
	 * @param facetDefinitionID
488
	 * @return
489
	 */
354 rajveer 490
	public String[] getFacetCounts(String facetDefinitionID) {
317 ashish 491
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
354 rajveer 492
		expression += "lst[@name = '" + facetDefinitionID + "']/int";
317 ashish 493
 
494
		NodeList nodes = null;
495
		try {
496
			nodes = (NodeList) this.xpath.evaluate(expression, 
497
				this.inputSource, XPathConstants.NODESET);
498
		}
499
		catch (XPathExpressionException xpee) {
500
			return null;
501
		}
502
 
503
		if(nodes.getLength() == 0) {
504
			return null;
505
		}
506
 
507
		String[] values = new String[nodes.getLength()];
508
		for(int i=0; i<nodes.getLength(); i++) {
509
			Node node = nodes.item(i);
510
			values[i] = node.getTextContent();
354 rajveer 511
//			log.info("Facets Counts  " + values[i]);
317 ashish 512
 		}
513
 
514
		return values;
515
	}
545 rajveer 516
 
517
	public static void main(String[] args){
518
		/*
519
    	// Hard coded for now
520
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
521
 
522
    	// Hard-coded for now
523
    	String[] facetLabels = new String[] {
524
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
525
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
526
	    	"Talk time"
527
    	};
528
 
529
		 */
530
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
2147 chandransh 531
    	//String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
545 rajveer 532
 
533
 
534
    	String[] fqrys = {};
569 rajveer 535
		SolrSearchService search = new SolrSearchService("nokia", fqrys, facetDefIDs, 0 , 20, null, null, 10000, null);
545 rajveer 536
 
537
    	long[] entityIDs = search.getResultEntityIDs();
538
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
539
 
540
    	String[] entityNames = search.getResultEntityNames();
541
    	log.info("entityNames=" + Arrays.toString(entityNames));
542
    	search.getFacetMap();
543
 
544
    	search.getResultMap();
545
    	search.getRangeQueryResultMap();
546
    	search.getPriceStatsMap();
547
    	search.getTotalResults();
548
       	for (int i=0; i<facetDefIDs.length; i++) {
549
       		search.getFacetCounts(facetDefIDs[i]);
550
       		search.getFacetValues(facetDefIDs[i]);
551
       	}
552
 
553
	}
317 ashish 554
}