Save This Page
Home » lucene-2.3.2-src » org.apache » lucene » queryParser » [javadoc | source]
org.apache.lucene.queryParser
public class: QueryParser [javadoc | source]
java.lang.Object
   org.apache.lucene.queryParser.QueryParser

All Implemented Interfaces:
    QueryParserConstants

Direct Known Subclasses:
    QPTestParser, AnalyzingQueryParser, MultiFieldQueryParser

This class is generated by JavaCC. The most important method is #parse(String) . The syntax for query strings is as follows: A Query is a series of clauses. A clause may be prefixed by: A clause may be either: Thus, in BNF, the query grammar is:
Query ::= ( Clause )*
Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )

Examples of appropriately formatted queries can be found in the query syntax documentation.

In RangeQuery s, QueryParser tries to detect date values, e.g. date:[6/1/2005 TO 6/4/2005] produces a range query that searches for "date" fields between 2005-06-01 and 2005-06-04. Note that the format of the accepted input depends on the locale . By default a date is converted into a search term using the deprecated DateField for compatibility reasons. To use the new DateTools to convert dates, a org.apache.lucene.document.DateTools.Resolution has to be set.

The date resolution that shall be used for RangeQueries can be set using #setDateResolution(DateTools.Resolution) or #setDateResolution(String, DateTools.Resolution) . The former sets the default date resolution for all fields, whereas the latter can be used to set field specific date resolutions. Field specific date resolutions take, if set, precedence over the default date resolution.

If you use neither DateField nor DateTools in your index, you can create your own query parser that inherits QueryParser and overwrites #getRangeQuery(String, String, String, boolean) to use a different method for date conversion.

Note that QueryParser is not thread-safe.

