source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/SharedSoleneGS2FieldSearch.java@ 33373

Last change on this file since 33373 was 33373, checked in by kjdon, 5 years ago

need to check for null result from getTextString - otherwise get a nullpointerexception, and you end up not being able to parse the XML past that point.

  • Property svn:executable set to *
File size: 7.7 KB
Line 
1/*
2 * SharedSoleneGS2FieldSearch.java -- shared base code for Solr and Lucene
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.util.ArrayList;
23
24import org.apache.log4j.Logger;
25import org.greenstone.LuceneWrapper4.SharedSoleneQuery;
26import org.greenstone.gsdl3.util.GSXML;
27import org.greenstone.gsdl3.util.XMLConverter;
28
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.NodeList;
32
33// Shared code for Solr and Lucene GS2FieldSearch
34
35public abstract class SharedSoleneGS2FieldSearch extends AbstractGS2FieldSearch
36{
37
38 protected static String RANK_PARAM_RANK = "rank";
39 protected static String RANK_PARAM_NONE = "none";
40
41 protected static final String SORT_ELEM = "sort";
42 protected static final String DEFAULT_SORT_ELEM = "defaultSort";
43
44 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SharedSoleneGS2FieldSearch.class.getName());
45
46 // Neither lucene nor solr appear to use solene_src. GS2LuceneQuery has its own object, lucene_src
47 protected SharedSoleneQuery solene_src = null;
48 protected String default_sort = "";
49
50 public SharedSoleneGS2FieldSearch()
51 {
52 does_paging = true;
53 does_chunking = false; // actually it can but we don't want to do a max docs, as we are already paging results
54
55 // Lucene/Solr uses double operators, not single
56 //AND_OPERATOR = "&&";
57 //OR_OPERATOR = "||";
58 AND_OPERATOR = "AND";
59 OR_OPERATOR = "OR";
60 NOT_OPERATOR = "NOT";
61
62 }
63
64 public void cleanUp()
65 {
66 super.cleanUp();
67
68 if(this.solene_src != null) {
69 this.solene_src.cleanUp();
70 }
71 }
72
73 /** configure this service */
74 public boolean configure(Element info, Element extra_info)
75 {
76 if (!super.configure(info, extra_info))
77 {
78 return false;
79 }
80
81 // the search element
82 Element config_search = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
83 Document owner = info.getOwnerDocument();
84 // find the sort fields in serviceRack xml, and add in the deisplayItems if any
85 NodeList sort_nodes = info.getElementsByTagName(SORT_ELEM);
86
87 for (int i = 0; i < sort_nodes.getLength(); i++)
88 {
89 Element sort = (Element) sort_nodes.item(i);
90 String name = sort.getAttribute(GSXML.NAME_ATT);
91 Element node_extra = GSXML.getNamedElement(config_search, SORT_ELEM, GSXML.NAME_ATT, name);
92 if (node_extra == null)
93 {
94 logger.error("haven't found extra info for sort field named " + name);
95 continue;
96 }
97
98 // get the display elements if any - displayName
99 NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
100 if (display_names != null)
101 {
102 for (int j = 0; j < display_names.getLength(); j++)
103 {
104 Element e = (Element) display_names.item(j);
105 sort.appendChild(owner.importNode(e, true));
106 }
107 }
108 } // for each sortfield
109
110 // get the default sort field
111 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_SORT_ELEM);
112 if (def != null)
113 {
114 this.default_sort = def.getAttribute(GSXML.SHORTNAME_ATT);
115 }
116
117 // Lucene/Solr doesn't do case folding or stemming or accent folding at the
118 // moment
119 does_case = false;
120 does_stem = false;
121 does_accent = false;
122
123 return true;
124 }
125
126 /** add in the Lucene/Solr specific params to TextQuery */
127 protected void addCustomQueryParams(Element param_list, String lang)
128 {
129 super.addCustomQueryParams(param_list, lang);
130 /** Lucene's/Solr's rank (sort) param is based on sort fields, not ranked/not */
131 createParameter(RANK_PARAM, param_list, lang);
132 }
133 /** add in Lucene/SOLR specific params for AdvancedFieldQuery */
134 protected void addCustomQueryParamsAdvField(Element param_list, String lang)
135 {
136 super.addCustomQueryParamsAdvField(param_list, lang);
137 createParameter(RANK_PARAM, param_list, lang);
138
139 }
140
141 /** create a param and add to the list */
142 /** we override this to do a special rank param */
143 protected void createParameter(String name, Element param_list, String lang)
144 {
145 Document doc = param_list.getOwnerDocument();
146 Element param = null;
147 String param_default = paramDefaults.get(name);
148 if (name.equals(RANK_PARAM))
149 {
150 // get the fields
151 ArrayList<String> fields = new ArrayList<String>();
152 ArrayList<String> field_names = new ArrayList<String>();
153 param_default = default_sort;
154 if (!getSortData(fields, field_names, lang)) {
155 fields.add(RANK_PARAM_RANK);
156 fields.add(RANK_PARAM_NONE);
157 field_names.add(getTextString("param." + RANK_PARAM + "." + RANK_PARAM_RANK, lang));
158 field_names.add(getTextString("param." + RANK_PARAM + "." + RANK_PARAM_NONE, lang));
159 param_default = RANK_PARAM_RANK;
160 }
161 else {
162 if (param_default == null) {
163 param_default = fields.get(0);
164 }
165 }
166 param = GSXML.createParameterDescription2(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, fields, field_names);
167
168 }
169
170 if (param != null)
171 {
172 param_list.appendChild(param);
173 }
174 else
175 {
176 super.createParameter(name, param_list, lang);
177 }
178
179 }
180
181 protected boolean getSortData(ArrayList<String> sort_ids, ArrayList<String> sort_names, String lang) {
182
183 Element sort_list = (Element) GSXML.getChildByTagName(this.config_info, SORT_ELEM + GSXML.LIST_MODIFIER);
184 if (sort_list == null) return false;
185 NodeList sorts = sort_list.getElementsByTagName(SORT_ELEM);
186 int len = sorts.getLength();
187 if (len == 0) return false;
188 for (int i = 0; i < len; i++)
189 {
190 Element sort = (Element) sorts.item(i);
191 String shortname = sort.getAttribute(GSXML.SHORTNAME_ATT);
192 sort_ids.add(shortname);
193 String display_name = getDisplayText(sort, GSXML.DISPLAY_TEXT_NAME, lang, "en", "metadata_names");
194 if (display_name.equals(""))
195 {
196 // use the sort name and look up in dictionary
197 display_name = sort.getAttribute(GSXML.NAME_ATT);
198 if (display_name.equals(""))
199 {
200 display_name = shortname;
201 }
202 else {
203 String dict_name = getTextString("param.sortBy." + display_name, lang);
204 if (dict_name != null) {
205 display_name = dict_name;
206 }
207 }
208 }
209 sort_names.add(display_name);
210
211 }
212 return true;
213 }
214
215 protected String addFieldInfo(String query, String field)
216 {
217 // currently, allfields (ZZ) is stored as a extra field for Lucene
218 if (field.equals(""))
219 { // || field.equals("ZZ")) {
220 return query;
221 }
222 return field + ":(" + query + ")";
223 }
224
225 protected void addQueryElem(StringBuffer s, String q, String f, String c)
226 {
227
228 String combine = "";
229 if (s.length() > 0)
230 {
231 combine = " " + c + " ";
232 }
233 s.append(combine + addFieldInfo(q, f));
234 }
235
236 /** Lucene/Solr doesn't use these options at the moment */
237 protected String addStemOptions(String query, String stem, String casef, String accent)
238 {
239 return query;
240 }
241
242 /**
243 * Lucene/Solr does not use internal ids. It just uses hash ids. So we need
244 * to override these methods so no conversion is done.
245 */
246 /** convert indexer internal id to Greenstone oid */
247 protected String internalNum2OID(long docnum)
248 {
249 return Long.toString(docnum);
250 }
251
252 protected String internalNum2OID(String docnum)
253 {
254 return docnum;
255
256 }
257}
Note: See TracBrowser for help on using the repository browser.