Subversion Repositories SmartDukaan

Rev

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

Rev 317 Rev 354
Line 2... Line 2...
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.serving.services;
4
package in.shop2020.serving.services;
5
 
5
 
6
 
6
 
-
 
7
import java.util.Arrays;
-
 
8
import java.util.HashMap;
-
 
9
import java.util.Iterator;
-
 
10
import java.util.LinkedHashMap;
-
 
11
import java.util.Map;
-
 
12
import java.util.TreeMap;
-
 
13
import java.util.Map.Entry;
-
 
14
 
-
 
15
 
7
import javax.xml.xpath.XPath;
16
import javax.xml.xpath.XPath;
8
import javax.xml.xpath.XPathConstants;
17
import javax.xml.xpath.XPathConstants;
9
import javax.xml.xpath.XPathExpressionException;
18
import javax.xml.xpath.XPathExpressionException;
10
import javax.xml.xpath.XPathFactory;
19
import javax.xml.xpath.XPathFactory;
11
 
20
 
12
import org.apache.juli.logging.Log;
21
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
22
import org.apache.juli.logging.LogFactory;
-
 
23
import org.w3c.dom.NamedNodeMap;
14
import org.w3c.dom.Node;
24
import org.w3c.dom.Node;
15
import org.w3c.dom.NodeList;
25
import org.w3c.dom.NodeList;
16
import org.xml.sax.InputSource;
26
import org.xml.sax.InputSource;
17
 
27
 
-
 
28
import org.apache.commons.lang.StringUtils;
-
 
29
 
18
/**
30
/**
19
 * @author naveen
31
 * @author naveen
20
 *
32
 *
21
 */
33
 */
