source: gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/service/GS2SolrSearch.java@ 25862

Last change on this file since 25862 was 25862, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

  • Property svn:executable set to *
File size: 8.6 KB
Line 
1/*
2 * GS2SolrSearch.java
3 * Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import java.io.File;
23import java.util.HashMap;
24import java.util.Iterator;
25import java.util.Map;
26import java.util.Set;
27import java.util.Vector;
28
29import org.apache.log4j.Logger;
30import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
31import org.apache.solr.core.CoreContainer;
32import org.greenstone.LuceneWrapper3.SharedSoleneQueryResult;
33import org.greenstone.gsdl3.util.GSFile;
34import org.greenstone.gsdl3.util.GSXML;
35import org.greenstone.gsdl3.util.SolrQueryWrapper;
36import org.greenstone.util.GlobalProperties;
37import org.w3c.dom.Element;
38
39public class GS2SolrSearch extends SharedSoleneGS2FieldSearch
40{
41 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2SolrSearch.class.getName());
42
43 static protected CoreContainer all_solr_cores = null;
44
45 protected HashMap solr_core_cache;
46 protected SolrQueryWrapper solr_src = null;
47
48 public GS2SolrSearch()
49 {
50 // Used to store the solr cores that match the required 'level'
51 // of search (e.g. either document-level=>didx, or
52 // section-level=>sidx. The hashmap is filled out on demand
53 // based on 'level' parameter passed in to 'setUpQueryer()'
54
55 solr_core_cache = new HashMap();
56
57 if (all_solr_cores == null)
58 {
59 // Share one CoreContainer across all sties/collections
60 try
61 {
62
63 String gsdl3_home = GlobalProperties.getGSDL3Home();
64 String solr_ext_name = GlobalProperties.getProperty("gsdlext.solr.dirname", "solr");
65
66 String solr_home_str = GSFile.extHome(gsdl3_home, solr_ext_name);
67 File solr_home = new File(solr_home_str);
68 File solr_xml = new File(solr_home, "solr.xml");
69
70 all_solr_cores = new CoreContainer(solr_home_str, solr_xml);
71 }
72 catch (Exception e)
73 {
74 e.printStackTrace();
75 }
76 }
77
78 this.solr_src = new SolrQueryWrapper();
79 }
80
81 public void cleanUp()
82 {
83 super.cleanUp();
84 this.solr_src.cleanUp();
85 all_solr_cores.shutdown();
86 }
87
88 /** methods to handle actually doing the query */
89
90 /** do any initialisation of the query object */
91 protected boolean setUpQueryer(HashMap params)
92 {
93 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + "index" + File.separatorChar;
94
95 String index = "didx";
96 String physical_index_language_name = null;
97 String physical_sub_index_name = null;
98 int maxdocs = 100;
99 int hits_per_page = 20;
100 int start_page = 1;
101 // set up the query params
102 Set entries = params.entrySet();
103 Iterator i = entries.iterator();
104 while (i.hasNext())
105 {
106 Map.Entry m = (Map.Entry) i.next();
107 String name = (String) m.getKey();
108 String value = (String) m.getValue();
109
110 if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
111 {
112 maxdocs = Integer.parseInt(value);
113 }
114 else if (name.equals(HITS_PER_PAGE_PARAM))
115 {
116 hits_per_page = Integer.parseInt(value);
117 }
118 else if (name.equals(START_PAGE_PARAM))
119 {
120 start_page = Integer.parseInt(value);
121
122 }
123 else if (name.equals(MATCH_PARAM))
124 {
125 if (value.equals(MATCH_PARAM_ALL))
126 {
127 this.solr_src.setDefaultConjunctionOperator("AND");
128 }
129 else
130 {
131 this.solr_src.setDefaultConjunctionOperator("OR");
132 }
133 }
134 else if (name.equals(RANK_PARAM))
135 {
136 if (value.equals(RANK_PARAM_RANK_VALUE))
137 {
138 value = null;
139 }
140 this.solr_src.setSortField(value);
141 }
142 else if (name.equals(LEVEL_PARAM))
143 {
144 if (value.toUpperCase().equals("SEC"))
145 {
146 index = "sidx";
147 }
148 else
149 {
150 index = "didx";
151 }
152 }
153 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
154 {
155 physical_sub_index_name = value;
156 }
157 else if (name.equals(INDEX_LANGUAGE_PARAM))
158 {
159 physical_index_language_name = value;
160 } // ignore any others
161 }
162 // set up start and end results if necessary
163 int start_results = 1;
164 if (start_page != 1)
165 {
166 start_results = ((start_page - 1) * hits_per_page) + 1;
167 }
168 int end_results = hits_per_page * start_page;
169 this.solr_src.setStartResults(start_results);
170 this.solr_src.setEndResults(end_results);
171 this.solr_src.setMaxDocs(maxdocs);
172
173 if (index.equals("sidx") || index.equals("didx"))
174 {
175 if (physical_sub_index_name != null)
176 {
177 index += physical_sub_index_name;
178 }
179 if (physical_index_language_name != null)
180 {
181 index += physical_index_language_name;
182 }
183 }
184
185 // now we know the index level, we can dig out the required
186 // solr-core, (caching the result in 'solr_core_cache')
187
188 String site_name = this.router.getSiteName();
189 String coll_name = this.cluster_name;
190
191 String core_name = site_name + "-" + coll_name + "-" + index;
192
193 EmbeddedSolrServer solr_core = null;
194
195 if (!solr_core_cache.containsKey(core_name))
196 {
197 solr_core = new EmbeddedSolrServer(all_solr_cores, core_name);
198
199 solr_core_cache.put(core_name, solr_core);
200 }
201 else
202 {
203 solr_core = (EmbeddedSolrServer) solr_core_cache.get(core_name);
204 }
205
206 this.solr_src.setSolrCore(solr_core);
207 this.solr_src.initialise();
208 return true;
209 }
210
211 /** do the query */
212 protected Object runQuery(String query)
213 {
214
215 /*
216 * ModifiableSolrParams solrParams = new ModifiableSolrParams();
217 * solrParams.set("collectionName", myCollection);
218 * solrParams.set("username", "admin"); solrParams.set("password",
219 * "password"); solrParams.set("facet", facet); solrParams.set("q",
220 * query); solrParams.set("start", start); solrParams.set("rows",
221 * nbDocuments); return server.query(solrParams);
222 */
223
224 /*
225 * SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(query);
226 * //solrQuery.set("collectionName", myCollection);
227 * solrQuery.set("username", "admin"); solrQuery.set("password",
228 * "password"); solrQuery.set("facet", facet);
229 * solrQuery.setStart(start); solrQuery.setRows(nbDocuments); //return
230 * server.query(solrQuery);
231 */
232
233 try
234 {
235 SharedSoleneQueryResult sqr = this.solr_src.runQuery(query);
236 return sqr;
237 }
238 catch (Exception e)
239 {
240 logger.error("Exception happened in run query: ", e);
241 }
242
243 return null;
244 }
245
246 /** get the total number of docs that match */
247 protected long numDocsMatched(Object query_result)
248 {
249 return ((SharedSoleneQueryResult) query_result).getTotalDocs();
250
251 }
252
253 /** get the list of doc ids */
254 protected String[] getDocIDs(Object query_result)
255 {
256 Vector docs = ((SharedSoleneQueryResult) query_result).getDocs();
257 String[] doc_nums = new String[docs.size()];
258 for (int d = 0; d < docs.size(); d++)
259 {
260 String doc_num = ((SharedSoleneQueryResult.DocInfo) docs.elementAt(d)).id_;
261 doc_nums[d] = doc_num;
262 }
263 return doc_nums;
264 }
265
266 /** get the list of doc ranks */
267 protected String[] getDocRanks(Object query_result)
268 {
269 Vector docs = ((SharedSoleneQueryResult) query_result).getDocs();
270 String[] doc_ranks = new String[docs.size()];
271 for (int d = 0; d < docs.size(); d++)
272 {
273 doc_ranks[d] = Float.toString(((SharedSoleneQueryResult.DocInfo) docs.elementAt(d)).rank_);
274 }
275 return doc_ranks;
276 }
277
278 /** add in term info if available */
279 protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
280 {
281 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
282
283 Vector terms = ((SharedSoleneQueryResult) query_result).getTerms();
284 for (int t = 0; t < terms.size(); t++)
285 {
286 SharedSoleneQueryResult.TermInfo term_info = (SharedSoleneQueryResult.TermInfo) terms.get(t);
287
288 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
289 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
290 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
291 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
292 term_elem.setAttribute(FIELD_ATT, term_info.field_);
293 term_list.appendChild(term_elem);
294 }
295
296 Vector stopwords = ((SharedSoleneQueryResult) query_result).getStopWords();
297 for (int t = 0; t < stopwords.size(); t++)
298 {
299 String stopword = (String) stopwords.get(t);
300
301 Element stopword_elem = this.doc.createElement(GSXML.STOPWORD_ELEM);
302 stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
303 term_list.appendChild(stopword_elem);
304 }
305
306 return true;
307 }
308
309}
Note: See TracBrowser for help on using the repository browser.