/* * AbstractSearch.java * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.gsdl3.service; // Greenstone classes import java.util.ArrayList; import java.util.HashMap; import org.apache.log4j.Logger; import org.greenstone.gsdl3.util.AbstractBasicDocument; import org.greenstone.gsdl3.util.BasicDocument; import org.greenstone.gsdl3.util.GSPath; import org.greenstone.gsdl3.util.GSXML; import org.greenstone.gsdl3.util.XMLConverter; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * Partially implements a generic search service * */ public abstract class AbstractSearch extends ServiceRack { static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractSearch.class.getName()); // the search service protected String QUERY_SERVICE = null; // set by subclass // compulsory params protected static final String INDEX_PARAM = "index"; protected static final String QUERY_PARAM = "query"; protected static final String RAW_PARAM = "rawquery"; // optional standard params - some of these have to be implemented protected static final String MAXDOCS_PARAM = "maxDocs"; protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage"; protected static final String START_PAGE_PARAM = "startPage"; protected AbstractBasicDocument gs_doc = null; /** can more than one index be searched at the same time? */ protected boolean does_multi_index_search = false; /** does this service support paging of results? */ protected boolean does_paging = false; /** does this service support asking for a subset of results? */ protected boolean does_chunking = false; /** does this service support faceting search results */ protected boolean does_faceting = false; /** does this service support highlighting snippets results */ protected boolean does_highlight_snippets = false; protected boolean does_full_field_highlighting = false; /** * the default document type - use if all documents are the same type */ protected String default_document_type = null; /** * the default index, or comma separated list if more than one is the * default (with start and end commas, eg ,TI,SU,). Should be set by * configure() */ protected String default_index = ""; protected Element service_metadata_list = null; protected HashMap paramDefaults = null; public AbstractSearch() { paramDefaults = new HashMap(); } /** * Sets up the short service info for service by QUERY_SERVICE (e.g. * TextQuery or AudioQuery) If other services will be provided, should be * added in the subclass configure also looks for search format info, and * document format info */ public boolean configure(Element info, Element extra_info) { if (!super.configure(info, extra_info)) { return false; } logger.info("Configuring AbstractSearch..."); this.config_info = info; // set up short_service_info_ // => for now just has id and type. the name (lang dependent) // will be added in if the list is requested. Element tq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM); tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY); tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE); this.short_service_info.appendChild(tq_service); // load up paging defaults if (does_chunking) { paramDefaults.put(MAXDOCS_PARAM, "100"); } if (does_paging) { paramDefaults.put(HITS_PER_PAGE_PARAM, "20"); paramDefaults.put(START_PAGE_PARAM, "1"); } // load up any search param defaults Element search_elem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM); if (search_elem != null) { getSearchParamDefaults(search_elem); } // add some format info to service map if there is any // => lookin extra info first look in buildConfig Element format = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM); if (format == null) { // try to find a format element inside that contains a gsf:template. Note what if we have only xsl:templates?? NodeList format_elems = null; if (search_elem != null) { format_elems = search_elem.getElementsByTagName(GSXML.FORMAT_ELEM); } for (int i = 0; i < format_elems.getLength(); i++) { format = (Element) format_elems.item(i); if (format.getElementsByTagName("gsf:template").getLength() != 0) { break; } } }//end of if(format==null) // if (format != null) { this.format_info_map.put(QUERY_SERVICE, this.desc_doc.importNode(format, true)); } // look for document display format - for documentType String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM); Element display_format = (Element) GSXML.getNodeByPath(extra_info, path); if (display_format != null) { // check for docType option. Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType"); if (doc_type_opt != null) { String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT); if (!value.equals("")) { this.default_document_type = value; } } } // Base line for document (might be overriden by sub-classes) gs_doc = new BasicDocument(this.default_document_type); return true; } protected void getSearchParamDefaults(Element search_elem) { NodeList param_defaults_list = GSXML.getChildrenByTagName(search_elem, GSXML.PARAM_DEFAULT_ELEM); for (int i=0; i index_ids = new ArrayList(); ArrayList index_names = new ArrayList(); getIndexData(index_ids, index_names, lang); String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE; if (does_multi_index_search) { param_type = GSXML.PARAM_TYPE_ENUM_MULTI; } if (param_default == null) { param_default = this.default_index; } param = GSXML.createParameterDescription2(doc, INDEX_PARAM, getTextString("param." + INDEX_PARAM, lang), param_type, param_default, index_ids, index_names); param_list.appendChild(param); return true; } else if (name.equals(MAXDOCS_PARAM)) { param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null); param_list.appendChild(param); return true; } else if (name.equals(HITS_PER_PAGE_PARAM)) { param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null); param_list.appendChild(param); return true; } else if (name.equals(START_PAGE_PARAM)) { // start page - set to 1 for the search page param = GSXML.createParameterDescription(doc, START_PAGE_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, param_default, null, null); param_list.appendChild(param); return true; } // Get to there then none of the above params matched // => return false so the chain can continue return false; } /** * create an element to go into the search results list. A node element has * the form */ protected Element createDocNode(Document doc, String node_id, String rank) { return this.gs_doc.createDocNode(doc, node_id, rank); } /** * returns the document type of the doc that the specified node belongs to. * should be one of GSXML.DOC_TYPE_SIMPLE, GSXML.DOC_TYPE_PAGED, * GSXML.DOC_TYPE_HIERARCHY */ protected String getDocType(String node_id) { return this.gs_doc.getDocType(node_id); } /** * returns the node type of the specified node. should be one of * GSXML.NODE_TYPE_LEAF, GSXML.NODE_TYPE_INTERNAL, GSXML.NODE_TYPE_ROOT */ protected String getNodeType(String node_id, String doc_type) { return this.gs_doc.getNodeType(node_id, doc_type); } /** returns true if the node has child nodes */ protected boolean hasChildren(String node_id) { return this.gs_doc.hasChildren(node_id); } /** returns true if the node has a parent */ protected boolean hasParent(String node_id) { return this.gs_doc.hasParent(node_id); } /** * get the details about the indexes available must be implemented by * subclass there must be at least one index */ abstract protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang); }