22
public class SolrSearchService {
34
public class SolrSearchService {
23
	/**
35
	/**
24
	 * 
36
	 * 
25
	 */
37
	 */
26
	private static Log log = LogFactory.getLog(SolrSearchService.class);
38
	private static Log log = LogFactory.getLog(SolrSearchService.class);
27
	
39
	
-
 
40
	private static final int NUMBER_OF_ROWS = 20, PERCENTAGE_OF_TOTAL_RESULTS = 2;
28
	/**
41
	/**
29
	 * 
42
	 * 
30
	 */
43
	 */
31
	public static String SOLR_URL = "http://localhost:8983/solr/select/";
44
	public static String SOLR_URL = "http://localhost:8983/solr/select/";
32
	
45
	
Line 38... Line 51...
38
	/**
51
	/**
39
	 * 
52
	 * 
40
	 */
53
	 */
41
	private InputSource inputSource;
54
	private InputSource inputSource;
42
	
55
	
-
 
56
	private String minPrice = "0";
-
 
57
	
-
 
58
	private String maxPrice = "*";
-
 
59
	
-
 
60
	TreeMap<String,HashMap<String,Integer>> facetMap;
-
 
61
	
-
 
62
	HashMap<String, String[]> resultMap;
43
	/**
63
	/**
44
	 * 
64
	 * 
45
	 * @param query
65
	 * @param query
46
	 * @param facetDefinitionIDs
66
	 * @param facetDefinitionIDs
47
	 */
67
	 */
48
	public SolrSearchService(String query, String[] facetqueries, 
68
	public SolrSearchService(String query, String[] facetqueries,	String[] facetDefinitionIDs) {
49
			long[] facetDefinitionIDs) {
-
 
50
		this.xpath = XPathFactory.newInstance().newXPath();
-
 
51
		
69
		
-
 
70
		this.xpath = XPathFactory.newInstance().newXPath();
52
		String uri = SOLR_URL + "?wt=xml&q=" + query;
71
		String uri = SOLR_URL + "?wt=xml&q=" + query;
53
		
72
		
-
 
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";
-
 
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
 
-
 
106
		
54
		if(facetqueries != null) {
107
		if(facetqueries != null) {
55
			for(int i=0; i<facetqueries.length; i++) {
108
			for(int i=0; i<facetqueries.length; i++) {
-
 
109
//				if(StringUtils.split(facetqueries[i], ":")[0].equals("minPrice") || StringUtils.split(facetqueries[i], ":")[0].equals("maxPrice")){
-
 
110
//					if(StringUtils.split(facetqueries[i], ":")[0].equals("maxPrice")){
-
 
111
//						this.maxPrice = StringUtils.split(facetqueries[i], ":")[1];
-
 
112
//					}else{
-
 
113
//						this.minPrice = StringUtils.split(facetqueries[i], ":")[1];
-
 
114
//					}
-
 
115
//					
-
 
116
//				}else{
56
				uri += "&fq=" + facetqueries[i];
117
				uri += "&fq=" + facetqueries[i];
-
 
118
//				}
57
			}
119
			}
58
		}
120
		}
59
		
-
 
-
 
121
//		uri += "&fq=F_50002:[" + this.minPrice + " TO " + this.maxPrice + "]";
60
		uri += "&fl=ID,Name&facet=true&rows=20";
122
		uri += "&fl=ID,Name&facet=true&rows=" + NUMBER_OF_ROWS;
61
		uri += "&facet.field=Category";
123
//		uri += "&facet.field=Category";
62
		
124
		
63
		for(int i=0; i<facetDefinitionIDs.length; i++) {
125
		for(int i=0; i<facetDefinitionIDs.length; i++) {
64
			uri += "&facet.field=" + "F_" + facetDefinitionIDs[i];
126
			uri += "&facet.field=" + facetDefinitionIDs[i];
65
		}
127
		}
66
		log.info("uri=" + uri);
128
		log.info("uri=" + uri);
67
		
129
		
68
		this.inputSource = new InputSource(uri);
130
		this.inputSource = new InputSource(uri);
-
 
131
		this.facetMap = new TreeMap<String, HashMap<String,Integer>>(getFacetMap());
69
	}
132
	}
70
	
133
	
-
 
134
	public static void main(String[] args){
-
 
135
    	// Hard coded for now
-
 
136
    	String[] facetDefIDs = new String[] {"F_50001", "F_50002", "F_50003", "F_50004", "F_50005", "F_50006", "F_50007", "F_50008", "F_50009"};
-
 
137
    	
-
 
138
    	// Hard-coded for now
-
 
139
    	String[] facetLabels = new String[] {
-
 
140
	    	"Brand", "Price","Form Factor", "Carry In Pocket", "Cellular Technologies", 
-
 
141
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
-
 
142
	    	"Talk time"
-
 
143
    	};
-
 
144
 
-
 
145
//    	HashMap<String,String> fqrys = new HashMap<String, String>();
-
 
146
    	String[] fqrys = {};
-
 
147
//    	fqrys.put("minPrice", "10");
-
 
148
		SolrSearchService search = new SolrSearchService("nokia", fqrys, facetDefIDs);
-
 
149
    	
-
 
150
    	long[] entityIDs = search.getResultEntityIDs();
-
 
151
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
-
 
152
    	
-
 
153
    	String[] entityNames = search.getResultEntityNames();
-
 
154
    	log.info("entityNames=" + Arrays.toString(entityNames));
-
 
155
    	search.getFacetMap();
-
 
156
    	
-
 
157
    	search.getResultMap();
-
 
158
    	search.getRangeQueryResultMap();
-
 
159
    	search.getPriceStatsMap();
-
 
160
    	
-
 
161
       	for (int i=0; i<facetDefIDs.length; i++) {
-
 
162
       		search.getFacetCounts(facetDefIDs[i]);
-
 
163
       		search.getFacetValues(facetDefIDs[i]);
-
 
164
       	}
-
 
165
       	
-
 
166
	}
-
 
167
 
-
 
168
	
-
 
169
	public TreeMap<String,HashMap<String,Integer>> removeUnwantedFacets(TreeMap<String,HashMap<String,Integer>> facetMap, int numberOfResults){
-
 
170
		TreeMap<String,HashMap<String,Integer>> tempFacets = new TreeMap<String, HashMap<String,Integer>>(facetMap); 
-
 
171
		for(String facet : facetMap.keySet()){
-
 
172
			if(facetMap.get(facet).size() == 1){
-
 
173
				// FIXME  
-
 
174
				//tempFacets.remove(facet);
-
 
175
			}else{
-
 
176
				int sumOfFacetCounts = 0;
-
 
177
				for(String facetValueName : facetMap.get(facet).keySet()){
-
 
178
					if(facetMap.get(facet).get(facetValueName) == 0){
-
 
179
						tempFacets.get(facet).remove(facetValueName);
-
 
180
					}else{
-
 
181
						sumOfFacetCounts += facetMap.get(facet).get(facetValueName);
-
 
182
					}
-
 
183
				}
-
 
184
				if(sumOfFacetCounts < numberOfResults*PERCENTAGE_OF_TOTAL_RESULTS/100){
-
 
185
					tempFacets.remove(facet);
-
 
186
				}
-
 
187
			}
-
 
188
		}
-
 
189
 
-
 
190
		return tempFacets;
-
 
191
	}
-
 
192
	
-
 
193
	public HashMap<String,Integer> getFacetDetails(String facetName){
-
 
194
		return facetMap.get(facetName);
-
 
195
	}
-
 
196
	
-
 
197
	public TreeMap<String,HashMap<String,Integer>> getFacetMap() {
-
 
198
		facetMap = new TreeMap<String,HashMap<String,Integer>>();
-
 
199
		
-
 
200
		String facetNamePath = "/response/lst/lst[@name = 'facet_fields']/lst";
-
 
201
		String facetValuePath = "/response/lst/lst[@name = 'facet_fields']/lst/int/@name";
-
 
202
		String facetCountPath = "/response/lst/lst[@name = 'facet_fields']/lst/int";
-
 
203
		//expression += "lst[@name = 'F_"+ facetDefinitionID +"']/int/@name";
-
 
204
		NodeList nodes = null;
-
 
205
		try {
-
 
206
			nodes = (NodeList) this.xpath.evaluate(facetNamePath, this.inputSource, XPathConstants.NODESET);
-
 
207
		}
-
 
208
		catch (XPathExpressionException xpee) {
-
 
209
			return null;
-
 
210
		}
-
 
211
		
-
 
212
		if(nodes.getLength() == 0) {
-
 
213
			return null;
-
 
214
		}
-
 
215
		
-
 
216
		NodeList subNodes = null;
-
 
217
		String facetName = new String();
-
 
218
		
-
 
219
		for(int i=0; i<nodes.getLength(); i++) {
-
 
220
			Node node = nodes.item(i);
-
 
221
			facetName = node.getAttributes().getNamedItem("name").getNodeValue();
-
 
222
			
-
 
223
//			System.out.println("facetName   :  "  + node.getAttributes().getNamedItem("name").getNodeValue());
-
 
224
			
-
 
225
			subNodes = node.getChildNodes();
-
 
226
			HashMap<String,Integer> facetValueCountMap = new HashMap<String,Integer>();
-
 
227
			for(int j=0; j<subNodes.getLength(); j++) {
-
 
228
				Node subNode = subNodes.item(j);
-
 
229
				if(Integer.parseInt(subNode.getTextContent())==0)
-
 
230
						continue;
-
 
231
				facetValueCountMap.put(subNode.getAttributes().getNamedItem("name").getNodeValue(), Integer.parseInt(subNode.getTextContent()));
-
 
232
	//			System.out.println("facetValue   :  " + subNode.getTextContent()  +  ":"+  subNode.getAttributes().getNamedItem("name").getNodeValue());
-
 
233
			}
-
 
234
			//System.out.println(facetName +":"+ facetValueCountMap);
-
 
235
			facetMap.put(facetName, facetValueCountMap);
-
 
236
			}
-
 
237
		System.out.println(facetMap);
-
 
238
		int numberOfResults = 10;
-
 
239
		facetMap = removeUnwantedFacets(facetMap, numberOfResults);
-
 
240
		System.out.println(facetMap);
-
 
241
		return facetMap;
-
 
242
		}
-
 
243
	
-
 
244
	public HashMap<String, String[]> getResultMap() {
-
 
245
		resultMap = new HashMap<String, String[]>();
-
 
246
 
-
 
247
		String resultDocsPath = "/response/result/doc";
-
 
248
		
-
 
249
		
-
 
250
		NodeList nodes = null;
-
 
251
		try {
-
 
252
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
-
 
253
		}
-
 
254
		catch (XPathExpressionException xpee) {
-
 
255
			return null;
-
 
256
		}
-
 
257
		
-
 
258
		if(nodes.getLength() == 0) {
-
 
259
			return null;
-
 
260
		}
-
 
261
		
-
 
262
 
-
 
263
		
-
 
264
		for(int i=0; i<nodes.getLength(); i++) {
-
 
265
			Node node = nodes.item(i);
-
 
266
			String docID = node.getFirstChild().getTextContent();
-
 
267
			String[] values = new String[] {node.getFirstChild().getNextSibling().getTextContent()};
-
 
268
			resultMap.put(docID, values);	
-
 
269
 		}
-
 
270
		System.out.println("resultMap is " + resultMap);
-
 
271
		return resultMap;
-
 
272
	}
-
 
273
 
-
 
274
	public HashMap<String, Double> getPriceStatsMap() {
-
 
275
		HashMap<String, Double> priceStatsMap = new HashMap<String, Double>();
-
 
276
 
-
 
277
		String resultDocsPath = "/response/lst[@name = 'stats']/lst[@name = 'stats_fields']/lst[@name = 'F_50002']";
-
 
278
		
-
 
279
		
-
 
280
		NodeList nodes = null;
-
 
281
		try {
-
 
282
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
-
 
283
		}
-
 
284
		catch (XPathExpressionException xpee) {
-
 
285
			return null;
-
 
286
		}
-
 
287
		
-
 
288
		if(nodes.getLength() == 0) {
-
 
289
			return null;
-
 
290
		}
-
 
291
		
-
 
292
		NodeList subNodes = nodes.item(0).getChildNodes();
-
 
293
		
-
 
294
		for(int i=0; i<subNodes.getLength(); i++) {
-
 
295
			Node node = subNodes.item(i);
-
 
296
			
-
 
297
			String parameter = node.getAttributes().getNamedItem("name").getNodeValue();
-
 
298
			String value = node.getTextContent();
-
 
299
			priceStatsMap.put(parameter, Double.parseDouble(value));	
-
 
300
 		}
-
 
301
		System.out.println("priceStatsMap is " + priceStatsMap);
-
 
302
		return priceStatsMap;
-
 
303
	}
-
 
304
 
-
 
305
	public HashMap<String,Integer> getRangeQueryResultMap() {
-
 
306
		HashMap<String, Integer> rangeQueryResultMap = new HashMap<String,Integer>();
-
 
307
 
-
 
308
		String resultDocsPath = "/response/lst[@name = 'facet_counts']/lst[@name = 'facet_queries']/int";
-
 
309
		
-
 
310
		
-
 
311
		NodeList nodes = null;
-
 
312
		try {
-
 
313
			nodes = (NodeList) this.xpath.evaluate(resultDocsPath, this.inputSource, XPathConstants.NODESET);
-
 
314
		}
-
 
315
		catch (XPathExpressionException xpee) {
-
 
316
			return null;
-
 
317
		}
-
 
318
		
-
 
319
		if(nodes.getLength() == 0) {
-
 
320
			return null;
-
 
321
		}
-
 
322
		
-
 
323
		
-
 
324
		for(int i=0; i<nodes.getLength(); i++) {
-
 
325
			Node node = nodes.item(i);
-
 
326
 
-
 
327
			String query = node.getAttributes().getNamedItem("name").getNodeValue();
-
 
328
			String docCount = node.getTextContent();
-
 
329
 
-
 
330
			rangeQueryResultMap.put(query,Integer.parseInt(docCount));	
-
 
331
 		}
-
 
332
		System.out.println("rangeQueryResultMap is " + rangeQueryResultMap);
-
 
333
		return rangeQueryResultMap;
-
 
334
		
-
 
335
	}
-
 
336
 
71
	/**
337
		/**
72
	 * 
338
	 * 
73
	 * @return
339
	 * @return
74
	 */
340
	 */
75
	public long[] getResultEntityIDs() {
341
	public long[] getResultEntityIDs() {
76
		String expression = "/response/result/doc/long";
342
		String expression = "/response/result/doc/long";
Line 191... Line 457...
191
	/**
457
	/**
192
	 * 
458
	 * 
193
	 * @param facetDefinitionID
459
	 * @param facetDefinitionID
194
	 * @return
460
	 * @return
195
	 */
461
	 */
196
	public String[] getFacetValues(long facetDefinitionID) {
462
	public String[] getFacetValues(String facetDefinitionID) {
197
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
463
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
198
		expression += "lst[@name = 'F_"+ facetDefinitionID +"']/int/@name";
464
		expression += "lst[@name = '"+ facetDefinitionID +"']/int/@name";
199
		
465
		
200
		NodeList nodes = null;
466
		NodeList nodes = null;
201
		try {
467
		try {
202
			nodes = (NodeList) this.xpath.evaluate(expression, 
468
			nodes = (NodeList) this.xpath.evaluate(expression, 
203
				this.inputSource, XPathConstants.NODESET);
469
				this.inputSource, XPathConstants.NODESET);
Line 212... Line 478...
212
		
478
		
213
		String[] values = new String[nodes.getLength()];
479
		String[] values = new String[nodes.getLength()];
214
		for(int i=0; i<nodes.getLength(); i++) {
480
		for(int i=0; i<nodes.getLength(); i++) {
215
			Node node = nodes.item(i);
481
			Node node = nodes.item(i);
216
			values[i] = node.getTextContent();
482
			values[i] = node.getTextContent();
-
 
483
			//log.info("Facets Values  " + values[i]);
217
 		}
484
 		}
218
		
485
		
219
		return values;
486
		return values;
220
	}
487
	}
221
	
488
	
222
	/**
489
	/**
223
	 * 
490
	 * 
224
	 * @param facetDefinitionID
491
	 * @param facetDefinitionID
225
	 * @return
492
	 * @return
226
	 */
493
	 */
227
	public String[] getFacetCounts(long facetDefinitionID) {
494
	public String[] getFacetCounts(String facetDefinitionID) {
228
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
495
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
229
		expression += "lst[@name = 'F_" + facetDefinitionID + "']/int";
496
		expression += "lst[@name = '" + facetDefinitionID + "']/int";
230
		
497
		
231
		NodeList nodes = null;
498
		NodeList nodes = null;
232
		try {
499
		try {
233
			nodes = (NodeList) this.xpath.evaluate(expression, 
500
			nodes = (NodeList) this.xpath.evaluate(expression, 
234
				this.inputSource, XPathConstants.NODESET);
501
				this.inputSource, XPathConstants.NODESET);
Line 243... Line 510...
243
		
510
		
244
		String[] values = new String[nodes.getLength()];
511
		String[] values = new String[nodes.getLength()];
245
		for(int i=0; i<nodes.getLength(); i++) {
512
		for(int i=0; i<nodes.getLength(); i++) {
246
			Node node = nodes.item(i);
513
			Node node = nodes.item(i);
247
			values[i] = node.getTextContent();
514
			values[i] = node.getTextContent();
-
 
515
//			log.info("Facets Counts  " + values[i]);
248
 		}
516
 		}
249
		
517
		
250
		return values;
518
		return values;
251
	}
519
	}
252
}
520
}