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

Last change on this file since 28181 was 28181, checked in by kjdon, 11 years ago

making search param defaults able to be set in config file. uses <paramDefault name=xx value=yy> element. Now all defaults are set in paramDefaults HashMap instead of individual variables. have left index etc ones for now as they are more complicated.

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