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

Last change on this file was 38154, checked in by kjdon, 7 months ago

moved sidx and didx to static strings

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