source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/AbstractGS2TextSearch.java@ 24394

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

Through the audioDB extension we now support a form of content-based audio/music searching. These commited changes reflect this generalization in our Service inheritance hierarchy for searching. Basically, what used to be thought of as a search service implied a *text* search service.

File size: 11.0 KB
Line 
1/*
2 * AbstractGS2TextSearch.java
3 * Copyright (C) 2011 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 */
18package org.greenstone.gsdl3.service;
19
20
21// Greenstone classes
22import org.greenstone.gsdl3.util.OID;
23import org.greenstone.gsdl3.util.DBInfo;
24import org.greenstone.gsdl3.util.GSXML;
25import org.greenstone.gsdl3.util.SimpleDocumentDatabase;
26import org.greenstone.gsdl3.util.GSFile;
27
28// XML classes
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.NodeList;
32
33// java
34import java.util.Vector;
35import java.util.ArrayList;
36import java.util.HashMap;
37import java.util.Map;
38import java.util.Set;
39import java.util.Iterator;
40import java.io.File;
41
42import org.apache.log4j.*;
43
44public abstract class AbstractGS2TextSearch
45 extends AbstractTextSearch
46{
47
48 protected static final String EQUIV_TERM_ELEM = "equivTerm";
49
50 protected static final String STEM_ATT = "stem";
51 protected static final String NUM_DOCS_MATCH_ATT = "numDocsMatch";
52 protected static final String FREQ_ATT = "freq";
53
54 // Elements used in the config file that are specific to this class
55 protected static final String DEFAULT_INDEX_ELEM = "defaultIndex";
56 protected static final String INDEX_STEM_ELEM = "indexStem";
57 protected static final String INDEX_ELEM = "index";
58 protected static final String DEFAULT_INDEX_SUBCOLLECTION_ELEM = "defaultIndexSubcollection";
59 protected static final String DEFAULT_INDEX_LANGUAGE_ELEM = "defaultIndexLanguage";
60 protected static final String INDEX_SUBCOLLECTION_ELEM = "indexSubcollection";
61 protected static final String INDEX_LANGUAGE_ELEM = "indexLanguage";
62
63
64 // Some indexing options
65 protected static final String STEMINDEX_OPTION = "stemIndexes";
66 protected static final String MAXNUMERIC_OPTION = "maxnumeric";
67
68 /** the stem used for the index files */
69 protected String index_stem = null;
70
71 // stem indexes available
72 protected boolean does_case=true;
73 protected boolean does_stem=true;
74 protected boolean does_accent=false;
75
76 // maxnumeric -
77 protected int maxnumeric = 4;
78
79 SimpleDocumentDatabase gs_doc_db = null;
80
81 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2TextSearch.class.getName());
82
83
84 /** constructor */
85 public AbstractGS2TextSearch()
86 {
87
88 }
89 public void cleanUp() {
90 super.cleanUp();
91 this.gs_doc_db.cleanUp();
92 }
93
94 /** configure this service */
95 public boolean configure(Element info, Element extra_info)
96 {
97 if (!super.configure(info,extra_info)) {
98 return false;
99 }
100
101 // find out what kind of database we have
102 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
103 String database_type = null;
104 if (database_type_elem != null) {
105 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
106 }
107 if (database_type == null || database_type.equals("")) {
108 database_type = "gdbm"; // the default
109 }
110
111 // the index stem is either the collection name or is specified in the config file
112 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, INDEX_STEM_ELEM);
113 if (index_stem_elem != null) {
114 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
115 }
116 if (this.index_stem == null || this.index_stem.equals("")) {
117 logger.warn("indexStem element not found, stem will default to collection name");
118 this.index_stem = this.cluster_name;
119 }
120
121 // replaces default AbstractSearch version with one tied to database
122 gs_doc_db = new SimpleDocumentDatabase(this.doc,
123 database_type,this.site_home,
124 this.cluster_name,
125 this.index_stem);
126 if (!gs_doc_db.isValid()) {
127 logger.error("Failed to open Document Database.");
128 return false;
129 }
130 this.gs_doc = gs_doc_db;
131
132 // do we support any of the extended features?
133 does_chunking = true;
134
135 // Get the default index out of <defaultIndex> (buildConfig.xml)
136 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
137 if (def != null) {
138 this.default_index = def.getAttribute(GSXML.SHORTNAME_ATT);
139 } // otherwise will be "", and the first one will be the default
140
141 //get the default indexSubcollection out of <defaultIndexSubcollection> (buildConfig.xml)
142 Element defSub = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_SUBCOLLECTION_ELEM);
143 if (defSub != null) {
144 this.default_index_subcollection = defSub.getAttribute(GSXML.SHORTNAME_ATT);
145 }
146
147 //get the default indexLanguage out of <defaultIndexLanguage> (buildConfig.xml)
148 Element defLang = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_LANGUAGE_ELEM);
149 if (defLang != null) {
150 this.default_index_language = defLang.getAttribute(GSXML.SHORTNAME_ATT);
151 } //concate defaultIndex + defaultIndexSubcollection + defaultIndexLanguage
152
153 // get index options
154 Element index_option_list = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_OPTION_ELEM + GSXML.LIST_MODIFIER);
155 if (index_option_list != null) {
156 NodeList options = index_option_list.getElementsByTagName(GSXML.INDEX_OPTION_ELEM);
157 for (int i=0; i<options.getLength(); i++) {
158 Element opt = (Element)options.item(i);
159 String name = opt.getAttribute(GSXML.NAME_ATT);
160 String value = opt.getAttribute(GSXML.VALUE_ATT);
161 if (name.equals(MAXNUMERIC_OPTION)) {
162 int maxnum = Integer.parseInt(value);
163 if (4 <= maxnum && maxnum < 512) {
164 maxnumeric = maxnum;
165 }
166 }
167 else if (name.equals(STEMINDEX_OPTION)) {
168 int stemindex = Integer.parseInt(value);
169 // stem and case are true by default, accent folding false by default
170 if ((stemindex & 1) == 0) {
171 does_case = false;
172 }
173 if ((stemindex & 2) == 0) {
174 does_stem = false;
175 }
176 if ((stemindex & 4) != 0) {
177 does_accent = true;
178 }
179 }
180 }
181 }
182
183 // get display info from extra info
184 if (extra_info !=null) {
185 Document owner = info.getOwnerDocument();
186 // so far we have index specific display elements, and global format elements
187 NodeList indexes = info.getElementsByTagName(GSXML.INDEX_ELEM);
188 Element config_search = (Element)GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
189
190 for (int i=0; i<indexes.getLength();i++) {
191 Element ind = (Element)indexes.item(i);
192 String name = ind.getAttribute(GSXML.NAME_ATT);
193 Element node_extra = GSXML.getNamedElement(config_search,
194 GSXML.INDEX_ELEM,
195 GSXML.NAME_ATT,
196 name);
197 if (node_extra == null) {
198 logger.error("haven't found extra info for index named "+name);
199 continue;
200 }
201
202 // get the display elements if any - displayName
203 NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
204 if (display_names !=null) {
205 for (int j=0; j<display_names.getLength(); j++) {
206 Element e = (Element)display_names.item(j);
207 ind.appendChild(owner.importNode(e, true));
208 }
209 }
210 } // for each index
211 }
212 return true;
213 }
214
215 protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
216 {
217 // the index info -
218 Element index_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_ELEM+GSXML.LIST_MODIFIER);
219 NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
220 int len = indexes.getLength();
221 // now add even if there is only one
222 for (int i=0; i<len; i++) {
223 Element index = (Element)indexes.item(i);
224 String shortname = index.getAttribute(GSXML.SHORTNAME_ATT);
225 if (shortname.equals("")) {
226 continue;
227 }
228 index_ids.add(shortname);
229 String display_name = GSXML.getDisplayText(index, GSXML.DISPLAY_TEXT_NAME, lang, "en");
230 if (display_name.equals("")) {
231 display_name = index.getAttribute(GSXML.NAME_ATT);
232 if (display_name.equals("")) {
233 display_name = shortname;
234 }
235 }
236 index_names.add(display_name);
237 }
238 }
239
240 protected void getIndexSubcollectionData(ArrayList index_sub_ids, ArrayList index_sub_names, String lang)
241 {
242 // the index info -
243 Element index_sub_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_SUBCOLLECTION_ELEM+GSXML.LIST_MODIFIER);
244 NodeList index_subs = index_sub_list.getElementsByTagName(INDEX_SUBCOLLECTION_ELEM);
245 int len = index_subs.getLength();
246 // now add even if there is only one
247 for (int i=0; i<len; i++) {
248 Element indexsub = (Element)index_subs.item(i);
249 String shortname = indexsub.getAttribute(GSXML.SHORTNAME_ATT);
250 if (shortname.equals("")) {
251 continue;
252 }
253 index_sub_ids.add(shortname);
254 String display_name = GSXML.getDisplayText(indexsub, GSXML.DISPLAY_TEXT_NAME, lang, "en");
255 if (display_name.equals("")) {
256 display_name = indexsub.getAttribute(GSXML.NAME_ATT);
257 if (display_name.equals("")) {
258 display_name = shortname;
259 }
260 }
261 index_sub_names.add(display_name);
262 }
263 }
264
265 protected void getIndexLanguageData(ArrayList index_lang_ids, ArrayList index_lang_names, String lang)
266 {
267 // the index info -
268 Element index_lang_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_LANGUAGE_ELEM+GSXML.LIST_MODIFIER);
269 NodeList index_langs = index_lang_list.getElementsByTagName(INDEX_LANGUAGE_ELEM);
270 int len = index_langs.getLength();
271 // now add even if there is only one
272 for (int i=0; i<len; i++) {
273 Element indexlang = (Element)index_langs.item(i);
274 String shortname = indexlang.getAttribute(GSXML.SHORTNAME_ATT);
275 if (shortname.equals("")) {
276 continue;
277 }
278 index_lang_ids.add(shortname);
279 String display_name = GSXML.getDisplayText(indexlang, GSXML.DISPLAY_TEXT_NAME, lang, "en");
280 if (display_name.equals("")) {
281 display_name = indexlang.getAttribute(GSXML.NAME_ATT);
282 if (display_name.equals("")) {
283 display_name = shortname;
284 }
285 }
286 index_lang_names.add(display_name);
287 }
288
289
290 }
291
292
293 protected void addCustomQueryParams(Element param_list, String lang)
294 {
295 if (this.does_case){
296 // gs2 has case on by default
297 createParameter(CASE_PARAM, param_list, lang, BOOLEAN_PARAM_ON);
298 }
299 if (this.does_stem){
300 // but stem is off by default
301 createParameter(STEM_PARAM, param_list, lang, BOOLEAN_PARAM_OFF);
302 }
303 if (this.does_accent){
304 // and so is accent folding
305 createParameter(ACCENT_PARAM, param_list, lang, BOOLEAN_PARAM_OFF);
306 }
307 createParameter(MATCH_PARAM, param_list, lang);
308 }
309
310
311 /** convert indexer internal id to Greenstone oid */
312 protected String internalNum2OID(long docnum)
313 {
314 return this.gs_doc_db.internalNum2OID(docnum);
315 }
316
317 protected String internalNum2OID(String docnum)
318 {
319 return this.gs_doc_db.internalNum2OID(docnum);
320 }
321
322}
323
324
Note: See TracBrowser for help on using the repository browser.