Ignore:
Timestamp:
2007-01-10T17:07:47+13:00 (17 years ago)
Author:
kjdon
Message:

in GS3 we will use GS2LuceneQuery directly, so moved most of the functionality out of the static methods and into instance methods. main() is now pretty much just a wrapper around the class. Caching has been left in the command line version for now - maybe should be in the class version too??

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/LuceneWrap/GS2LuceneQuery.java

    r13054 r13557  
    1 /**
    2  *
    3  * @author  [email protected]
    4  * @author  [email protected]
    5  * @author  [email protected]
    6  * @author  [email protected]
    7  * @version
    8  */
    9 
     1/**********************************************************************
     2 *
     3 * GS2LuceneQuery.java
     4 *
     5 * Copyright 2004 The New Zealand Digital Library Project
     6 *
     7 * A component of the Greenstone digital library software
     8 * from the New Zealand Digital Library Project at the
     9 * University of Waikato, New Zealand.
     10 *
     11 * This program is free software; you can redistribute it and/or modify
     12 * it under the terms of the GNU General Public License as published by
     13 * the Free Software Foundation; either version 2 of the License, or
     14 * (at your option) any later version.
     15 *
     16 * This program is distributed in the hope that it will be useful,
     17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 * GNU General Public License for more details.
     20 *
     21 * You should have received a copy of the GNU General Public License
     22 * along with this program; if not, write to the Free Software
     23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     24 *
     25 *********************************************************************/
    1026package org.nzdl.gsdl.LuceneWrap;
    1127
     
    3854public class GS2LuceneQuery
    3955{
     56
     57
    4058    static private String TEXTFIELD = "TX";
    41 
    42     // Fairly self-explanatory I should hope
    43     static private boolean query_result_caching_enabled = false;
    4459
    4560    // Use the standard set of English stop words by default
    4661    static private String[] stop_words = StandardAnalyzer.STOP_WORDS;
    4762
    48     // Command-line options
    49     static private String fuzziness = null;
    50     static private String filter_string = null;
    51     static private Filter filter = null;
    52     static private String sort_string = null;
    53     static private Sort sorter = new Sort();
    54     static private String default_conjuction_operator = "OR";
    55     static private int start_results = 1;
    56     static private int end_results = Integer.MAX_VALUE;
    57 
    58 
    59     static public void main (String args[])
    60     {
    61     if (args.length == 0) {
    62         System.out.println("Usage: GS2LuceneQuery <index directory> [-fuzziness value] [-filter filter_string] [-sort sort_field] [-dco AND|OR] [-startresults number -endresults number] [query]");
    63         return;
    64     }
    65 
     63    private String full_indexdir="";
     64    private String default_conjunction_operator = "OR";
     65    private String fuzziness = null;
     66    private String sort_field = null;
     67    private Sort sorter=new Sort();
     68    private String filter_string = null;
     69    private Filter filter = null;
     70    private int start_results=1;
     71    private int end_results=Integer.MAX_VALUE;
     72
     73    private QueryParser query_parser = null;
     74    private QueryParser query_parser_no_stop_words = null;
     75    private Searcher searcher = null;
     76    private IndexReader reader = null;
     77   
     78    public GS2LuceneQuery() {
     79
     80    // Create one query parser with the standard set of stop words, and one with none
     81
     82    query_parser = new QueryParser(TEXTFIELD, new StandardAnalyzer(stop_words));
     83        query_parser_no_stop_words = new QueryParser(TEXTFIELD, new StandardAnalyzer(new String[] { }));
     84    }
     85   
     86   
     87    public boolean initialise() {
     88
     89        if (full_indexdir==null || full_indexdir.length()==-1){
     90        System.out.println("Index directory is not indicated ");
     91        return false;
     92        }
    6693        try {
    67         String index_directory = args[0];
    68         Searcher searcher = new IndexSearcher(index_directory);
    69         IndexReader reader = ((IndexSearcher) searcher).getIndexReader();
    70 
    71         // Prepare the index cache directory, if query result caching is enabled
    72         if (query_result_caching_enabled) {
    73         // Make the index cache directory if it doesn't already exist
    74         File index_cache_directory = new File(index_directory, "cache");
    75         if (!index_cache_directory.exists()) {
    76             index_cache_directory.mkdir();
    77         }
    78 
    79         // Disable caching if the index cache directory isn't available
    80         if (!index_cache_directory.exists() || !index_cache_directory.isDirectory()) {
    81             query_result_caching_enabled = false;
    82         }
    83         }
    84 
    85         // Create one query parser with the standard set of stop words, and one with none
    86         QueryParser query_parser = new QueryParser(TEXTFIELD, new StandardAnalyzer(stop_words));
    87         QueryParser query_parser_no_stop_words = new QueryParser(TEXTFIELD, new StandardAnalyzer(new String[] { }));
    88 
    89         String query_string = null;
    90 
    91         // Parse the command-line arguments
    92             for (int i = 1; i < args.length; i++) {
    93         if (args[i].equals("-sort")) {
    94             i++;
    95             sort_string = args[i];
    96             sorter = new Sort(sort_string);
    97         }
    98         else if (args[i].equals("-filter")) {
    99             i++;
    100             filter_string = args[i];
    101             filter = parseFilterString(filter_string);
    102         }
    103         else if (args[i].equals("-dco")) {
    104             i++;
    105             default_conjuction_operator = args[i];
    106         }
    107         else if (args[i].equals("-fuzziness")) {
    108             i++;
    109             fuzziness = args[i];
    110         }
    111         else if (args[i].equals("-startresults")) {
    112             i++;
    113             if (args[i].matches("\\d+")) {
    114             start_results = Integer.parseInt(args[i]);
    115             }
    116         }
    117         else if (args[i].equals("-endresults")) {
    118             i++;
    119             if (args[i].matches("\\d+")) {
    120             end_results = Integer.parseInt(args[i]);
    121             }
    122         }
    123         else {
    124             query_string = args[i];
    125         }
    126         }
    127        
    128         // Lucene does "OR" queries by default; do an "AND" query if specified
    129         if (default_conjuction_operator.equals("AND")) {
    130         query_parser.setDefaultOperator(query_parser.AND_OPERATOR);
    131         query_parser_no_stop_words.setDefaultOperator(query_parser.AND_OPERATOR);
    132         }
    133 
    134         // The query string has been specified as a command-line argument
    135         if (query_string != null) {
    136         runQuery(index_directory, searcher, reader, query_parser, query_parser_no_stop_words, query_string);
    137         }
    138 
    139         // Read queries from STDIN
    140         else {
    141         BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    142         while (true) {
    143             // Read the query from STDIN
    144             query_string = in.readLine();
    145             if (query_string == null || query_string.length() == -1) {
    146             break;
    147             }
    148 
    149             runQuery(index_directory, searcher, reader, query_parser, query_parser_no_stop_words, query_string);
    150         }
    151         }
    152 
    153         searcher.close();
     94            searcher = new IndexSearcher(full_indexdir);
     95            reader = ((IndexSearcher) searcher).getIndexReader();
     96       
    15497    }
    15598    catch (IOException exception) {
    156         exception.printStackTrace();
    157     }
    158     }
    159 
    160 
    161     private static void runQuery(String index_directory, Searcher searcher, IndexReader reader, QueryParser query_parser, QueryParser query_parser_no_stop_words, String query_string)
    162     throws IOException
    163     {
    164     StringBuffer query_results_xml = new StringBuffer();
    165 
    166     // Check if this query result has been cached from a previous search (if it's enabled)
    167     File query_result_cache_file = null;
    168     if (query_result_caching_enabled) {
    169         // Generate the cache file name from the query options
    170         String query_result_cache_file_name = query_string + "-";
    171         query_result_cache_file_name += ((fuzziness != null) ? fuzziness : "") + "-";
    172         query_result_cache_file_name += ((filter_string != null) ? filter_string : "") + "-";
    173         query_result_cache_file_name += ((sort_string != null) ? sort_string : "") + "-";
    174         query_result_cache_file_name += default_conjuction_operator + "-";
    175         query_result_cache_file_name += start_results + "-" + end_results;
    176         query_result_cache_file_name = fileSafe(query_result_cache_file_name);
    177 
    178         // If the query result cache file exists, just return its contents and we're done
    179         File index_cache_directory = new File(index_directory, "cache");
    180         query_result_cache_file = new File(index_cache_directory, query_result_cache_file_name);
    181         if (query_result_cache_file.exists() && query_result_cache_file.isFile()) {
    182         FileInputStream fis = new FileInputStream(query_result_cache_file);
    183         InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    184         BufferedReader buffered_reader = new BufferedReader(isr);
    185         String line = "";
    186         while ((line = buffered_reader.readLine()) != null) {
    187             query_results_xml.append(line + "\n");
    188         }
    189         String query_results_xml_string = query_results_xml.toString();
    190         query_results_xml_string = query_results_xml_string.replaceFirst("cached=\"false\"", "cached=\"true\"");
    191         System.out.print(query_results_xml_string);
    192         return;
    193         }
    194     }
    195 
    196     query_results_xml.append("<ResultSet cached=\"false\">\n");
    197     query_results_xml.append("  <QueryString>" + xmlSafe(query_string) + "</QueryString>\n");
    198     if (filter != null) {
    199         query_results_xml.append("  <FilterString>" + filter.toString() + "</FilterString>\n");
    200     }
    201 
    202     try {
    203         Query query_including_stop_words = parseQuery(reader, query_parser_no_stop_words, query_string, fuzziness);
     99            exception.printStackTrace();
     100        return false;
     101        }
     102    return true;
     103
     104    }
     105   
     106    public LuceneQueryResult runQuery(String query_string) {
     107   
     108    if (query_string == null || query_string.equals("")) {
     109        System.out.println("The query word is not indicated ");
     110        return null;
     111    }
     112
     113    LuceneQueryResult lucene_query_result=new LuceneQueryResult();
     114    lucene_query_result.clear();
     115       
     116    try {               
     117        Query query_including_stop_words = query_parser_no_stop_words.parse(query_string);
    204118        query_including_stop_words = query_including_stop_words.rewrite(reader);
    205 
     119       
    206120        Query query = parseQuery(reader, query_parser, query_string, fuzziness);
    207121        query = query.rewrite(reader);
    208 
    209         // Return the list of expanded query terms and their frequencies
     122       
     123        // Get the list of expanded query terms and their frequencies
     124        // num docs matching, and total frequency       
    210125        HashSet terms = new HashSet();
    211126        query.extractTerms(terms);
    212         Iterator term_iterator = terms.iterator();
    213         query_results_xml.append("  <QueryTermsInfo num=\"" + terms.size() + "\"/>\n");
    214         while (term_iterator.hasNext()) {
    215         Term term = (Term) term_iterator.next();
    216 
     127       
     128        Iterator iter = terms.iterator();
     129        while (iter.hasNext()) {
     130           
     131        Term term = (Term) iter.next();
     132       
    217133        // Get the term frequency over all the documents
    218134        TermDocs term_docs = reader.termDocs(term);
    219135        int term_freq = term_docs.freq();
     136        int match_docs = 0;
     137        if (term_freq != 0) match_docs++;
    220138        while (term_docs.next()) {
    221139            term_freq += term_docs.freq();
    222         }
    223 
    224         // If you wanted to limit this to just text terms add
    225         // something like this:
    226         // if (term.field().equals(TEXTFIELD))
    227         query_results_xml.append("  <Term value=\"" + xmlSafe(term.text()) + "\" field=\"" + term.field() + "\" freq=\"" + term_freq + "\" />\n");
    228         }
    229 
    230         // Return the list of stop words removed from the query
     140            if (term_docs.freq()!= 0) {
     141            match_docs++;
     142            }
     143        }
     144
     145        // Create a term
     146        lucene_query_result.addTerm(term.text(), term.field(), match_docs, term_freq);
     147        }
     148   
     149        // Get the list of stop words removed from the query
    231150        HashSet terms_including_stop_words = new HashSet();
    232151        query_including_stop_words.extractTerms(terms_including_stop_words);
     
    235154        Term term = (Term) terms_including_stop_words_iter.next();
    236155        if (!terms.contains(term)) {
    237             query_results_xml.append("  <StopWord value=\"" + term.text() + "\"/>\n");
    238         }
    239         }
    240 
     156            lucene_query_result.addStopWord(term.text());
     157        }
     158        }
     159
     160        // do the query
    241161        // Simple case for getting all the matching documents
    242162        if (end_results == Integer.MAX_VALUE) {
    243163        // Perform the query (filter and sorter may be null)
    244164        Hits hits = searcher.search(query, filter, sorter);
    245         query_results_xml.append("  <MatchingDocsInfo num=\"" + hits.length() + "\"/>\n");
     165        lucene_query_result.setTotalDocs(hits.length());
    246166
    247167        // Output the matching documents
    248         query_results_xml.append("  <StartResults num=\"" + start_results + "\" />\n");
    249         query_results_xml.append("  <EndsResults num=\"" + hits.length() + "\" />\n");
     168        lucene_query_result.setStartResults(start_results);
     169        lucene_query_result.setEndResults(hits.length());
     170
    250171        for (int i = start_results; i <= hits.length(); i++) {
    251172            Document doc = hits.doc(i - 1);
    252             query_results_xml.append("  <Match id=\"" + doc.get("nodeID") + "\" />\n");
     173            lucene_query_result.addDoc(Long.parseLong(doc.get("nodeID").trim()), hits.score(i-1));
    253174        }
    254175        }
     
    258179        // Perform the query (filter may be null)
    259180        TopFieldDocs hits = searcher.search(query, filter, end_results, sorter);
    260         query_results_xml.append("  <MatchingDocsInfo num=\"" + hits.totalHits + "\"/>\n");
     181        lucene_query_result.setTotalDocs(hits.totalHits);
     182       
     183        lucene_query_result.setStartResults(start_results);
     184        lucene_query_result.setEndResults(end_results < hits.scoreDocs.length ? end_results: hits.scoreDocs.length);
    261185
    262186        // Output the matching documents
    263         query_results_xml.append("  <StartResults num=\"" + start_results + "\" />\n");
    264         query_results_xml.append("  <EndsResults num=\"" + end_results + "\" />\n");
    265187        for (int i = start_results; (i <= hits.scoreDocs.length && i <= end_results); i++) {
    266188            Document doc = reader.document(hits.scoreDocs[i - 1].doc);
    267             query_results_xml.append("  <Match id=\"" + doc.get("nodeID") + "\" />\n");
    268         }
    269         }
    270     }
     189            lucene_query_result.addDoc(Long.parseLong(doc.get("nodeID").trim()), hits.scoreDocs[i-1].score);
     190        }
     191        }
     192    }
     193   
    271194    catch (ParseException parse_exception) {
    272         query_results_xml.append("  <Error type=\"PARSE_EXCEPTION\"/>\n");
     195        lucene_query_result.setError(LuceneQueryResult.PARSE_ERROR);
    273196    }
    274197    catch (TooManyClauses too_many_clauses_exception) {
    275         query_results_xml.append("  <Error type=\"TOO_MANY_CLAUSES\"/>\n");
    276     }
    277 
    278     query_results_xml.append("</ResultSet>\n");
    279 
    280     System.out.print(query_results_xml);
    281 
    282     // Cache this query result, if desired
    283     if (query_result_caching_enabled) {
    284         FileWriter query_result_cache_file_writer = new FileWriter(query_result_cache_file);
    285         query_result_cache_file_writer.write(query_results_xml.toString());
    286         query_result_cache_file_writer.close();
    287     }
    288     }
    289 
    290 
    291     private static String fileSafe(String text)
    292     {
    293     StringBuffer file_safe_text = new StringBuffer();
    294     for (int i = 0; i < text.length(); i++) {
    295         char character = text.charAt(i);
    296         if ((character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '-') {
    297         file_safe_text.append(character);
    298         }
    299         else {
    300         file_safe_text.append('%');
    301         file_safe_text.append((int) character);
    302         }
    303     }
    304     return file_safe_text.toString();
    305     }
    306 
    307 
    308     private static String xmlSafe(String text) {
    309     text = text.replaceAll("&","&amp;amp;");
    310     text = text.replaceAll("<","&amp;lt;");
    311     text = text.replaceAll(">","&amp;gt;");
    312     text = text.replaceAll("'","&amp;#039;");
    313     text = text.replaceAll("\\\"","&amp;quot;");
    314     return text;
    315     }
    316 
    317 
    318     private static Query parseQuery(IndexReader reader, QueryParser query_parser, String query_string, String fuzziness)
     198        lucene_query_result.setError(LuceneQueryResult.TOO_MANY_CLAUSES_ERROR);
     199    }
     200    catch (IOException exception) {
     201        lucene_query_result.setError(LuceneQueryResult.IO_ERROR);
     202        exception.printStackTrace();
     203    }
     204
     205    return lucene_query_result;
     206    }
     207
     208    public void setDefaultConjunctionOperator(String default_conjunction_operator) {
     209    this.default_conjunction_operator = default_conjunction_operator.toUpperCase();
     210    if (default_conjunction_operator == "AND") {
     211        query_parser.setDefaultOperator(query_parser.AND_OPERATOR);
     212        query_parser_no_stop_words.setDefaultOperator(query_parser.AND_OPERATOR);
     213    } else { // default is OR
     214        query_parser.setDefaultOperator(query_parser.OR_OPERATOR);
     215        query_parser_no_stop_words.setDefaultOperator(query_parser.OR_OPERATOR);
     216    }
     217    }
     218   
     219    public String getDefaultConjunctionOperator() {
     220    return this.default_conjunction_operator;
     221    }
     222   
     223    public void setEndResults(int end_results) {
     224    this.end_results = end_results;
     225    }
     226    public int getEndResults() {
     227    return this.end_results;
     228    }
     229       
     230    public void setFilterString(String filter_string) {
     231    this.filter_string = filter_string;
     232    this.filter = parseFilterString(filter_string);
     233    }
     234    public String getFilterString() {
     235    return this.filter_string ;
     236    }
     237   
     238    public Filter getFilter() {
     239    return this.filter;
     240    }
     241
     242    public void setIndexDir(String full_indexdir) {
     243    this.full_indexdir = full_indexdir;
     244    }
     245   
     246    public void setFuzziness(String fuzziness) {
     247    this.fuzziness = fuzziness;
     248    }
     249    public String getFuzziness() {
     250    return this.fuzziness;
     251    }
     252   
     253    public void setSortField(String sort_field) {
     254    this.sort_field = sort_field;
     255    this.sorter = new Sort(sort_field);
     256    }
     257    public String getSortField() {
     258    return this.sort_field;
     259    }
     260       
     261    public void setStartResults(int start_results) {
     262    this.start_results = start_results;
     263    }
     264    public int getStartResults() {
     265    return this.start_results;
     266    }
     267       
     268    public void cleanUp() {
     269    try {
     270        searcher.close();
     271    } catch (IOException exception) {
     272        exception.printStackTrace();
     273    }
     274    }
     275
     276    private Query parseQuery(IndexReader reader, QueryParser query_parser, String query_string, String fuzziness)
    319277    throws java.io.IOException, org.apache.lucene.queryParser.ParseException
    320278    {
     
    337295    String query_prefix = query_string.substring(0, offset);
    338296    String query_suffix = query_string.substring(offset);
    339 
     297   
    340298    ///ystem.err.println("Prefix: " + query_prefix);
    341299    ///ystem.err.println("Suffix: " + query_suffix);
    342 
     300   
    343301    Query query = query_parser.parse(query_prefix);
    344302    query = query.rewrite(reader);
    345 
     303   
    346304    // If this is a fuzzy search, then we need to add the fuzzy
    347305    // flag to each of the query terms
    348306    if (fuzziness != null && query.toString().length() > 0) {
     307       
    349308        // Revert the query to a string
    350309        System.err.println("Rewritten query: " + query.toString());
    351310        // Search through the string for TX:<term> query terms
    352         // and append the ~ operator. Not that this search will
     311        // and append the ~ operator. Note that this search will
    353312        // not change phrase searches (TX:"<term> <term>") as
    354313        // fuzzy searching is not possible for these entries.
     
    358317        // 0 = BASE, 1 = SEEN_T, 2 = SEEN_TX, 3 = SEEN_TX:
    359318        int s = 0; // State
    360         while (o < mutable_query_string.length()) {
     319        while(o < mutable_query_string.length()) {
    361320        char c = mutable_query_string.charAt(o);
    362321        if (s == 0 && c == TEXTFIELD.charAt(0)) {
     
    403362        // term - then we just found it!
    404363        if (s == 3) {
     364           
    405365        mutable_query_string.append('~' + fuzziness);
    406366        }
     
    416376    }
    417377
    418 
    419   /**
    420    *  @todo Michael to comment
    421    */
    422   private static Filter parseFilterString(String filter_string)
    423   {
    424     Filter result = null;
    425     Pattern pattern = Pattern.compile("\\s*\\+(\\w+)\\:([\\{\\[])(\\d+)\\s+TO\\s+(\\d+)([\\}\\]])\\s*");
    426     Matcher matcher = pattern.matcher(filter_string);
    427     if (matcher.matches()) {
    428         String field_name = matcher.group(1);
    429         boolean include_lower = matcher.group(2).equals("[");
    430         String lower_term = matcher.group(3);
    431         String upper_term = matcher.group(4);
    432         boolean include_upper = matcher.group(5).equals("]");
    433         result = new RangeFilter(field_name, lower_term, upper_term, include_lower, include_upper);
    434     }
    435     else {
    436         System.err.println("Error: Could not understand filter string \"" + filter_string + "\"");
    437     }
    438     return result;
    439   }
    440   /** parseFilterString() **/
     378    private Filter parseFilterString(String filter_string)
     379    {
     380    Filter result = null;
     381    Pattern pattern = Pattern.compile("\\s*\\+(\\w+)\\:([\\{\\[])(\\d+)\\s+TO\\s+(\\d+)([\\}\\]])\\s*");
     382    Matcher matcher = pattern.matcher(filter_string);
     383    if (matcher.matches()) {
     384        String field_name = matcher.group(1);
     385        boolean include_lower = matcher.group(2).equals("[");
     386        String lower_term = matcher.group(3);
     387        String upper_term = matcher.group(4);
     388        boolean include_upper = matcher.group(5).equals("]");
     389        result = new RangeFilter(field_name, lower_term, upper_term, include_lower, include_upper);
     390    }
     391    else {
     392        System.err.println("Error: Could not understand filter string \"" + filter_string + "\"");
     393    }
     394    return result;
     395    }
     396
     397   
     398    /** command line program and auxiliary methods */
     399
     400    // Fairly self-explanatory I should hope
     401    static private boolean query_result_caching_enabled = false;
     402
     403    static public void main (String args[])
     404    {
     405    if (args.length == 0) {
     406        System.out.println("Usage: GS2LuceneQuery <index directory> [-fuzziness value] [-filter filter_string] [-sort sort_field] [-dco AND|OR] [-startresults number -endresults number] [query]");
     407        return;
     408    }
     409
     410    try {
     411        String index_directory = args[0];
     412       
     413        GS2LuceneQuery queryer = new GS2LuceneQuery();
     414        queryer.setIndexDir(index_directory);
     415
     416        // Prepare the index cache directory, if query result caching is enabled
     417        if (query_result_caching_enabled) {
     418        // Make the index cache directory if it doesn't already exist
     419        File index_cache_directory = new File(index_directory, "cache");
     420        if (!index_cache_directory.exists()) {
     421            index_cache_directory.mkdir();
     422        }
     423
     424        // Disable caching if the index cache directory isn't available
     425        if (!index_cache_directory.exists() || !index_cache_directory.isDirectory()) {
     426            query_result_caching_enabled = false;
     427        }
     428        }
     429
     430        String query_string = null;
     431
     432        // Parse the command-line arguments
     433            for (int i = 1; i < args.length; i++) {
     434        if (args[i].equals("-sort")) {
     435            i++;
     436            queryer.setSortField(args[i]);
     437        }
     438        else if (args[i].equals("-filter")) {
     439            i++;
     440            queryer.setFilterString(args[i]);
     441        }
     442        else if (args[i].equals("-dco")) {
     443            i++;
     444            queryer.setDefaultConjunctionOperator(args[i]);
     445        }
     446        else if (args[i].equals("-fuzziness")) {
     447            i++;
     448            queryer.setFuzziness(args[i]);
     449        }
     450        else if (args[i].equals("-startresults")) {
     451            i++;
     452            if (args[i].matches("\\d+")) {
     453            queryer.setStartResults(Integer.parseInt(args[i]));
     454            }
     455        }
     456        else if (args[i].equals("-endresults")) {
     457            i++;
     458            if (args[i].matches("\\d+")) {
     459            queryer.setEndResults(Integer.parseInt(args[i]));
     460            }
     461        }
     462        else {
     463            query_string = args[i];
     464        }
     465        }
     466       
     467        if (!queryer.initialise()) {
     468        return;
     469        }
     470       
     471        // The query string has been specified as a command-line argument
     472        if (query_string != null) {
     473        runQueryCaching(index_directory, queryer, query_string);
     474        }
     475
     476        // Read queries from STDIN
     477        else {
     478        BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
     479        while (true) {
     480            // Read the query from STDIN
     481            query_string = in.readLine();
     482            if (query_string == null || query_string.length() == -1) {
     483            break;
     484            }
     485            runQueryCaching(index_directory, queryer, query_string);
     486           
     487        }
     488        }
     489        queryer.cleanUp();
     490    }
     491    catch (IOException exception) {
     492        exception.printStackTrace();
     493    }
     494    }
     495
     496    private static void runQueryCaching(String index_directory, GS2LuceneQuery queryer, String query_string)
     497    throws IOException
     498    {
     499    StringBuffer query_results_xml = new StringBuffer();
     500
     501    // Check if this query result has been cached from a previous search (if it's enabled)
     502    File query_result_cache_file = null;
     503    if (query_result_caching_enabled) {
     504        // Generate the cache file name from the query options
     505        String query_result_cache_file_name = query_string + "-";
     506        String fuzziness = queryer.getFuzziness();
     507        query_result_cache_file_name += ((fuzziness != null) ? fuzziness : "") + "-";
     508        String filter_string = queryer.getFilterString();
     509        query_result_cache_file_name += ((filter_string != null) ? filter_string : "") + "-";
     510        String sort_string = queryer.getSortField();
     511        query_result_cache_file_name += ((sort_string != null) ? sort_string : "") + "-";
     512        String default_conjunction_operator = queryer.getDefaultConjunctionOperator();
     513        query_result_cache_file_name += default_conjunction_operator + "-";
     514        int start_results = queryer.getStartResults();
     515        int end_results = queryer.getEndResults();
     516        query_result_cache_file_name += start_results + "-" + end_results;
     517        query_result_cache_file_name = fileSafe(query_result_cache_file_name);
     518
     519        // If the query result cache file exists, just return its contents and we're done
     520        File index_cache_directory = new File(index_directory, "cache");
     521        query_result_cache_file = new File(index_cache_directory, query_result_cache_file_name);
     522        if (query_result_cache_file.exists() && query_result_cache_file.isFile()) {
     523        FileInputStream fis = new FileInputStream(query_result_cache_file);
     524        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
     525        BufferedReader buffered_reader = new BufferedReader(isr);
     526        String line = "";
     527        while ((line = buffered_reader.readLine()) != null) {
     528            query_results_xml.append(line + "\n");
     529        }
     530        String query_results_xml_string = query_results_xml.toString();
     531        query_results_xml_string = query_results_xml_string.replaceFirst("cached=\"false\"", "cached=\"true\"");
     532        System.out.print(query_results_xml_string);
     533        return;
     534        }
     535    }
     536   
     537    // not cached
     538    query_results_xml.append("<ResultSet cached=\"false\">\n");
     539    query_results_xml.append("<QueryString>" + LuceneQueryResult.xmlSafe(query_string) + "</QueryString>\n");
     540    Filter filter = queryer.getFilter();
     541    if (filter != null) {
     542        query_results_xml.append("<FilterString>" + filter.toString() + "</FilterString>\n");
     543    }
     544   
     545    LuceneQueryResult query_result = queryer.runQuery(query_string);
     546    if (query_result == null) {
     547        System.err.println("Couldn't run the query");
     548        return;
     549    }
     550   
     551    if (query_result.getError() != LuceneQueryResult.NO_ERROR) {
     552        query_results_xml.append("<Error type=\""+query_result.getErrorString()+"\" />\n");
     553    } else {
     554        query_results_xml.append(query_result.getXMLString());
     555    }
     556    query_results_xml.append("</ResultSet>\n");
     557
     558    System.out.print(query_results_xml);
     559
     560    // Cache this query result, if desired
     561    if (query_result_caching_enabled) {
     562        FileWriter query_result_cache_file_writer = new FileWriter(query_result_cache_file);
     563        query_result_cache_file_writer.write(query_results_xml.toString());
     564        query_result_cache_file_writer.close();
     565    }
     566    }
     567   
     568    private static String fileSafe(String text)
     569    {
     570    StringBuffer file_safe_text = new StringBuffer();
     571    for (int i = 0; i < text.length(); i++) {
     572        char character = text.charAt(i);
     573        if ((character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '-') {
     574        file_safe_text.append(character);
     575        }
     576        else {
     577        file_safe_text.append('%');
     578        file_safe_text.append((int) character);
     579        }
     580    }
     581    return file_safe_text.toString();
     582    }
     583
     584   
    441585}
     586
     587
Note: See TracChangeset for help on using the changeset viewer.