Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
317 ashish 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.services;
5
 
6
 
7
import javax.xml.xpath.XPath;
8
import javax.xml.xpath.XPathConstants;
9
import javax.xml.xpath.XPathExpressionException;
10
import javax.xml.xpath.XPathFactory;
11
 
12
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
14
import org.w3c.dom.Node;
15
import org.w3c.dom.NodeList;
16
import org.xml.sax.InputSource;
17
 
18
/**
19
 * @author naveen
20
 *
21
 */
22
public class SolrSearchService {
23
	/**
24
	 * 
25
	 */
26
	private static Log log = LogFactory.getLog(SolrSearchService.class);
27
 
28
	/**
29
	 * 
30
	 */
31
	public static String SOLR_URL = "http://localhost:8983/solr/select/";
32
 
33
	/**
34
	 * 
35
	 */
36
	private XPath xpath;
37
 
38
	/**
39
	 * 
40
	 */
41
	private InputSource inputSource;
42
 
43
	/**
44
	 * 
45
	 * @param query
46
	 * @param facetDefinitionIDs
47
	 */
48
	public SolrSearchService(String query, String[] facetqueries, 
49
			long[] facetDefinitionIDs) {
50
		this.xpath = XPathFactory.newInstance().newXPath();
51
 
52
		String uri = SOLR_URL + "?wt=xml&q=" + query;
53
 
54
		if(facetqueries != null) {
55
			for(int i=0; i<facetqueries.length; i++) {
56
				uri += "&fq=" + facetqueries[i];
57
			}
58
		}
59
 
60
		uri += "&fl=ID,Name&facet=true&rows=20";
61
		uri += "&facet.field=Category";
62
 
63
		for(int i=0; i<facetDefinitionIDs.length; i++) {
64
			uri += "&facet.field=" + "F_" + facetDefinitionIDs[i];
65
		}
66
		log.info("uri=" + uri);
67
 
68
		this.inputSource = new InputSource(uri);
69
	}
70
 
71
	/**
72
	 * 
73
	 * @return
74
	 */
75
	public long[] getResultEntityIDs() {
76
		String expression = "/response/result/doc/long";
77
 
78
		NodeList nodes = null;
79
		try {
80
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
81
					XPathConstants.NODESET);
82
		} 
83
		catch(XPathExpressionException xpee) {
84
			return null;
85
		}
86
 
87
		if(nodes.getLength() == 0) {
88
			return null;
89
		}
90
 
91
		long[] values = new long[nodes.getLength()];
92
		for(int i=0; i<nodes.getLength(); i++) {
93
			Node node = nodes.item(i);
94
			String value = node.getTextContent();
95
			values[i] = Long.parseLong(value);
96
 		}
97
 
98
		return values;
99
	}
100
 
101
	/**
102
	 * 
103
	 * @return
104
	 */
105
	public String[] getResultCategoryNames() {
106
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
107
		expression += "lst[@name = 'Category']/int/@name";
108
 
109
		NodeList nodes = null;
110
		try {
111
			nodes = (NodeList) this.xpath.evaluate(expression, 
112
				this.inputSource, XPathConstants.NODESET);
113
		}
114
		catch (XPathExpressionException xpee) {
115
			return null;
116
		}
117
 
118
		if(nodes.getLength() == 0) {
119
			return null;
120
		}
121
 
122
		String[] values = new String[nodes.getLength()];
123
		for(int i=0; i<nodes.getLength(); i++) {
124
			Node node = nodes.item(i);
125
			values[i] = node.getTextContent();
126
 		}
127
 
128
		return values;
129
	}
130
 
131
	/**
132
	 * 
133
	 * @return
134
	 */
135
	public int[] getResultCategoryCounts() {
136
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
137
		expression += "lst[@name = 'Category']/int";
138
 
139
		NodeList nodes = null;
140
		try {
141
			nodes = (NodeList) this.xpath.evaluate(expression, 
142
				this.inputSource, XPathConstants.NODESET);
143
		}
144
		catch (XPathExpressionException xpee) {
145
			return null;
146
		}
147
 
148
		if(nodes.getLength() == 0) {
149
			return null;
150
		}
151
 
152
		int[] values = new int[nodes.getLength()];
153
		for(int i=0; i<nodes.getLength(); i++) {
154
			Node node = nodes.item(i);
155
			values[i] = Integer.parseInt(node.getTextContent());
156
 		}
157
 
158
		return values;
159
	}
160
 
161
	/**
162
	 * 
163
	 * @return
164
	 */
165
	public String[]  getResultEntityNames() {
166
		String expression = "/response/result/doc/str";
167
 
168
		NodeList nodes = null;
169
		try {
170
			nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,
171
					XPathConstants.NODESET);
172
		} 
173
		catch(XPathExpressionException xpee) {
174
			return null;
175
		}
176
 
177
		if(nodes.getLength() == 0) {
178
			return null;
179
		}
180
 
181
		String[] values = new String[nodes.getLength()];
182
		for(int i=0; i<nodes.getLength(); i++) {
183
			Node node = nodes.item(i);
184
			String value = node.getTextContent();
185
			values[i] = value;
186
 		}
187
 
188
		return values;
189
	}
190
 
191
	/**
192
	 * 
193
	 * @param facetDefinitionID
194
	 * @return
195
	 */
196
	public String[] getFacetValues(long facetDefinitionID) {
197
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
198
		expression += "lst[@name = 'F_"+ facetDefinitionID +"']/int/@name";
199
 
200
		NodeList nodes = null;
201
		try {
202
			nodes = (NodeList) this.xpath.evaluate(expression, 
203
				this.inputSource, XPathConstants.NODESET);
204
		}
205
		catch (XPathExpressionException xpee) {
206
			return null;
207
		}
208
 
209
		if(nodes.getLength() == 0) {
210
			return null;
211
		}
212
 
213
		String[] values = new String[nodes.getLength()];
214
		for(int i=0; i<nodes.getLength(); i++) {
215
			Node node = nodes.item(i);
216
			values[i] = node.getTextContent();
217
 		}
218
 
219
		return values;
220
	}
221
 
222
	/**
223
	 * 
224
	 * @param facetDefinitionID
225
	 * @return
226
	 */
227
	public String[] getFacetCounts(long facetDefinitionID) {
228
		String expression = "/response/lst/lst[@name = 'facet_fields']/";
229
		expression += "lst[@name = 'F_" + facetDefinitionID + "']/int";
230
 
231
		NodeList nodes = null;
232
		try {
233
			nodes = (NodeList) this.xpath.evaluate(expression, 
234
				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
		String[] values = new String[nodes.getLength()];
245
		for(int i=0; i<nodes.getLength(); i++) {
246
			Node node = nodes.item(i);
247
			values[i] = node.getTextContent();
248
 		}
249
 
250
		return values;
251
	}
252
}