Changeset 3823


Ignore:
Timestamp:
2003-03-10T10:59:12+13:00 (21 years ago)
Author:
mdewsnip
Message:

Modified to use base class GS2Search. Implements processTextQuery, as this is partly specific to the search engine used.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGSearch.java

    r3800 r3823  
    1919package org.greenstone.gsdl3.service;
    2020
    21 // greenstone classes
     21
     22// Greenstone classes
    2223import org.greenstone.mg.*;
    23 import org.greenstone.gdbm.*;
    2424import org.greenstone.gsdl3.util.*;
    2525
    26 // xml classes
    27 import org.w3c.dom.Document;
     26// XML classes
    2827import org.w3c.dom.Element;
    2928import org.w3c.dom.Node;
    3029import org.w3c.dom.NodeList;
    31 import org.w3c.dom.Text;
    32 
    33 // general java classes
     30
     31// General Java classes
    3432import java.io.File;
    3533import java.util.HashMap;
     
    3937import java.util.Vector;
    4038
     39
    4140/**
    4241 *
     
    4645
    4746public class GS2MGSearch
    48     extends ServiceRack {
    49 
    50     // the services on offer
    51     // these strings must match what is found in the properties file
    52     private static final String TEXT_QUERY_SERVICE = "TextQuery";
    53 
    54     // params used
     47    extends GS2Search {
     48
     49    // Parameters used
    5550    private static final String INDEX_PARAM = "index";
    56     private static final String CASE_PARAM = "case";
    57     private static final String STEM_PARAM = "stem";
    58     private static final String MATCH_PARAM = "matchMode";
    59     private static final String MATCH_PARAM_ALL = "all";
    60     private static final String MATCH_PARAM_SOME = "some";
    61     private static final String RANK_PARAM = "sortBy";
    62     private static final String RANK_PARAM_RANK = "rank";
    63     private static final String RANK_PARAM_NONE = "natural";
    64     private static final String MAXDOCS_PARAM = "maxDocs";
    65     private static final String BOOLEAN_PARAM_ON = "1";
    66     private static final String BOOLEAN_PARAM_OFF = "0";
    67     private static final String QUERY_PARAM = "query";
    68 
    69     // elements used in the config file that are specific to this class
     51
     52    // Elements used in the config file that are specific to this class
    7053    private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
    7154    private static final String INDEX_ELEM = "index";
    7255
    73     private static final String EQUIV_TERM_ELEM = "equivTerm";
    74 
    75     private static final String STEM_ATT = "stem";
    76     private static final String NUM_DOCS_MATCH_ATT = "numDocsMatch";
    77     private static final String FREQ_ATT = "freq";
    78 
    7956    private MGWrapper mg_src_ = null;
    80     private GDBMWrapper gdbm_src_ = null;
    8157
    8258    private String default_index_ = null;
    8359
    84     private Element config_info_ = null;
    85 
    8660
    8761    /** constructor */
    88     public GS2MGSearch() {
     62    public GS2MGSearch()
     63    {
    8964    mg_src_ = new MGWrapper();
    90     gdbm_src_ = new GDBMWrapper();
    9165    }
    9266
     
    9569    public boolean configure(Element info)
    9670    {
    97     System.out.println("configuring GS2MGSearch");
    98     config_info_ = info;
    99 
    100     // get the default index out of <defaultIndex> (buildConfig.xml)
     71    // Do generic configuration
     72    if (super.configure(info) == false)
     73        return false;
     74
     75    // Do specific configuration
     76    System.out.println("Configuring GS2MGSearch...");
     77
     78    // Get the default index out of <defaultIndex> (buildConfig.xml)
    10179    Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
    10280    if (def != null) {
     
    10785        return false;
    10886    }
    109 
    110     Element e = null;
    111     // these entries should reflect the build config file - some services may not be available depending on how the colleciton was built.
    112     // set up short_service_info_ - for now just has name and type
    113     e = doc_.createElement(GSXML.SERVICE_ELEM);
    114     e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    115     e.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
    116     short_service_info_.appendChild(e);
    117 
    118     // set up service_info_map_ - for now, just has the same elements as above
    119     // should have full details about each service incl params lists etc.
    120     e = doc_.createElement(GSXML.SERVICE_ELEM);
    121     e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    122     e.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
    123     Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    124     createTextQueryParamList(param_list, false, null);
    125     e.appendChild(param_list);
    126     service_info_map_.put(TEXT_QUERY_SERVICE, e);
    127 
    128     // Open GDBM database for querying
    129     String gdbm_db_file = GSFile.GDBMDatabaseFile(site_home_, cluster_name_);
    130     if (gdbm_src_.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
    131         return true;
    132     }
    133     else {
    134         System.err.println("Error: Could not open gdbm database!");
    135         return false;
    136     }
     87    // System.out.println("Default index: " + default_index_);
     88
     89    return true;
    13790    }
    13891
    13992
    14093    /** creates a new param element and adds it to the param list */
    141     protected void createParameter(String name, Element param_list, boolean display, String lang)
    142     {
    143     Element param=null;
     94    protected void createParameter(String name, Element param_list, boolean display,
     95                   String lang)
     96    {
     97    Element param = null;
    14498
    14599    if (name.equals(INDEX_PARAM)) {
     
    149103        int len = indexes.getLength();
    150104        if (len > 1) { // add index param to list only if more than one index specified
    151         String [] inds = new String[len];
     105        String[] inds = new String[len];
    152106        for (int i=0; i<len; i++) {
    153107            inds[i] = ((Element)indexes.item(i)).getAttribute(GSXML.NAME_ATT);
     
    161115        }
    162116    }
    163     else if (name.equals(CASE_PARAM)) {
    164         if (display) {
    165         String[] bool_ops = {"0", "1"};
    166         String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
    167         param = GSXML.createParameterDisplay(doc_, CASE_PARAM, getTextString("param."+CASE_PARAM, lang),  bool_ops, bool_texts);
    168         } else {
    169         param = GSXML.createParameter(doc_, CASE_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
    170         }
    171     }
    172     else if (name.equals(STEM_PARAM)) {
    173         if (display) {
    174         String[] bool_ops = {"0", "1"};
    175         String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
    176         param = GSXML.createParameterDisplay(doc_, STEM_PARAM, getTextString("param."+STEM_PARAM, lang),  bool_ops, bool_texts);
    177         } else {
    178         param = GSXML.createParameter(doc_, STEM_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
    179         }
    180     }
    181     else if (name.equals(MATCH_PARAM)) {
    182         String[] vals = {MATCH_PARAM_ALL, MATCH_PARAM_SOME};
    183         if (display) {
    184         String[] val_texts = {getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_ALL, lang),getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_SOME, lang)};
    185 
    186         param = GSXML.createParameterDisplay(doc_, MATCH_PARAM, getTextString("param."+MATCH_PARAM, lang), vals, val_texts);
    187         } else {
    188         param = GSXML.createParameter(doc_, MATCH_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, MATCH_PARAM_ALL, vals);
    189         }   
    190     }
    191     else if (name.equals(MAXDOCS_PARAM)) {
    192         if (display) {
    193         param = GSXML.createParameterDisplay(doc_, MAXDOCS_PARAM, getTextString("param."+MAXDOCS_PARAM, lang), null, null);
    194         } else {
    195         param = GSXML.createParameter(doc_, MAXDOCS_PARAM, GSXML.PARAM_TYPE_INTEGER, "10", null);
    196         }
    197     }
    198     else if (name.equals(QUERY_PARAM)) {
    199         if (display) {
    200         param = GSXML.createParameterDisplay(doc_, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), null, null);
    201         } else {
    202         param = GSXML.createParameter(doc_, QUERY_PARAM, GSXML.PARAM_TYPE_STRING, null, null);
    203         }
    204     }
    205117
    206118    // add the param to the list
    207119    if (param != null) {
    208120        param_list.appendChild(param);
     121    }
     122    else {
     123        super.createParameter(name, param_list, display, lang);
    209124    }
    210125    }
     
    215130     * otherwise it creates the description version
    216131     */
    217     protected boolean createTextQueryParamList(Element param_list, boolean display, String lang)
     132    protected boolean createTextQueryParamList(Element param_list, boolean display,
     133                           String lang)
    218134    {
    219135    // the order they are specified here is the order they appear on
    220136    // the query form
    221137    createParameter(INDEX_PARAM, param_list, display, lang);
    222     createParameter(CASE_PARAM, param_list, display, lang);
    223     createParameter(STEM_PARAM, param_list, display, lang);
    224     createParameter(MATCH_PARAM, param_list, display, lang);
    225     createParameter(MAXDOCS_PARAM, param_list, display, lang);
    226     createParameter(QUERY_PARAM, param_list, display, lang);
    227     return true;
    228     }
    229 
    230 
    231     /** creates a display element containing all the text strings needed to display the service page, in the language specified */
    232     protected Element createServiceDisplay(String service, String lang) {
    233     Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
    234     display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM,
    235                             getTextString(service+".name", lang)));
    236     display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM,
    237                             getTextString(service+".submit", lang)));
    238 
    239     // now need to add in the params
    240     if (service.equals(TEXT_QUERY_SERVICE)) {
    241         createTextQueryParamList(display, true, lang);
    242     }
    243 
    244     return display;
    245     }
    246 
    247 
    248     /** process a text query */
     138    return super.createTextQueryParamList(param_list, display, lang);
     139    }
     140
     141
     142    /** Creates a display element containing all the text strings needed to display
     143    the service page, in the language specified */
     144    protected Element createServiceDisplay(String service, String lang)
     145    {
     146    // Create a service display for the basic text query service
     147    return super.createServiceDisplay(service, lang);
     148    }
     149
     150
     151    /** Process a text query */
    249152    protected Element processTextQuery(Element request)
    250153    {
     154    // Create a new (empty) result message
    251155    Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
    252156    String from = GSPath.appendLink(cluster_name_, TEXT_QUERY_SERVICE);
    253157    result.setAttribute(GSXML.FROM_ATT, from);
    254     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
    255 
    256     // get param list
    257     Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    258     if (param_list==null) {
    259         System.err.println("GS2MGSearch, TextQuery Error: no param list in request!");
    260         return result; // empty result
    261     }
     158    result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     159    Element result_content = doc_.createElement(GSXML.CONTENT_ELEM);
     160    result.appendChild(result_content);
     161
     162    // Get the parameters of the request
     163    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     164    if (param_list == null) {
     165        System.err.println("Error: TextQuery request had no paramList.");
     166        return result;  // Return the empty result
     167    }
     168
     169    // Process the request parameters
    262170    HashMap params = GSXML.extractParams(param_list);
    263     String query = (String)params.get(QUERY_PARAM);
    264     if (query == null) {
    265         // no query, no result
    266         return result;
    267     }
    268 
     171
     172    // Make sure a query has been specified
     173    String query = (String) params.get(QUERY_PARAM);
     174    if (query == null || query == "") {
     175        return result;  // Return the empty result
     176    }
     177
     178    // If an index hasn't been specified, use the default
    269179    String index = (String) params.get(INDEX_PARAM);
    270     if (index == null) { // if it is not present, use the default index
     180    if (index == null) {
    271181        index = default_index_;
    272182    }
    273183
    274     // now set up the mg stuff
     184    // The location of the MG index and text files
    275185    String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_) +
    276186                       File.separatorChar;  // Needed for MG
    277187    String textdir = GSFile.collectionTextPath(cluster_name_);
    278188    String indexpath = GSFile.collectionIndexPath(cluster_name_, index);
     189    mg_src_.setIndex(indexpath);
    279190
    280191    // set the mg query parameters to the values the user has specified
    281192    setStandardQueryParams(params);
    282     mg_src_.setIndex(indexpath);
    283 
    284     System.out.println("GS2MGSearch, query string: " + query);
    285     mg_src_.runQuery(basedir + File.separatorChar, textdir, query);
     193    mg_src_.runQuery(basedir, textdir, query);
    286194    MGQueryResult mqr = mg_src_.getQueryResult();
    287195    long totalDocs = mqr.getTotalDocs();
    288     // System.out.println("Matching documents: " + totalDocs);
    289 
    290     // get the docnums out, and convert to HASH ids
     196
     197    // Get the docnums out, and convert to HASH ids
    291198    Vector docs = mqr.getDocs();
    292199    if (docs.size() == 0) {
    293         // no docs found
    294         System.out.println("No results found...\n");
     200        System.out.println("Warning: No results found...\n");
    295201    }
    296202
     
    304210    metadata_list.appendChild(num_matches_elem);
    305211
    306     // Response content: documents and terms
    307     Element c = doc_.createElement(GSXML.CONTENT_ELEM);
    308     result.appendChild(c);
    309 
     212    // Create a document list to store the matching documents, and add them
    310213    Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
    311     c.appendChild(document_list);
    312     // add each document
     214    result_content.appendChild(document_list);
    313215    for (int d = 0; d < docs.size(); d++) {
    314216        long docnum = ((MGDocInfo) docs.elementAt(d)).num_;
    315         String id = gdbm_src_.docnum2Oid(docnum);
    316         System.out.println("Docnum: " + docnum + " ID: " + id);
    317         Node no = GSXML.createDocumentElement(doc_, id);
    318         document_list.appendChild(no);
    319     }
    320 
     217        String doc_id = gdbm_src_.docnum2Oid(docnum);
     218        Element doc_node = createDocumentNodeElement(doc_id);
     219        document_list.appendChild(doc_node);
     220    }
     221
     222    // Create a term list to store the term information, and add it
    321223    Element term_list = doc_.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
    322     c.appendChild(term_list);
    323     // get the terms out, and add to query result metadata
     224    result_content.appendChild(term_list);
    324225    Vector terms = mqr.getTerms();
    325226    for (int t = 0; t < terms.size(); t++) {
     
    379280        }
    380281        else if (name.equals(MATCH_PARAM)) {
    381         int mode;
    382         if (value.equals(MATCH_PARAM_ALL)) mode = 1;
    383         else mode = 0;
     282        int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
    384283        mg_src_.setMatchMode(mode);
    385284        }
Note: See TracChangeset for help on using the changeset viewer.