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

Last change on this file since 24739 was 24739, checked in by davidb, 13 years ago

Shifted to using LuceneWrapper3

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