Changeset 14428

Show
Ignore:
Timestamp:
06.09.2007 10:39:23 (12 years ago)
Author:
xiao
Message:

make MGSearchWrapper a static variable; modify to get all activities in method processTextQuery() synchronized.

Files:
1 modified

Legend:

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

    r14378 r14428  
    2525// XML classes 
    2626import org.w3c.dom.Document; 
    27 import org.w3c.dom.Element;  
     27import org.w3c.dom.Element; 
    2828import org.w3c.dom.NodeList; 
    2929 
     
    4545 */ 
    4646 
    47 public class GS2MGSearch  
    48     extends AbstractGS2Search  
    49 { 
    50  
    51     protected MGWrapper mg_src = null; 
    52  
    53     static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2MGSearch.class.getName()); 
    54  
    55  
     47public class GS2MGSearch 
     48extends AbstractGS2Search { 
     49     
     50//    protected MGSearchWrapper mg_src = null; 
     51    protected MGSearchWrapper mg_src = null; 
     52     
     53    static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2MGSearch.class.getName ()); 
     54     
     55     
    5656    /** constructor */ 
    57     public GS2MGSearch() 
    58     { 
    59     this.mg_src = new MGWrapper(); 
    60      
    61     } 
    62     public void cleanUp() { 
    63     super.cleanUp(); 
    64     this.mg_src.unloadIndexData(); 
    65     } 
    66  
     57    public GS2MGSearch () { 
     58//        this.mg_src = new MGSearchWrapper (); 
     59        this.mg_src = new MGSearchWrapper (); 
     60         
     61    } 
     62    public void cleanUp () { 
     63        super.cleanUp (); 
     64        this.mg_src.unloadIndexData (); 
     65    } 
     66     
    6767    /** configure this service */ 
    68     public boolean configure(Element info, Element extra_info) 
    69     { 
    70     if (!super.configure(info, extra_info)){ 
    71         return false; 
    72     } 
    73  
    74     this.mg_src.setMaxNumeric(this.maxnumeric);              
    75              
    76     return true; 
    77     } 
    78      
    79  
    80  
     68    public boolean configure (Element info, Element extra_info) { 
     69        if (!super.configure (info, extra_info)){ 
     70            return false; 
     71        } 
     72         
     73        this.mg_src.setMaxNumeric (this.maxnumeric); 
     74         
     75        return true; 
     76    } 
     77     
     78     
     79     
    8180    /** do the actual query */ 
    82     protected Element processTextQuery(Element request)  
    83     { 
    84  
    85     // Create a new (empty) result message 
    86     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM); 
    87     result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE); 
    88     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 
    89  
    90     // Get the parameters of the request 
    91     Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
    92     if (param_list == null) { 
    93         logger.error("TextQuery request had no paramList."); 
    94         return result;  // Return the empty result 
    95     } 
    96  
    97     // Process the request parameters 
    98     HashMap params = GSXML.extractParams(param_list, false); 
    99      
    100     // Make sure a query has been specified 
    101     String query = (String) params.get(QUERY_PARAM); 
    102     if (query == null || query.equals("")) { 
    103         return result;  // Return the empty result 
    104     } 
    105  
    106     // If an index hasn't been specified, use the default 
    107     String index = (String) params.get(INDEX_PARAM);  
    108     if (index == null) { 
    109         index = this.default_index; 
    110     } 
    111  
    112     // If a subcollection index has been specified, use it 
    113     String indexSub = (String) params.get(INDEX_SUBCOLLECTION_PARAM);  
    114     if (indexSub != null) { 
    115         index += indexSub; 
    116     } 
    117     else{ 
    118         if (!this.default_index_subcollection.equals("")){ 
    119         index += this.default_index_subcollection; 
    120         } 
    121     } 
    122  
    123     // If a subcollection index has been specified, use it 
    124     String indexLang = (String) params.get(INDEX_LANGUAGE_PARAM);  
    125     if (indexLang != null) { 
    126         index += indexLang; 
    127     } 
    128     else{ 
    129         if (!this.default_index_language.equals("")){ 
    130         index += this.default_index_language;  
    131         } 
    132     } 
    133     
    134     // The location of the MG index and text files 
    135     String basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +  File.separatorChar;  // Needed for MG 
    136     String textdir = GSFile.collectionTextPath(this.index_stem); 
    137     String indexpath = GSFile.collectionIndexPath(this.index_stem, index); 
    138     this.mg_src.setIndex(indexpath); 
    139     System.err.println("index path = "+indexpath); 
    140     // set the mg query parameters to the values the user has specified 
    141     setStandardQueryParams(params); 
    142     this.mg_src.runQuery(basedir, textdir, query); 
    143     MGQueryResult mqr = this.mg_src.getQueryResult(); 
    144     if (mqr.isClear()) { 
    145         // something has gone wrong 
    146         GSXML.addError(this.doc, result, "Couldn't query the mg database", GSXML.ERROR_TYPE_SYSTEM); 
    147         return result; 
    148     } 
    149     long totalDocs = mqr.getTotalDocs(); 
    150  
    151     // Get the docnums out, and convert to HASH ids 
    152     Vector docs = mqr.getDocs(); 
    153     if (docs.size() == 0) { 
    154         logger.error("No results found...\n"); 
    155     } 
    156  
    157     // Create a metadata list to store information about the query results 
    158     Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);  
    159     result.appendChild(metadata_list); 
    160  
    161     // Add a metadata element specifying the number of matching documents 
    162     // because teh total number is just the number returned, use numDocsReturned, not numDocsMatched 
    163     GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+totalDocs); 
    164     // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query 
    165     GSXML.addMetadata(this.doc, metadata_list, "query", query); 
    166  
    167     if (docs.size() > 0) { 
    168         // Create a document list to store the matching documents, and add them 
    169         Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER); 
    170         result.appendChild(document_list); 
    171         for (int d = 0; d < docs.size(); d++) { 
    172         long docnum = ((MGDocInfo) docs.elementAt(d)).num_; 
    173         float rank = ((MGDocInfo) docs.elementAt(d)).rank_; 
    174         String doc_id = internalNum2OID(docnum); 
    175         Element doc_node = createDocNode(doc_id, Float.toString(rank)); 
    176         document_list.appendChild(doc_node); 
    177         } 
    178     } 
    179      
    180     // Create a term list to store the term information, and add it 
    181     Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER); 
    182     result.appendChild(term_list); 
    183     Vector terms = mqr.getTerms(); 
    184     for (int t = 0; t < terms.size(); t++) { 
    185         MGTermInfo term_info = (MGTermInfo) terms.get(t); 
    186  
    187         String term = term_info.term_; 
    188         int stem_method = term_info.stem_method_; 
    189         Vector equiv_terms = term_info.equiv_terms_; 
    190  
    191         Element term_elem = this.doc.createElement(GSXML.TERM_ELEM); 
    192         term_elem.setAttribute(GSXML.NAME_ATT, term); 
    193         term_elem.setAttribute(STEM_ATT, "" + stem_method); 
    194  
    195         Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER); 
    196         term_elem.appendChild(equiv_term_list); 
    197  
    198         long total_term_freq = 0; 
    199         for (int et = 0; et < equiv_terms.size(); et++) { 
    200         MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et); 
    201  
    202         Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM); 
    203         equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_); 
    204         equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_); 
    205         equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_); 
    206         equiv_term_list.appendChild(equiv_term_elem); 
    207  
    208         total_term_freq += equiv_term_info.term_freq_; 
    209         } 
    210  
    211         term_elem.setAttribute(FREQ_ATT, "" + total_term_freq); 
    212         term_list.appendChild(term_elem); 
    213     } 
    214     return result; 
    215     } 
    216  
     81    protected Element processTextQuery (Element request) { 
     82         
     83        // Create a new (empty) result message ('doc' is in ServiceRack.java) 
     84        Element result = this.doc.createElement (GSXML.RESPONSE_ELEM); 
     85        result.setAttribute (GSXML.FROM_ATT, TEXT_QUERY_SERVICE); 
     86        result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); 
     87         
     88        // Get the parameters of the request 
     89        Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
     90        if (param_list == null) { 
     91            logger.error ("TextQuery request had no paramList."); 
     92            return result;  // Return the empty result 
     93        } 
     94         
     95        // Process the request parameters 
     96        HashMap params = GSXML.extractParams (param_list, false); 
     97         
     98        // Make sure a query has been specified 
     99        String query = (String) params.get (QUERY_PARAM); 
     100        if (query == null || query.equals ("")) { 
     101            return result;  // Return the empty result 
     102        } 
     103         
     104        // If an index hasn't been specified, use the default 
     105        String index = (String) params.get (INDEX_PARAM); 
     106        if (index == null) { 
     107            index = this.default_index; 
     108        } 
     109         
     110        // If a subcollection index has been specified, use it 
     111        String indexSub = (String) params.get (INDEX_SUBCOLLECTION_PARAM); 
     112        if (indexSub != null) { 
     113            index += indexSub; 
     114        } 
     115        else{ 
     116            if (!this.default_index_subcollection.equals ("")){ 
     117                index += this.default_index_subcollection; 
     118            } 
     119        } 
     120         
     121        // If a subcollection index has been specified, use it 
     122        String indexLang = (String) params.get (INDEX_LANGUAGE_PARAM); 
     123        if (indexLang != null) { 
     124            index += indexLang; 
     125        } 
     126        else{ 
     127            if (!this.default_index_language.equals ("")){ 
     128                index += this.default_index_language; 
     129            } 
     130        } 
     131         
     132        // The location of the MG index and text files 
     133        String basedir = GSFile.collectionBaseDir (this.site_home, this.cluster_name) +  File.separatorChar;  // Needed for MG 
     134        String textdir = GSFile.collectionTextPath (this.index_stem); 
     135        String indexpath = GSFile.collectionIndexPath (this.index_stem, index); 
     136        this.mg_src.setIndex (indexpath); 
     137        System.err.println ("index path = "+indexpath); 
     138        // set the mg query parameters to the values the user has specified 
     139        setStandardQueryParams (params); 
     140        this.mg_src.runQuery (basedir, textdir, query); 
     141        MGQueryResult mqr = this.mg_src.getQueryResult (); 
     142        if (mqr.isClear ()) { 
     143            // something has gone wrong 
     144            GSXML.addError (this.doc, result, "Couldn't query the mg database", GSXML.ERROR_TYPE_SYSTEM); 
     145            return result; 
     146        } 
     147        long totalDocs = mqr.getTotalDocs (); 
     148         
     149        // Get the docnums out, and convert to HASH ids 
     150        Vector docs = mqr.getDocs (); 
     151        if (docs.size () == 0) { 
     152            logger.error ("No results found...\n"); 
     153        } 
     154         
     155        // Create a metadata list to store information about the query results 
     156        Element metadata_list = this.doc.createElement (GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER); 
     157        result.appendChild (metadata_list); 
     158         
     159        // Add a metadata element specifying the number of matching documents 
     160        // because teh total number is just the number returned, use numDocsReturned, not numDocsMatched 
     161        GSXML.addMetadata (this.doc, metadata_list, "numDocsReturned", ""+totalDocs); 
     162        // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query 
     163        GSXML.addMetadata (this.doc, metadata_list, "query", query); 
     164         
     165        if (docs.size () > 0) { 
     166            // Create a document list to store the matching documents, and add them 
     167            Element document_list = this.doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER); 
     168            result.appendChild (document_list); 
     169            for (int d = 0; d < docs.size (); d++) { 
     170                long docnum = ((MGDocInfo) docs.elementAt (d)).num_; 
     171                float rank = ((MGDocInfo) docs.elementAt (d)).rank_; 
     172                String doc_id = internalNum2OID (docnum); 
     173                Element doc_node = createDocNode (doc_id, Float.toString (rank)); 
     174                document_list.appendChild (doc_node); 
     175            } 
     176        } 
     177         
     178        // Create a term list to store the term information, and add it 
     179        Element term_list = this.doc.createElement (GSXML.TERM_ELEM+GSXML.LIST_MODIFIER); 
     180        result.appendChild (term_list); 
     181        Vector terms = mqr.getTerms (); 
     182        for (int t = 0; t < terms.size (); t++) { 
     183            MGTermInfo term_info = (MGTermInfo) terms.get (t); 
     184             
     185            String term = term_info.term_; 
     186            int stem_method = term_info.stem_method_; 
     187            Vector equiv_terms = term_info.equiv_terms_; 
     188             
     189            Element term_elem = this.doc.createElement (GSXML.TERM_ELEM); 
     190            term_elem.setAttribute (GSXML.NAME_ATT, term); 
     191            term_elem.setAttribute (STEM_ATT, "" + stem_method); 
     192             
     193            Element equiv_term_list = this.doc.createElement (EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER); 
     194            term_elem.appendChild (equiv_term_list); 
     195             
     196            long total_term_freq = 0; 
     197            for (int et = 0; et < equiv_terms.size (); et++) { 
     198                MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get (et); 
     199                 
     200                Element equiv_term_elem = this.doc.createElement (GSXML.TERM_ELEM); 
     201                equiv_term_elem.setAttribute (GSXML.NAME_ATT, equiv_term_info.term_); 
     202                equiv_term_elem.setAttribute (NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_); 
     203                equiv_term_elem.setAttribute (FREQ_ATT, "" + equiv_term_info.term_freq_); 
     204                equiv_term_list.appendChild (equiv_term_elem); 
     205                 
     206                total_term_freq += equiv_term_info.term_freq_; 
     207            } 
     208             
     209            term_elem.setAttribute (FREQ_ATT, "" + total_term_freq); 
     210            term_list.appendChild (term_elem); 
     211        } 
     212        return result; 
     213    } 
     214     
    217215    // should probably use a list rather than map 
    218216    protected boolean setStandardQueryParams(HashMap params) 
     
    247245    return true; 
    248246    } 
    249  
    250  
     247     
     248     
    251249} 
    252250