Nested Class Summary:
public static final class  QueryParser.Operator  The default operator for parsing queries. Use {@link QueryParser#setDefaultOperator} to change it. 
static final class  QueryParser.JJCalls   
Field Summary
public static final  QueryParser.Operator AND_OPERATOR    Alternative form of QueryParser.Operator.AND 
public static final  QueryParser.Operator OR_OPERATOR    Alternative form of QueryParser.Operator.OR 
 boolean lowercaseExpandedTerms     
 boolean useOldRangeQuery     
 boolean allowLeadingWildcard     
 boolean enablePositionIncrements     
 Analyzer analyzer     
 String field     
 int phraseSlop     
 float fuzzyMinSim     
 int fuzzyPrefixLength     
 Locale locale     
 DateTools.Resolution dateResolution     
 Map fieldToDateResolution     
public  QueryParserTokenManager token_source     
public  Token token     
public  Token jj_nt     
public  boolean lookingAhead     
Constructor:
 public QueryParser(CharStream stream) 
 public QueryParser(QueryParserTokenManager tm) 
 public QueryParser(String f,
    Analyzer a) 
    Constructs a query parser.
    Parameters:
    f - the default field for query terms.
    a - used to find terms in the query text.
Method from org.apache.lucene.queryParser.QueryParser Summary:
Clause,   Conjunction,   Modifiers,   Query,   ReInit,   ReInit,   Term,   TopLevelQuery,   addClause,   disable_tracing,   enable_tracing,   escape,   generateParseException,   getAllowLeadingWildcard,   getAnalyzer,   getBooleanQuery,   getBooleanQuery,   getDateResolution,   getDefaultOperator,   getEnablePositionIncrements,   getField,   getFieldQuery,   getFieldQuery,   getFuzzyMinSim,   getFuzzyPrefixLength,   getFuzzyQuery,   getLocale,   getLowercaseExpandedTerms,   getNextToken,   getPhraseSlop,   getPrefixQuery,   getRangeQuery,   getToken,   getUseOldRangeQuery,   getWildcardQuery,   main,   parse,   setAllowLeadingWildcard,   setDateResolution,   setDateResolution,   setDefaultOperator,   setEnablePositionIncrements,   setFuzzyMinSim,   setFuzzyPrefixLength,   setLocale,   setLowercaseExpandedTerms,   setPhraseSlop,   setUseOldRangeQuery
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.lucene.queryParser.QueryParser Detail:
 public final Query Clause(String field) throws ParseException 
 public final int Conjunction() throws ParseException 
 public final int Modifiers() throws ParseException 
 public final Query Query(String field) throws ParseException 
 public  void ReInit(CharStream stream) 
 public  void ReInit(QueryParserTokenManager tm) 
 public final Query Term(String field) throws ParseException 
 public final Query TopLevelQuery(String field) throws ParseException 
 protected  void addClause(Vector clauses,
    int conj,
    int mods,
    Query q) 
 public final  void disable_tracing() 
 public final  void enable_tracing() 
 public static String escape(String s) 
    Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.
 public ParseException generateParseException() 
 public boolean getAllowLeadingWildcard() 
 public Analyzer getAnalyzer() 
 protected Query getBooleanQuery(Vector clauses) throws ParseException 
    Factory method for generating query, given a set of clauses. By default creates a boolean query composed of clauses passed in. Can be overridden by extending classes, to modify query being returned.
 protected Query getBooleanQuery(Vector clauses,
    boolean disableCoord) throws ParseException 
    Factory method for generating query, given a set of clauses. By default creates a boolean query composed of clauses passed in. Can be overridden by extending classes, to modify query being returned.
 public DateTools.Resolution getDateResolution(String fieldName) 
    Returns the date resolution that is used by RangeQueries for the given field. Returns null, if no default or field specific date resolution has been set for the given field.
 public QueryParser.Operator getDefaultOperator() 
    Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
 public boolean getEnablePositionIncrements() 
 public String getField() 
 protected Query getFieldQuery(String field,
    String queryText) throws ParseException 
 protected Query getFieldQuery(String field,
    String queryText,
    int slop) throws ParseException 
    Base implementation delegates to #getFieldQuery(String,String) . This method may be overridden, for example, to return a SpanNearQuery instead of a PhraseQuery.
 public float getFuzzyMinSim() 
    Get the minimal similarity for fuzzy queries.
 public int getFuzzyPrefixLength() 
    Get the prefix length for fuzzy queries.
 protected Query getFuzzyQuery(String field,
    String termStr,
    float minSimilarity) throws ParseException 
    Factory method for generating a query (similar to #getWildcardQuery ). Called when parser parses an input term token that has the fuzzy suffix (~) appended.
 public Locale getLocale() 
    Returns current locale, allowing access by subclasses.
 public boolean getLowercaseExpandedTerms() 
 public final Token getNextToken() 
 public int getPhraseSlop() 
    Gets the default slop for phrases.
 protected Query getPrefixQuery(String field,
    String termStr) throws ParseException 
    Factory method for generating a query (similar to #getWildcardQuery ). Called when parser parses an input term token that uses prefix notation; that is, contains a single '*' wildcard character as its last character. Since this is a special case of generic wildcard term, and such a query can be optimized easily, this usually results in a different query object.

    Depending on settings, a prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.

    Can be overridden by extending classes, to provide custom handling for wild card queries, which may be necessary due to missing analyzer calls.

 protected Query getRangeQuery(String field,
    String part1,
    String part2,
    boolean inclusive) throws ParseException 
 public final Token getToken(int index) 
 public boolean getUseOldRangeQuery() 
 protected Query getWildcardQuery(String field,
    String termStr) throws ParseException 
    Factory method for generating a query. Called when parser parses an input term token that contains one or more wildcard characters (? and *), but is not a prefix term token (one that has just a single * character at the end)

    Depending on settings, prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.

    Can be overridden by extending classes, to provide custom handling for wildcard queries, which may be necessary due to missing analyzer calls.

 public static  void main(String[] args) throws Exception 
 public Query parse(String query) throws ParseException 
 public  void setAllowLeadingWildcard(boolean allowLeadingWildcard) 
    Set to true to allow leading wildcard characters.

    When set, * or ? are allowed as the first character of a PrefixQuery and WildcardQuery. Note that this can produce very slow queries on big indexes.

    Default: false.

 public  void setDateResolution(DateTools.Resolution dateResolution) 
 public  void setDateResolution(String fieldName,
    DateTools.Resolution dateResolution) 
    Sets the date resolution used by RangeQueries for a specific field.
 public  void setDefaultOperator(QueryParser.Operator op) 
    Sets the boolean operator of the QueryParser. In default mode (OR_OPERATOR) terms without any modifiers are considered optional: for example capital of Hungary is equal to capital OR of OR Hungary.
    In AND_OPERATOR mode terms are considered to be in conjuction: the above mentioned query is parsed as capital AND of AND Hungary
 public  void setEnablePositionIncrements(boolean enable) 
    Set to true to enable position increments in result query.

    When set, result phrase and multi-phrase queries will be aware of position increments. Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token.

    Default: false.

 public  void setFuzzyMinSim(float fuzzyMinSim) 
    Set the minimum similarity for fuzzy queries. Default is 0.5f.
 public  void setFuzzyPrefixLength(int fuzzyPrefixLength) 
    Set the prefix length for fuzzy queries. Default is 0.
 public  void setLocale(Locale locale) 
    Set locale used by date range parsing.
 public  void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms) 
    Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default is true.
 public  void setPhraseSlop(int phraseSlop) 
    Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.
 public  void setUseOldRangeQuery(boolean useOldRangeQuery) 
    By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery for range queries. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of range terms unduly influence score c) avoids any "TooManyBooleanClauses" exception. However, if your application really needs to use the old-fashioned RangeQuery and the above points are not required then set this option to true Default is false.