Changeset 12429


Ignore:
Timestamp:
2006-08-10T16:59:21+12:00 (18 years ago)
Author:
mdewsnip
Message:

Changed the "-filter" argument to use a general Lucene QueryFilter, which means that a filter can be specified in exactly the same way as a query.

Location:
trunk
Files:
3 edited

Legend:

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

    r12418 r12429  
    99
    1010
    11 import java.io.BufferedReader;
    12 import java.io.InputStreamReader;
    13 import java.io.IOException;
    14 import java.util.Collections;
    15 import java.util.HashMap;
    16 import java.util.HashSet;
    17 import java.util.Iterator;
    18 import java.util.Set;
     11import java.io.*;
     12import java.util.*;
    1913
    2014import org.apache.lucene.analysis.Analyzer;
     
    3226import org.apache.lucene.search.IndexSearcher;
    3327import org.apache.lucene.search.Query;
     28import org.apache.lucene.search.QueryFilter;
    3429import org.apache.lucene.search.RangeFilter;
    3530import org.apache.lucene.search.Searcher;
     
    5045        IndexReader reader = ((IndexSearcher) searcher).getIndexReader();
    5146
     47        // Create one query parser with the standard set of stop words, and one with none
     48        QueryParser query_parser = new QueryParser("TX", new StandardAnalyzer());
     49        QueryParser query_parser_no_stop_words = new QueryParser("TX", new StandardAnalyzer(new String[] { }));
     50
    5251        Sort sorter = new Sort();
    53         Filter filter = null;
     52        QueryFilter filter = null;
    5453            boolean fuzzy = false;
    5554
     
    6766                        {
    6867                            i++;
    69                 filter = parseFilterString(args[i]);
     68                try {
     69                filter = new QueryFilter(query_parser.parse(args[i]));
     70                }
     71                catch (ParseException exception) {
     72                exception.printStackTrace();
     73                }
    7074                        }
    7175                    if (args[i].equals("-dco"))
     
    7983                        }
    8084                }
    81 
    82         // Create one query parser with the standard set of stop words, and one with none
    83         QueryParser query_parser = new QueryParser("TX", new StandardAnalyzer());
    84         QueryParser query_parser_no_stop_words = new QueryParser("TX", new StandardAnalyzer(new String[] { }));
    8585
    8686        // Lucene does "OR" queries by default; do an "AND" query if specified
     
    227227
    228228
    229     private static Filter parseFilterString(String filter_string)
    230     {
    231     // Range filters
    232     if (filter_string.matches("(.*):[\\{\\[](.+) TO (.+)[\\}\\]]")) {
    233         String field_name = filter_string.substring(0, filter_string.indexOf(":"));
    234         boolean include_lower = (filter_string.charAt(filter_string.indexOf(":") + 1) == '[');
    235         String lower_term = filter_string.substring(filter_string.indexOf(":") + 2, filter_string.indexOf(" TO "));
    236         String upper_term = filter_string.substring(filter_string.indexOf(" TO ") + " TO ".length(), filter_string.length() - 1);
    237         boolean include_upper = (filter_string.charAt(filter_string.length() - 1) == ']');
    238         return new RangeFilter(field_name, lower_term, upper_term, include_lower, include_upper);
    239     }
    240 
    241     System.err.println("Error: Could not understand filter string \"" + filter_string + "\"");
    242     return null;
    243     }
    244 
    245 
    246229    private static Query parseQuery(IndexReader reader, QueryParser query_parser, String query_string, boolean fuzzy)
    247230    throws java.io.IOException, org.apache.lucene.queryParser.ParseException
  • trunk/indexers/lucene-gs/src/org/greenstone/LuceneWrapper/GS2LuceneQuery.java

    r12418 r12429  
    99
    1010
    11 import java.io.BufferedReader;
    12 import java.io.InputStreamReader;
    13 import java.io.IOException;
    14 import java.util.Collections;
    15 import java.util.HashMap;
    16 import java.util.HashSet;
    17 import java.util.Iterator;
    18 import java.util.Set;
     11import java.io.*;
     12import java.util.*;
    1913
    2014import org.apache.lucene.analysis.Analyzer;
     
    3226import org.apache.lucene.search.IndexSearcher;
    3327import org.apache.lucene.search.Query;
     28import org.apache.lucene.search.QueryFilter;
    3429import org.apache.lucene.search.RangeFilter;
    3530import org.apache.lucene.search.Searcher;
     
    5045        IndexReader reader = ((IndexSearcher) searcher).getIndexReader();
    5146
     47        // Create one query parser with the standard set of stop words, and one with none
     48        QueryParser query_parser = new QueryParser("TX", new StandardAnalyzer());
     49        QueryParser query_parser_no_stop_words = new QueryParser("TX", new StandardAnalyzer(new String[] { }));
     50
    5251        Sort sorter = new Sort();
    53         Filter filter = null;
     52        QueryFilter filter = null;
    5453            boolean fuzzy = false;
    5554
     
    6766                        {
    6867                            i++;
    69                 filter = parseFilterString(args[i]);
     68                try {
     69                filter = new QueryFilter(query_parser.parse(args[i]));
     70                }
     71                catch (ParseException exception) {
     72                exception.printStackTrace();
     73                }
    7074                        }
    7175                    if (args[i].equals("-dco"))
     
    7983                        }
    8084                }
    81 
    82         // Create one query parser with the standard set of stop words, and one with none
    83         QueryParser query_parser = new QueryParser("TX", new StandardAnalyzer());
    84         QueryParser query_parser_no_stop_words = new QueryParser("TX", new StandardAnalyzer(new String[] { }));
    8585
    8686        // Lucene does "OR" queries by default; do an "AND" query if specified
     
    227227
    228228
    229     private static Filter parseFilterString(String filter_string)
    230     {
    231     // Range filters
    232     if (filter_string.matches("(.*):[\\{\\[](.+) TO (.+)[\\}\\]]")) {
    233         String field_name = filter_string.substring(0, filter_string.indexOf(":"));
    234         boolean include_lower = (filter_string.charAt(filter_string.indexOf(":") + 1) == '[');
    235         String lower_term = filter_string.substring(filter_string.indexOf(":") + 2, filter_string.indexOf(" TO "));
    236         String upper_term = filter_string.substring(filter_string.indexOf(" TO ") + " TO ".length(), filter_string.length() - 1);
    237         boolean include_upper = (filter_string.charAt(filter_string.length() - 1) == ']');
    238         return new RangeFilter(field_name, lower_term, upper_term, include_lower, include_upper);
    239     }
    240 
    241     System.err.println("Error: Could not understand filter string \"" + filter_string + "\"");
    242     return null;
    243     }
    244 
    245 
    246229    private static Query parseQuery(IndexReader reader, QueryParser query_parser, String query_string, boolean fuzzy)
    247230    throws java.io.IOException, org.apache.lucene.queryParser.ParseException
Note: See TracChangeset for help on using the changeset viewer.