source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2LuceneSearch.java@ 25885

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

Removed the Solr code from the main greenstone code

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/*
2 * GS2LuceneSearch.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.ArrayList;
24import java.util.HashMap;
25import java.util.Iterator;
26import java.util.Map;
27import java.util.Set;
28import java.util.Vector;
29
30import org.apache.log4j.Logger;
31import org.greenstone.LuceneWrapper3.GS2LuceneQuery;
32import org.greenstone.LuceneWrapper3.LuceneQueryResult;
33import org.greenstone.gsdl3.util.FacetWrapper;
34import org.greenstone.gsdl3.util.GSFile;
35import org.greenstone.gsdl3.util.GSXML;
36import org.w3c.dom.Element;
37
38public class GS2LuceneSearch extends SharedSoleneGS2FieldSearch
39{
40 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2LuceneSearch.class.getName());
41
42 private GS2LuceneQuery lucene_src = null;
43
44 public GS2LuceneSearch()
45 {
46 this.lucene_src = new GS2LuceneQuery();
47 }
48
49 public void cleanUp()
50 {
51 super.cleanUp();
52 this.lucene_src.cleanUp();
53 }
54
55 /** methods to handle actually doing the query */
56
57 /** do any initialisation of the query object */
58 protected boolean setUpQueryer(HashMap params)
59 {
60 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + "index" + File.separatorChar;
61
62 String index = "didx";
63 String physical_index_language_name = null;
64 String physical_sub_index_name = null;
65 int maxdocs = 100;
66 int hits_per_page = 20;
67 int start_page = 1;
68 // set up the query params
69 Set entries = params.entrySet();
70 Iterator i = entries.iterator();
71 while (i.hasNext())
72 {
73 Map.Entry m = (Map.Entry) i.next();
74 String name = (String) m.getKey();
75 String value = (String) m.getValue();
76
77 if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
78 {
79 maxdocs = Integer.parseInt(value);
80 }
81 else if (name.equals(HITS_PER_PAGE_PARAM))
82 {
83 hits_per_page = Integer.parseInt(value);
84 }
85 else if (name.equals(START_PAGE_PARAM))
86 {
87 start_page = Integer.parseInt(value);
88
89 }
90 else if (name.equals(MATCH_PARAM))
91 {
92 if (value.equals(MATCH_PARAM_ALL))
93 {
94 this.lucene_src.setDefaultConjunctionOperator("AND");
95 }
96 else
97 {
98 this.lucene_src.setDefaultConjunctionOperator("OR");
99 }
100 }
101 else if (name.equals(RANK_PARAM))
102 {
103 if (value.equals(RANK_PARAM_RANK_VALUE))
104 {
105 value = null;
106 }
107 this.lucene_src.setSortField(value);
108 }
109 else if (name.equals(LEVEL_PARAM))
110 {
111 if (value.toUpperCase().equals("SEC"))
112 {
113 index = "sidx";
114 }
115 else
116 {
117 index = "didx";
118 }
119 }
120 else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
121 {
122 physical_sub_index_name = value;
123 }
124 else if (name.equals(INDEX_LANGUAGE_PARAM))
125 {
126 physical_index_language_name = value;
127 } // ignore any others
128 }
129 // set up start and end results if necessary
130 int start_results = 1;
131 if (start_page != 1)
132 {
133 start_results = ((start_page - 1) * hits_per_page) + 1;
134 }
135 int end_results = hits_per_page * start_page;
136 this.lucene_src.setStartResults(start_results);
137 this.lucene_src.setEndResults(end_results);
138
139 if (index.equals("sidx") || index.equals("didx"))
140 {
141 if (physical_sub_index_name != null)
142 {
143 index += physical_sub_index_name;
144 }
145 if (physical_index_language_name != null)
146 {
147 index += physical_index_language_name;
148 }
149 }
150
151 this.lucene_src.setIndexDir(indexdir + index);
152 this.lucene_src.initialise();
153 return true;
154 }
155
156 /** do the query */
157 protected Object runQuery(String query)
158 {
159 try
160 {
161 LuceneQueryResult lqr = this.lucene_src.runQuery(query);
162 return lqr;
163 }
164 catch (Exception e)
165 {
166 logger.error("Exception happened in runQuery(): ", e);
167 }
168
169 return null;
170 }
171
172 /** get the total number of docs that match */
173 protected long numDocsMatched(Object query_result)
174 {
175 return ((LuceneQueryResult) query_result).getTotalDocs();
176 }
177
178 /** get the list of doc ids */
179 protected String[] getDocIDs(Object query_result)
180 {
181 Vector docs = ((LuceneQueryResult) query_result).getDocs();
182 String[] doc_nums = new String[docs.size()];
183 for (int d = 0; d < docs.size(); d++)
184 {
185 String doc_num = ((LuceneQueryResult.DocInfo) docs.elementAt(d)).id_;
186 doc_nums[d] = doc_num;
187 }
188 return doc_nums;
189 }
190
191 /** get the list of doc ranks */
192 protected String[] getDocRanks(Object query_result)
193 {
194 Vector docs = ((LuceneQueryResult) query_result).getDocs();
195 String[] doc_ranks = new String[docs.size()];
196 for (int d = 0; d < docs.size(); d++)
197 {
198 doc_ranks[d] = Float.toString(((LuceneQueryResult.DocInfo) docs.elementAt(d)).rank_);
199 }
200 return doc_ranks;
201 }
202
203 /** add in term info if available */
204 protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
205 {
206 String query_level = (String) params.get(LEVEL_PARAM); // the current query level
207
208 Vector terms = ((LuceneQueryResult) query_result).getTerms();
209 for (int t = 0; t < terms.size(); t++)
210 {
211 LuceneQueryResult.TermInfo term_info = (LuceneQueryResult.TermInfo) terms.get(t);
212
213 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
214 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
215 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
216 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
217 term_elem.setAttribute(FIELD_ATT, term_info.field_);
218 term_list.appendChild(term_elem);
219 }
220
221 Vector stopwords = ((LuceneQueryResult) query_result).getStopWords();
222 for (int t = 0; t < stopwords.size(); t++)
223 {
224 String stopword = (String) stopwords.get(t);
225
226 Element stopword_elem = this.doc.createElement(GSXML.STOPWORD_ELEM);
227 stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
228 term_list.appendChild(stopword_elem);
229 }
230
231 return true;
232 }
233
234 protected ArrayList<FacetWrapper> getFacets(Object query_result)
235 {
236 return null;
237 }
238}
Note: See TracBrowser for help on using the repository browser.