Subversion Repositories SmartDukaan

Rev

Rev 536 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 536 Rev 545
Line 4... Line 4...
4
package in.shop2020.serving.services;
4
package in.shop2020.serving.services;
5
 
5
 
6
 
6
 
7
import java.util.Arrays;
7
import java.util.Arrays;
8
import java.util.HashMap;
8
import java.util.HashMap;
9
import java.util.Iterator;
-
 
10
import java.util.LinkedHashMap;
-
 
11
import java.util.Map;
9
import java.util.Map;
12
import java.util.TreeMap;
10
import java.util.TreeMap;
13
import java.util.Map.Entry;
-
 
14
 
11
 
15
 
12
 
16
import javax.xml.xpath.XPath;
13
import javax.xml.xpath.XPath;
17
import javax.xml.xpath.XPathConstants;
14
import javax.xml.xpath.XPathConstants;
18
import javax.xml.xpath.XPathExpressionException;
15
import javax.xml.xpath.XPathExpressionException;
19
import javax.xml.xpath.XPathFactory;
16
import javax.xml.xpath.XPathFactory;
20
 
17
 
21
import org.apache.juli.logging.Log;
18
import org.apache.juli.logging.Log;
22
import org.apache.juli.logging.LogFactory;
19
import org.apache.juli.logging.LogFactory;
23
import org.w3c.dom.NamedNodeMap;
-
 
24
import org.w3c.dom.Node;
20
import org.w3c.dom.Node;
25
import org.w3c.dom.NodeList;
21
import org.w3c.dom.NodeList;
26
import org.xml.sax.InputSource;
22
import org.xml.sax.InputSource;
27
 
23
 
28
import org.apache.commons.lang.StringUtils;
-
 
29
 
24
 
30
/**
25
/**
31
 * @author naveen
26
 * @author rajveer
32
 *
27
 *
33
 */
28
 */
