source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/AbstractSearch.java@ 32419

Last change on this file since 32419 was 30049, checked in by Georgiy Litvinov, 9 years ago

While using Solr field highlighted by Solr Servlet. Also added snippets to search results while using Solr.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 KB
RevLine 
[9002]1/*
2 * AbstractSearch.java
3 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
[25764]22import java.util.ArrayList;
[28181]23import java.util.HashMap;
[25764]24
25import org.apache.log4j.Logger;
[26042]26import org.greenstone.gsdl3.util.AbstractBasicDocument;
27import org.greenstone.gsdl3.util.BasicDocument;
[25764]28import org.greenstone.gsdl3.util.GSPath;
[9002]29import org.greenstone.gsdl3.util.GSXML;
[29558]30import org.greenstone.gsdl3.util.XMLConverter;
[28966]31import org.w3c.dom.Document;
[24858]32import org.w3c.dom.Element;
[9002]33import org.w3c.dom.NodeList;
34
[24858]35/**
36 * Partially implements a generic search service
37 *
[9002]38 */
39
[24858]40public abstract class AbstractSearch extends ServiceRack
[9002]41{
[13124]42
[24858]43 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractSearch.class.getName());
[13124]44
[24858]45 // the search service
46 protected String QUERY_SERVICE = null; // set by subclass
[9002]47
[24858]48 // compulsory params
49 protected static final String INDEX_PARAM = "index";
50 protected static final String QUERY_PARAM = "query";
[24892]51 protected static final String RAW_PARAM = "rawquery";
[9002]52
[24858]53 // optional standard params - some of these have to be implemented
54 protected static final String MAXDOCS_PARAM = "maxDocs";
55 protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage";
56 protected static final String START_PAGE_PARAM = "startPage";
[13998]57
[26042]58 protected AbstractBasicDocument gs_doc = null;
[9002]59
[24858]60 /** can more than one index be searched at the same time? */
61 protected boolean does_multi_index_search = false;
62 /** does this service support paging of results? */
63 protected boolean does_paging = false;
64 /** does this service support asking for a subset of results? */
65 protected boolean does_chunking = false;
[25885]66 /** does this service support faceting search results */
67 protected boolean does_faceting = false;
[30049]68 /** does this service support highlighting snippets results */
69 protected boolean does_highlight_snippets = false;
70 protected boolean does_full_field_highlighting = false;
[24858]71 /**
72 * the default document type - use if all documents are the same type
73 */
74 protected String default_document_type = null;
75 /**
76 * the default index, or comma separated list if more than one is the
77 * default (with start and end commas, eg ,TI,SU,). Should be set by
78 * configure()
79 */
80 protected String default_index = "";
[9002]81
[29558]82 protected Element service_metadata_list = null;
[28181]83 protected HashMap<String, String> paramDefaults = null;
[24858]84
85 public AbstractSearch()
86 {
[28181]87 paramDefaults = new HashMap<String, String>();
[10093]88 }
89
[24858]90 /**
91 * Sets up the short service info for service by QUERY_SERVICE (e.g.
92 * TextQuery or AudioQuery) If other services will be provided, should be
93 * added in the subclass configure also looks for search format info, and
94 * document format info
95 */
96 public boolean configure(Element info, Element extra_info)
97 {
98 if (!super.configure(info, extra_info))
99 {
100 return false;
101 }
[9002]102
[24858]103 logger.info("Configuring AbstractSearch...");
[24394]104
[24858]105 this.config_info = info;
[9002]106
[24858]107 // set up short_service_info_
108 // => for now just has id and type. the name (lang dependent)
109 // will be added in if the list is requested.
[24394]110
[28966]111 Element tq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
[24858]112 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
113 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
114 this.short_service_info.appendChild(tq_service);
[13998]115
[29558]116 // load up paging defaults
117 if (does_chunking) {
118 paramDefaults.put(MAXDOCS_PARAM, "100");
119 }
120 if (does_paging) {
121 paramDefaults.put(HITS_PER_PAGE_PARAM, "20");
122 paramDefaults.put(START_PAGE_PARAM, "1");
123 }
124
[28181]125 // load up any search param defaults
126 Element search_elem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
127 if (search_elem != null) {
128 getSearchParamDefaults(search_elem);
129 }
[24858]130 // add some format info to service map if there is any
131 // => lookin extra info first look in buildConfig
[14183]132
[24858]133 Element format = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
[24394]134
[24858]135 if (format == null)
136 {
[28181]137 // try to find a format element inside <search> that contains a gsf:template. Note what if we have only xsl:templates??
138
[24858]139 NodeList format_elems = null;
140 if (search_elem != null)
141 {
142 format_elems = search_elem.getElementsByTagName(GSXML.FORMAT_ELEM);
143 }
144 for (int i = 0; i < format_elems.getLength(); i++)
145 {
146 format = (Element) format_elems.item(i);
147 if (format.getElementsByTagName("gsf:template").getLength() != 0)
148 {
149 break;
150 }
151 }
152 }//end of if(format==null)
153 //
154 if (format != null)
155 {
[28966]156 this.format_info_map.put(QUERY_SERVICE, this.desc_doc.importNode(format, true));
[24394]157 }
[24858]158
159 // look for document display format - for documentType
160 String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
161 Element display_format = (Element) GSXML.getNodeByPath(extra_info, path);
162 if (display_format != null)
163 {
164 // check for docType option.
165 Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType");
166 if (doc_type_opt != null)
167 {
168 String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT);
169 if (!value.equals(""))
170 {
171 this.default_document_type = value;
172 }
173 }
[9002]174 }
[24858]175
176 // Base line for document (might be overriden by sub-classes)
[28966]177 gs_doc = new BasicDocument(this.default_document_type);
[24858]178
179 return true;
[9002]180 }
181
[28181]182 protected void getSearchParamDefaults(Element search_elem) {
183
184 NodeList param_defaults_list = GSXML.getChildrenByTagName(search_elem, GSXML.PARAM_DEFAULT_ELEM);
185 for (int i=0; i<param_defaults_list.getLength(); i++) {
186 Element paramdef = (Element)param_defaults_list.item(i);
187 String name = paramdef.getAttribute(GSXML.NAME_ATT);
188 String val = paramdef.getAttribute(GSXML.VALUE_ATT);
189 if (!name.equals("") && !val.equals("")) {
190 paramDefaults.put(name, val);
191 }
192 }
193 }
[24858]194 /**
195 * returns a basic description for QUERY_SERVICE. If a subclass provides
196 * other services they need to provide their own descriptions
197 */
[28966]198 protected Element getServiceDescription(Document doc, String service, String lang, String subset)
[24858]199 {
200 if (!service.equals(QUERY_SERVICE))
201 {
202 return null;
203 }
[24394]204
[28966]205 Element tq_service = doc.createElement(GSXML.SERVICE_ELEM);
[24858]206 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
207 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
208 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
209 {
[28966]210 tq_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(QUERY_SERVICE, lang)));
211 tq_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_SUBMIT, getServiceSubmit(QUERY_SERVICE, lang)));
212 tq_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(QUERY_SERVICE, lang)));
[24858]213 }
214 if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
215 {
[28966]216 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
[24858]217 addCustomQueryParams(param_list, lang);
218 addStandardQueryParams(param_list, lang);
219 tq_service.appendChild(param_list);
220 }
[29558]221 if (subset == null || subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER)) {
222
223 if (service_metadata_list == null) {
224 Document ml_doc = XMLConverter.newDOM();
225 service_metadata_list = ml_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
226 if (does_paging) {
227 service_metadata_list.appendChild(GSXML.createMetadataElement(ml_doc, "does_paging", "true"));
228 }
229 }
230 tq_service.appendChild(doc.importNode(service_metadata_list, true));
231 }
[24858]232 return tq_service;
233
[9002]234 }
235
[24858]236 // perhaps these should be changed to search down the class hierarchy for
237 // values - do can just put the info in the resource bundle to use it
238 /** returns the default name for the TextQuery service */
239 protected String getServiceName(String service_id, String lang)
240 {
241 return getTextString(service_id + ".name", lang);
[9002]242 }
[24858]243
244 /** returns the default description for the TextQuery service */
245 protected String getServiceDescription(String service_id, String lang)
246 {
247 return getTextString(service_id + ".description", lang);
[9002]248 }
[9281]249
[24858]250 /** returns the default submit button text for the TextQuery service */
251 protected String getServiceSubmit(String service_id, String lang)
252 {
253 return getTextString(service_id + ".submit", lang);
[9281]254
[24858]255 }
[9281]256
[24858]257 /** adds the standard query params into the service description */
258 protected void addStandardQueryParams(Element param_list, String lang)
259 {
260 // this test is not so good. here we are using absence of default index
261 // to determine whether we have indexes or not. But in other places,
262 // absence of default index just means to use the first one as default.
263 if (!default_index.equals(""))
264 {
265 createParameter(INDEX_PARAM, param_list, lang);
266 }
267 if (does_chunking)
268 {
269 createParameter(MAXDOCS_PARAM, param_list, lang);
270 }
271 if (does_paging)
272 {
273 createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
274 createParameter(START_PAGE_PARAM, param_list, lang);
275 }
276 createParameter(QUERY_PARAM, param_list, lang);
[14002]277 }
[24858]278
279 /**
280 * adds any service specific query params into the service default
281 * implementation: add nothing. subclasses may need to override this to add
282 * in their specific parameters
283 */
284 protected void addCustomQueryParams(Element param_list, String lang)
285 {
286 // default behaviour, do nothing
[9266]287 }
[24858]288
289 protected void createParameter(String name, Element param_list, String lang)
290 {
291 createParameter(name, param_list, lang, null);
[9002]292 }
293
[24858]294 protected void createParameter(String name, Element param_list, String lang, String default_value)
295 {
296 // at this level, not interested in boolean return type
297 createParameterChain(name, param_list, lang, default_value);
298 }
[9002]299
[24858]300 /**
301 * default implementations for the standard parameters plus some other
302 * common ones index, maxDocs, hitsPerPage, startPage
303 */
[24394]304
[24858]305 protected boolean createParameterChain(String name, Element param_list, String lang, String default_value)
306 {
[28966]307 Document doc = param_list.getOwnerDocument();
[24858]308 Element param = null;
309 String param_default = default_value;
[28181]310 if (default_value == null) {
311 // have we got a stored up default? will be null if not there
312 param_default = paramDefaults.get(name);
313 }
[24892]314 if (name.equals(QUERY_PARAM) || name.equals(RAW_PARAM))
[24858]315 {
[28966]316 param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_STRING, param_default, null, null);
[24858]317 param_list.appendChild(param);
318 return true;
319 }
320 else if (name.equals(INDEX_PARAM))
321 {
322 // should we make these class fields?
[25635]323 ArrayList<String> index_ids = new ArrayList<String>();
324 ArrayList<String> index_names = new ArrayList<String>();
[24858]325 getIndexData(index_ids, index_names, lang);
326 String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE;
327 if (does_multi_index_search)
328 {
329 param_type = GSXML.PARAM_TYPE_ENUM_MULTI;
330 }
331 if (param_default == null)
332 {
333 param_default = this.default_index;
334 }
[28966]335 param = GSXML.createParameterDescription2(doc, INDEX_PARAM, getTextString("param." + INDEX_PARAM, lang), param_type, param_default, index_ids, index_names);
[24858]336 param_list.appendChild(param);
337 return true;
338 }
339 else if (name.equals(MAXDOCS_PARAM))
340 {
[28966]341 param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null);
[24858]342 param_list.appendChild(param);
343 return true;
344 }
345 else if (name.equals(HITS_PER_PAGE_PARAM))
346 {
[28966]347 param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null);
[24858]348 param_list.appendChild(param);
349 return true;
350 }
351 else if (name.equals(START_PAGE_PARAM))
352 {
353 // start page - set to 1 for the search page
[28966]354 param = GSXML.createParameterDescription(doc, START_PAGE_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, param_default, null, null);
[24858]355 param_list.appendChild(param);
356 return true;
357 }
358
359 // Get to there then none of the above params matched
360 // => return false so the chain can continue
361 return false;
[13998]362 }
[24858]363
364 /**
365 * create an element to go into the search results list. A node element has
366 * the form <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy'
367 * rank='0.23'/>
368 */
[28966]369 protected Element createDocNode(Document doc, String node_id, String rank)
[24858]370 {
[28966]371 return this.gs_doc.createDocNode(doc, node_id, rank);
[14945]372 }
[24858]373
374 /**
375 * returns the document type of the doc that the specified node belongs to.
376 * should be one of GSXML.DOC_TYPE_SIMPLE, GSXML.DOC_TYPE_PAGED,
377 * GSXML.DOC_TYPE_HIERARCHY
378 */
379 protected String getDocType(String node_id)
380 {
381 return this.gs_doc.getDocType(node_id);
[9002]382 }
[24394]383
[24858]384 /**
385 * returns the node type of the specified node. should be one of
386 * GSXML.NODE_TYPE_LEAF, GSXML.NODE_TYPE_INTERNAL, GSXML.NODE_TYPE_ROOT
387 */
388 protected String getNodeType(String node_id, String doc_type)
389 {
390 return this.gs_doc.getNodeType(node_id, doc_type);
391 }
[24394]392
[24858]393 /** returns true if the node has child nodes */
394 protected boolean hasChildren(String node_id)
395 {
396 return this.gs_doc.hasChildren(node_id);
397 }
[24394]398
[24858]399 /** returns true if the node has a parent */
400 protected boolean hasParent(String node_id)
401 {
402 return this.gs_doc.hasParent(node_id);
403 }
[9002]404
[24858]405 /**
406 * get the details about the indexes available must be implemented by
407 * subclass there must be at least one index
408 */
[25635]409 abstract protected void getIndexData(ArrayList<String> index_ids, ArrayList<String> index_names, String lang);
[24394]410
[9266]411}
Note: See TracBrowser for help on using the repository browser.