Subversion Repositories SmartDukaan

Rev

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