34
public class SolrSearchService {
29
public class SolrSearchService {
35
	/**
30
	/**
36
	 * 
31
	 * 
37
	 */
32
	 */
38
	private static Log log = LogFactory.getLog(SolrSearchService.class);
33
	private static Log log = LogFactory.getLog(SolrSearchService.class);
39
	
34
	
-
 
35
	/**
-
 
36
	 * 
-
 
37
	 */
40
	private static final int NUMBER_OF_ROWS = 20, PERCENTAGE_OF_TOTAL_RESULTS = 2;
38
	private static final int PERCENTAGE_OF_TOTAL_RESULTS = 2;
-
 
39
	
41
	/**
40
	/**
42
	 * 
41
	 * 
43
	 */
42
	 */
44
	public static String SOLR_URL = "http://localhost:8983/solr/select/";
43
	public static String SOLR_URL = "http://localhost:8983/solr/select/";
45
	
44
	
Line 51... Line 50...
51
	/**
50
	/**
52
	 * 
51
	 * 
53
	 */
52
	 */
54
	private InputSource inputSource;
53
	private InputSource inputSource;
55
	
54
	
56
	private String minPrice = "0";
-
 
57
	
-
 
58
	private String maxPrice = "*";
-
 
59
	
-
 
60
	TreeMap<String,HashMap<String,Integer>> facetMap;
55
	TreeMap<String,HashMap<String,Integer>> facetMap;
61
	
56
	
62
	HashMap<String, String[]> resultMap;
57
	HashMap<String, String[]> resultMap;
-
 
58
	
-
 
59
	long numberOfResults=0;
63
	/**
60
	/**
64
	 * 
61
	 * 
65
	 * @param query
62
	 * @param query
66
	 * @param facetDefinitionIDs
63
	 * @param facetDefinitionIDs
67
	 */
64
	 */
68
	public SolrSearchService(String query, String[] facetqueries,	String[] facetDefinitionIDs) {
65
	public SolrSearchService(String query, String[] facetqueries, String[] facetDefinitionIDs, long start, long rows,  Double minPrice, Double maxPrice) {
69
		
66
		
70
		this.xpath = XPathFactory.newInstance().newXPath();
67
		this.xpath = XPathFactory.newInstance().newXPath();
-
 
68
		
-
 
69
    	// if multiple words are given, it should do AND for all of them
-
 
70
    	if(query.contains(" ")){
-
 
71
    		String[] tokens = query.split("\\s+");
-
 
72
    		query = tokens[0];
-
 
73
    		for(int i = 1; i < tokens.length; i++){
-
 
74
    			query = query + " AND " + tokens[i];
-
 
75
    		}
-
 
76
    	}
-
 
77
    	log.info("query=" + query);
-
 
78
    	
71
		String uri = SOLR_URL + "?wt=xml&q=" + query;
79
		String uri = SOLR_URL + "?wt=xml&q=" + query;
72
		
80
		
73
		/*
-
 
74
		if(facetqueries.containsKey("minPrice")){
-
 
75
			this.minPrice = facetqueries.get("minPrice");
-
 
76
		}else {
-
 
77
			this.minPrice = "0";
-
 
78
		}
-
 
79
		if(facetqueries.containsKey("maxPrice")){
-
 
80
			this.maxPrice = facetqueries.get("minPrice"); 
-
 
81
		}else {
-
 
82
			this.maxPrice = "*";
-
 
83
		}
-
 
84
		facetqueries.remove("minPrice");
-
 
85
		facetqueries.remove("maxPrice");
-
 
86
		*/
-
 
87
		
-
 
88
		// query for price filtering
-
 
89
		//uri += "&facet.query=F_50002:[" + minPrice + " TO " + maxPrice + "]";
-
 
90
		// get stats for price field
-
 
91
		uri += "&stats=on&stats.field=F_50002";
81
		uri += "&stats=on&stats.field=F_50002";
92
//		 
-
 
93
//		if(facetqueries.containsKey("maxPrice")){
-
 
94
//			 if(facetqueries.containsKey("minPrice")){
-
 
95
//				 uri += "&fq=price:["+facetqueries.get("minPrice")+" TO "+ facetqueries.get("maxPrice")+"]";
-
 
96
//			 }else {
-
 
97
//				 uri += "&fq=price:[0 TO "+ facetqueries.get("minPrice")+"]";
-
 
98
//			 }
-
 
99
//				 
-
 
100
//		}
-
 
101
				 
-
 
102
//		for(String facetParam : facetqueries.keySet()){
-
 
103
//				uri += "&fq=" + facetParam + ":" + facetqueries.get(facetParam);
-
 
104
//		}
-
 
105
 
82
 
-
 
83
 
-
 
84
		if(minPrice != null || maxPrice != null){
-
 
85
			String minString = "0";
-
 
86
			String maxString = "*";  
-
 
87
			if(minPrice != null){
-
 
88
				minString = minPrice.toString();
-
 
89
			}
-
 
90
			if(maxPrice != null){
-
 
91
				maxString = maxPrice.toString();
-
 
92
			}
-
 
93
			uri += "&fq=F_50002:["+  minString + " " + maxString + "]";
-
 
94
		}
-
 
95
		
-
 
96
 
-
 
97
		
106
		
98
		
107
		if(facetqueries != null) {
99
		if(facetqueries != null) {
108
			for(int i=0; i<facetqueries.length; i++) {
100
			for(int i=0; i<facetqueries.length; i++) {
109
 
-
 
110
				if(facetqueries[i].contains(" ") && !facetqueries[i].contains("F_50002")){
101
				if(facetqueries[i].contains(" ") && !facetqueries[i].contains("F_50002")){
111
					String[] tokens = facetqueries[i].split(":");
102
					String[] tokens = facetqueries[i].split(":");
112
					uri += "&fq=" + tokens[0] + "\"" + tokens[1] + "\"";
103
					uri += "&fq=" + tokens[0] + ":\"" + tokens[1] + "\"";
113
				}else{
104
				}else{
114
					uri += "&fq=" + facetqueries[i] + "";
105
					uri += "&fq=" + facetqueries[i] + "";
115
				}
106
				}
116
			}
107
			}
117
		}
108
		}
118
		uri += "&fl=ID,Name&facet=true&rows=" + NUMBER_OF_ROWS;
109
		uri += "&fl=ID,Name&facet=true&start=" + start + "&rows=" + rows;
119
		
110
		
120
		for(int i=0; i<facetDefinitionIDs.length; i++) {
111
		for(int i=0; i<facetDefinitionIDs.length; i++) {
121
			uri += "&facet.field=" + facetDefinitionIDs[i];
112
			uri += "&facet.field=" + facetDefinitionIDs[i];
122
		}
113
		}
123
		log.info("uri=" + uri);
114
		log.info("uri=" + uri);
124
		
115
		
125
		this.inputSource = new InputSource(uri);
116
		this.inputSource = new InputSource(uri);
126
		this.facetMap = new TreeMap<String, HashMap<String,Integer>>(getFacetMap());
-
 
127
	}
-
 
128
	
-
 
129
	public static void main(String[] args){
-
 
130
		/*
-
 
131
    	// Hard coded for now
-
 
132
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
-
 
133
 
117
 
134
    	// Hard-coded for now
-
 
135
    	String[] facetLabels = new String[] {
-
 
136
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
-
 
137
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
-
 
138
	    	"Talk time"
-
 
139
    	};
-
 
140
 
-
 
141
		 */
-
 
142
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
-
 
143
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
-
 
144
 
-
 
145
    	
-
 
146
    	String[] fqrys = {};
-
 
147
		SolrSearchService search = new SolrSearchService("nokia", fqrys, facetDefIDs);
-
 
148
    	
-
 
149
    	long[] entityIDs = search.getResultEntityIDs();
-
 
150
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
-
 
151
    	
-
 
152
    	String[] entityNames = search.getResultEntityNames();
-
 
153
    	log.info("entityNames=" + Arrays.toString(entityNames));
-
 
154
    	search.getFacetMap();
118
		this.facetMap = getFacetMap();
155
    	
-
 
156
    	search.getResultMap();
-
 
157
    	search.getRangeQueryResultMap();
-
 
158
    	search.getPriceStatsMap();
-
 
159
    	
-
 
160
       	for (int i=0; i<facetDefIDs.length; i++) {
-
 
161
       		search.getFacetCounts(facetDefIDs[i]);
-
 
162
       		search.getFacetValues(facetDefIDs[i]);
-
 
163
       	}
-
 
164
       	
-
 
165
	}
119
	}
166
 
120
 
167
	
-
 
168
	public TreeMap<String,HashMap<String,Integer>> removeUnwantedFacets(TreeMap<String,HashMap<String,Integer>> facetMap, int numberOfResults){
121
	public TreeMap<String,HashMap<String,Integer>> removeUnwantedFacets(TreeMap<String,HashMap<String,Integer>> facetMap, long numberOfResults){
169
		TreeMap<String,HashMap<String,Integer>> tempFacets = new TreeMap<String, HashMap<String,Integer>>(facetMap); 
122
		TreeMap<String,HashMap<String,Integer>> tempFacets = new TreeMap<String, HashMap<String,Integer>>(); 
170
		for(String facet : facetMap.keySet()){
123
		for(String facet : facetMap.keySet()){
171
			if(facetMap.get(facet).size() <= 1){
124
			if(facetMap.get(facet).size() > 1){
172
				// FIXME  
-
 
173
				tempFacets.remove(facet);
125
				HashMap<String,Integer> tempMap = new HashMap<String, Integer>();
174
			}else{
126
				
175
				int sumOfFacetCounts = 0;
-
 
176
				for(String facetValueName : facetMap.get(facet).keySet()){
127
				for(String facetValueName : facetMap.get(facet).keySet()){
177
					if(facetMap.get(facet).get(facetValueName) == 0 || facetMap.get(facet).get(facetValueName) == numberOfResults){
128
					if(facetMap.get(facet).get(facetValueName) != 0 && facetMap.get(facet).get(facetValueName) != numberOfResults){
178
						tempFacets.get(facet).remove(facetValueName);
-
 
179
						// FIXME
-
 
180
						/*
-
 
181
						HashMap<String, Integer> tmp = tempFacets.get(facet);
-
 
182
						tmp.remove(facetValueName);
-
 
183
						tempFacets.put(facet, tmp);
-
 
184
						*/
-
 
185
					}else{
-
 
186
						sumOfFacetCounts += facetMap.get(facet).get(facetValueName);
129
						tempMap.put(facetValueName, facetMap.get(facet).get(facetValueName));
187
					}
130
					}
188
				}
131
				}
189
				if(sumOfFacetCounts < numberOfResults*PERCENTAGE_OF_TOTAL_RESULTS/100){
132
				if(!tempMap.isEmpty()){
190
					tempFacets.remove(facet);
133
					tempFacets.put(facet, tempMap);
191
				}
134
				}
192
			}
135
			}	
193
		}
136
		}
194
 
137
 
195
		return tempFacets;
138
		return tempFacets;
196
	}
139
	}
197
	
140
	
-
 
141
/*
-
 
142
	int sumOfFacetCounts = 0;
-
 
143
	for(String facetValueName : facetMap.get(facet).keySet()){
-
 
144
		if(facetMap.get(facet).get(facetValueName) == 0 || facetMap.get(facet).get(facetValueName) == numberOfResults){
-
 
145
			tempFacets.get(facet).remove(facetValueName);
-
 
146
			// FIXME
-
 
147
			/
-
 
148
			HashMap<String, Integer> tmp = tempFacets.get(facet);
-
 
149
			tmp.remove(facetValueName);
-
 
150
			tempFacets.put(facet, tmp);
-
 
151
			/
-
 
152
		}else{
-
 
153
			sumOfFacetCounts += facetMap.get(facet).get(facetValueName);
-
 
154
		}
-
 
155
	}
-
 
156
	if(sumOfFacetCounts < numberOfResults*PERCENTAGE_OF_TOTAL_RESULTS/100){
-
 
157
		tempFacets.remove(facet);
-
 
158
	}
-
 
159
}
-
 
160
 
-
 
161
*/
198
	public HashMap<String,Integer> getFacetDetails(String facetName){
162
	public HashMap<String,Integer> getFacetDetails(String facetName){
199
		return facetMap.get(facetName);
163
		return facetMap.get(facetName);
200
	}
164
	}
201
	
165
	
202
	public TreeMap<String,HashMap<String,Integer>> getFacetMap() {
166
	public TreeMap<String,HashMap<String,Integer>> getFacetMap() {
203
		facetMap = new TreeMap<String,HashMap<String,Integer>>();
167
		facetMap = new TreeMap<String,HashMap<String,Integer>>();
204
		
168
		
205
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
169
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
206
		String facetValuePath = "/response/lst/lst[@name = 'facet_fields']/lst/int/@name";
170
		String facetValuePath = "/response/lst/lst[@name = 'facet_fields']/lst/int/@name";
207
		String facetCountPath = "/response/lst/lst[@name = 'facet_fields']/lst/int";
171
		String facetCountPath = "/response/lst/lst[@name = 'facet_fields']/lst/int";
208
		//expression += "lst[@name = 'F_"+ facetDefinitionID +"']/int/@name";
-
 
-
 
172
		
209
		NodeList nodes = null;
173
		NodeList nodes = null;
210
		try {
174
		try {
211
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
175
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
212
		}
176
		}
213
		catch (XPathExpressionException xpee) {
177
		catch (XPathExpressionException xpee) {
Line 222... Line 186...
222
		String facetName = new String();
186
		String facetName = new String();
223
		
187
		
224
		for(int i=0; i<nodes.getLength(); i++) {
188
		for(int i=0; i<nodes.getLength(); i++) {
225
			Node node = nodes.item(i);
189
			Node node = nodes.item(i);
226
			facetName = node.getAttributes().getNamedItem("name").getNodeValue();
190
			facetName = node.getAttributes().getNamedItem("name").getNodeValue();
227
			
-
 
228
//			System.out.println("facetName   :  "  + node.getAttributes().getNamedItem("name").getNodeValue());
-
 
229
			
-
 
230
			subNodes = node.getChildNodes();
191
			subNodes = node.getChildNodes();
231
			HashMap<String,Integer> facetValueCountMap = new HashMap<String,Integer>();
192
			HashMap<String,Integer> facetValueCountMap = new HashMap<String,Integer>();
232
			for(int j=0; j<subNodes.getLength(); j++) {
193
			for(int j=0; j<subNodes.getLength(); j++) {
233
				Node subNode = subNodes.item(j);
194
				Node subNode = subNodes.item(j);
234
				if(Integer.parseInt(subNode.getTextContent())==0)
195
				if(Integer.parseInt(subNode.getTextContent())==0)
235
						continue;
196
						continue;
236
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
197
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
237
	//			System.out.println("facetValue   :  " + subNode.getTextContent()  +  ":"+  subNode.getAttributes().getNamedItem("name").getNodeValue());
-
 
238
			}
198
			}
239
			//System.out.println(facetName +":"+ facetValueCountMap);
-
 
240
			facetMap.put(facetName, facetValueCountMap);
199
			facetMap.put(facetName, facetValueCountMap);
241
			}
200
			}
242
		System.out.println(facetMap);
201
		System.out.println(facetMap);
243
		
202
		
244
		int numberOfResults = 10;
203
		this.numberOfResults  = this.getTotalResults();
245
		
204
		
246
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
205
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
247
		System.out.println(facetMap);
206
		System.out.println(facetMap);
248
		return facetMap;
207
		return facetMap;
249
		}
208
		}
Line 264... Line 223...
264
		
223
		
265
		if(nodes.getLength() == 0) {
224
		if(nodes.getLength() == 0) {
266
			return null;
225
			return null;
267
		}
226
		}
268
		
227
		
269
 
-
 
270
		
-
 
271
		for(int i=0; i<nodes.getLength(); i++) {
228
		for(int i=0; i<nodes.getLength(); i++) {
272
			Node node = nodes.item(i);
229
			Node node = nodes.item(i);
273
			String docID = node.getFirstChild().getTextContent();
230
			String docID = node.getFirstChild().getTextContent();
274
			String[] values = new String[] {node.getFirstChild().getNextSibling().getTextContent()};
231
			String[] values = new String[] {node.getFirstChild().getNextSibling().getTextContent()};
275
			resultMap.put(docID, values);	
232
			resultMap.put(docID, values);	
Line 339... Line 296...
339
		System.out.println("rangeQueryResultMap is " + rangeQueryResultMap);
296
		System.out.println("rangeQueryResultMap is " + rangeQueryResultMap);
340
		return rangeQueryResultMap;
297
		return rangeQueryResultMap;
341
		
298
		
342
	}
299
	}
343
 
300
 
-
 
301
	/**
-
 
302
	 * 
-
 
303
	 */
-
 
304
	public long getTotalResults(){
-
 
305
		String resultDocsPath = "/response/result";
-
 
306
		NodeList nodes = null;
-
 
307
		try {
-
 
308
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
-
 
309
		}
-
 
310
		catch (XPathExpressionException xpee) {
-
 
311
			return 0;
-
 
312
		}
-
 
313
		
-
 
314
		Node node = nodes.item(0);
-
 
315
 
-
 
316
		return Long.parseLong(node.getAttributes().getNamedItem("numFound").getNodeValue());
-
 
317
		
-
 
318
	}
344
		/**
319
		/**
345
	 * 
320
	 * 
346
	 * @return
321
	 * @return
347
	 */
322
	 */
348
	public long[] getResultEntityIDs() {
323
	public long[] getResultEntityIDs() {
Line 485... Line 460...
485
		
460
		
486
		String[] values = new String[nodes.getLength()];
461
		String[] values = new String[nodes.getLength()];
487
		for(int i=0; i<nodes.getLength(); i++) {
462
		for(int i=0; i<nodes.getLength(); i++) {
488
			Node node = nodes.item(i);
463
			Node node = nodes.item(i);
489
			values[i] = node.getTextContent();
464
			values[i] = node.getTextContent();
490
			//log.info("Facets Values  " + values[i]);
-
 
491
 		}
465
		}
492
		
466
		
493
		return values;
467
		return values;
494
	}
468
	}
495
	
469
	
496
	/**
470
	/**
Line 522... Line 496...
522
//			log.info("Facets Counts  " + values[i]);
496
//			log.info("Facets Counts  " + values[i]);
523
 		}
497
 		}
524
		
498
		
525
		return values;
499
		return values;
526
	}
500
	}
-
 
501
 
-
 
502
	public static void main(String[] args){
-
 
503
		/*
-
 
504
    	// Hard coded for now
-
 
505
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
-
 
506
 
-
 
507
    	// Hard-coded for now
-
 
508
    	String[] facetLabels = new String[] {
-
 
509
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
-
 
510
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
-
 
511
	    	"Talk time"
-
 
512
    	};
-
 
513
 
-
 
514
		 */
-
 
515
    	String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
-
 
516
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"	};
-
 
517
 
-
 
518
    	
-
 
519
    	String[] fqrys = {};
-
 
520
		SolrSearchService search = new SolrSearchService("nokia", fqrys, facetDefIDs, 0 , 20, null, null);
-
 
521
    	
-
 
522
    	long[] entityIDs = search.getResultEntityIDs();
-
 
523
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
-
 
524
    	
-
 
525
    	String[] entityNames = search.getResultEntityNames();
-
 
526
    	log.info("entityNames=" + Arrays.toString(entityNames));
-
 
527
    	search.getFacetMap();
-
 
528
    	
-
 
529
    	search.getResultMap();
-
 
530
    	search.getRangeQueryResultMap();
-
 
531
    	search.getPriceStatsMap();
-
 
532
    	search.getTotalResults();
-
 
533
       	for (int i=0; i<facetDefIDs.length; i++) {
-
 
534
       		search.getFacetCounts(facetDefIDs[i]);
-
 
535
       		search.getFacetValues(facetDefIDs[i]);
-
 
536
       	}
-
 
537
       	
-
 
538
	}
527
}
539
}