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

Last change on this file since 29541 was 29541, checked in by kjdon, 9 years ago

changed sort order param to reverse sort param. matches better what lucene/solr actually do, and removes the issue of needing a different default sort order when sorting by rank vs other field.

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