Subversion Repositories SmartDukaan

Rev

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