org.apache.lucene.queryParser

Class QueryParser

Implemented Interfaces:
QueryParserConstants
Known Direct Subclasses:
MultiFieldQueryParser

public class QueryParser
extends java.lang.Object
implements QueryParserConstants

This class is generated by JavaCC. The only method that clients should need to call is parse(). 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 test cases.

Authors:
Brian Goetz
Peter Halacsy
Tatu Saloranta

Field Summary

static int
DEFAULT_OPERATOR_AND
static int
DEFAULT_OPERATOR_OR
Token
jj_nt
boolean
lookingAhead
Token
token
QueryParserTokenManager
token_source

Fields inherited from interface org.apache.lucene.queryParser.QueryParserConstants

AND, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, RANGEEX_END, RANGEEX_GOOP, RANGEEX_QUOTED, RANGEEX_START, RANGEEX_TO, RANGEIN_END, RANGEIN_GOOP, RANGEIN_QUOTED, RANGEIN_START, RANGEIN_TO, RPAREN, RangeEx, RangeIn, TERM, WILDTERM, _ESCAPED_CHAR, _NUM_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, tokenImage

Constructor Summary

QueryParser(String f, Analyzer a)
Constructs a query parser.
QueryParser(CharStream stream)
QueryParser(QueryParserTokenManager tm)

Method Summary

Query
Clause(String field)
int
Conjunction()
int
Modifiers()
Query
Query(String field)
void
ReInit(CharStream stream)
void
ReInit(QueryParserTokenManager tm)
Query
Term(String field)
protected void
addClause(Vector clauses, int conj, int mods, Query q)
void
disable_tracing()
void
enable_tracing()
static String
escape(String s)
Returns a String where those characters that QueryParser expects to be escaped are escaped, i.e.
ParseException
generateParseException()
Analyzer
getAnalyzer()
protected Query
getBooleanQuery(Vector clauses)
Factory method for generating query, given a set of clauses.
String
getField()
protected Query
getFieldQuery(String field, String queryText)
protected Query
getFieldQuery(String field, String queryText, int slop)
Base implementation delegates to getFieldQuery(String,String).
protected Query
getFieldQuery(String field, Analyzer analyzer, String queryText)
Note that parameter analyzer is ignored.
protected Query
getFieldQuery(String field, Analyzer analyzer, String queryText, int slop)
Base implementation delegates to getFieldQuery(String,Analyzer,String).
float
getFuzzyMinSim()
Get the default minimal similarity for fuzzy queries.
protected Query
getFuzzyQuery(String field, String termStr)
Factory method for generating a query (similar to (getWildcardQuery(String,String)).
protected Query
getFuzzyQuery(String field, String termStr, float minSimilarity)
Factory method for generating a query (similar to (getWildcardQuery(String,String)).
Locale
getLocale()
Returns current locale, allowing access by subclasses.
boolean
getLowercaseWildcardTerms()
Token
getNextToken()
int
getOperator()
Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND or DEFAULT_OPERATOR_OR.
int
getPhraseSlop()
Gets the default slop for phrases.
protected Query
getPrefixQuery(String field, String termStr)
Factory method for generating a query (similar to (getWildcardQuery(String,String)).
protected Query
getRangeQuery(String field, String part1, String part2, boolean inclusive)
protected Query
getRangeQuery(String field, Analyzer analyzer, String part1, String part2, boolean inclusive)
Note that parameter analyzer is ignored.
Token
getToken(int index)
protected Query
getWildcardQuery(String field, String termStr)
Factory method for generating a query.
static void
main(String[] args)
Query
parse(String query)
Parses a query string, returning a Query.
static Query
parse(String query, String field, Analyzer analyzer)
Parses a query string, returning a Query.
void
setFuzzyMinSim(float fuzzyMinSim)
Set the default minimum similarity for fuzzy queries.
void
setLocale(Locale locale)
Set locale used by date range parsing.
void
setLowercaseWildcardTerms(boolean lowercaseWildcardTerms)
void
setOperator(int operator)
Sets the boolean operator of the QueryParser.
void
setPhraseSlop(int phraseSlop)
Sets the default slop for phrases.

Field Details

DEFAULT_OPERATOR_AND

public static final int DEFAULT_OPERATOR_AND
Field Value:
1

DEFAULT_OPERATOR_OR

public static final int DEFAULT_OPERATOR_OR
Field Value:
0

jj_nt

public Token jj_nt

lookingAhead

public boolean lookingAhead

token

public Token token

token_source

public QueryParserTokenManager token_source

Constructor Details

QueryParser

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.

QueryParser

public QueryParser(CharStream stream)

QueryParser

public QueryParser(QueryParserTokenManager tm)

Method Details

Clause

public final Query Clause(String field)
            throws ParseException

Conjunction

public final int Conjunction()
            throws ParseException

Modifiers

public final int Modifiers()
            throws ParseException

Query

public final Query Query(String field)
            throws ParseException

ReInit

public void ReInit(CharStream stream)

ReInit

public void ReInit(QueryParserTokenManager tm)

Term

public final Query Term(String field)
            throws ParseException

addClause

protected void addClause(Vector clauses,
                         int conj,
                         int mods,
                         Query q)

disable_tracing

public final void disable_tracing()

enable_tracing

public final void enable_tracing()

escape

public static String escape(String s)
Returns a String where those characters that QueryParser expects to be escaped are escaped, i.e. preceded by a \.

generateParseException

public ParseException generateParseException()

getAnalyzer

public Analyzer getAnalyzer()
Returns:
Returns the analyzer.

getBooleanQuery

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.
Parameters:
clauses - Vector that contains BooleanClause instances to join.
Returns:
Resulting Query object.
Throws:
ParseException - throw in overridden method to disallow

getField

public String getField()
Returns:
Returns the field.

getFieldQuery

protected Query getFieldQuery(String field,
                              String queryText)
            throws ParseException
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

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.
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field,
                              Analyzer analyzer,
                              String queryText)
            throws ParseException
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyser. This method will be deprecated and substituted by getFieldQuery(String,String) in future versions of Lucene. Currently overwriting either of these methods works.
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field,
                              Analyzer analyzer,
                              String queryText,
                              int slop)
            throws ParseException
Base implementation delegates to getFieldQuery(String,Analyzer,String). This method may be overwritten, for example, to return a SpanNearQuery instead of a PhraseQuery. Note that parameter analyzer is ignored. Calls inside the parser always use class member analyser. This method will be deprecated and substituted by getFieldQuery(String,String,int) in future versions of Lucene. Currently overwriting either of these methods works.
Throws:
ParseException - throw in overridden method to disallow

getFuzzyMinSim

public float getFuzzyMinSim()
Get the default minimal similarity for fuzzy queries.

getFuzzyQuery

protected Query getFuzzyQuery(String field,
                              String termStr)
            throws ParseException
Factory method for generating a query (similar to (getWildcardQuery(String,String)). Called when parser parses an input term token that has the fuzzy suffix (~) appended.
Parameters:
field - Name of the field query will use.
termStr - Term token to use for building term for the query
Returns:
Resulting Query built for the term
Throws:
ParseException - throw in overridden method to disallow

getFuzzyQuery

protected Query getFuzzyQuery(String field,
                              String termStr,
                              float minSimilarity)
            throws ParseException
Factory method for generating a query (similar to (getWildcardQuery(String,String)). Called when parser parses an input term token that has the fuzzy suffix (~floatNumber) appended.
Parameters:
field - Name of the field query will use.
termStr - Term token to use for building term for the query
minSimilarity - the minimum similarity required for a fuzzy match
Returns:
Resulting Query built for the term
Throws:
ParseException - throw in overridden method to disallow

getLocale

public Locale getLocale()
Returns current locale, allowing access by subclasses.

getLowercaseWildcardTerms

public boolean getLowercaseWildcardTerms()

getNextToken

public final Token getNextToken()

getOperator

public int getOperator()
Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND or DEFAULT_OPERATOR_OR.

getPhraseSlop

public int getPhraseSlop()
Gets the default slop for phrases.

getPrefixQuery

protected Query getPrefixQuery(String field,
                               String termStr)
            throws ParseException
Factory method for generating a query (similar to (getWildcardQuery(String,String)). 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.

Parameters:
field - Name of the field query will use.
termStr - Term token to use for building term for the query (without trailing '*' character!)
Returns:
Resulting Query built for the term
Throws:
ParseException - throw in overridden method to disallow

getRangeQuery

protected Query getRangeQuery(String field,
                              String part1,
                              String part2,
                              boolean inclusive)
            throws ParseException
Throws:
ParseException - throw in overridden method to disallow

getRangeQuery

protected Query getRangeQuery(String field,
                              Analyzer analyzer,
                              String part1,
                              String part2,
                              boolean inclusive)
            throws ParseException
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyser. This method will be deprecated and substituted by getRangeQuery(String,String,String,boolean) in future versions of Lucene. Currently overwriting either of these methods works.
Throws:
ParseException - throw in overridden method to disallow

getToken

public final Token getToken(int index)

getWildcardQuery

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.

Parameters:
field - Name of the field query will use.
termStr - Term token that contains one or more wild card characters (? or *), but is not simple prefix term
Returns:
Resulting Query built for the term
Throws:
ParseException - throw in overridden method to disallow

main

public static void main(String[] args)
            throws Exception

parse

public Query parse(String query)
            throws ParseException
Parses a query string, returning a Query.
Parameters:
query - the query string to be parsed.
Throws:
ParseException - if the parsing fails

parse

public static Query parse(String query,
                          String field,
                          Analyzer analyzer)
            throws ParseException
Parses a query string, returning a Query.
Parameters:
query - the query string to be parsed.
field - the default field for query terms.
analyzer - used to find terms in the query text.
Throws:
ParseException - if the parsing fails

setFuzzyMinSim

public void setFuzzyMinSim(float fuzzyMinSim)
Set the default minimum similarity for fuzzy queries.

setLocale

public void setLocale(Locale locale)
Set locale used by date range parsing.

setLowercaseWildcardTerms

public void setLowercaseWildcardTerms(boolean lowercaseWildcardTerms)

setOperator

public void setOperator(int operator)
Sets the boolean operator of the QueryParser. In classic mode (DEFAULT_OPERATOR_OR) terms without any modifiers are considered optional: for example capital of Hungary is equal to capital OR of OR Hungary.
In DEFAULT_OPERATOR_AND terms are considered to be in conjuction: the above mentioned query is parsed as capital AND of AND Hungary

setPhraseSlop

public void setPhraseSlop(int phraseSlop)
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.

Copyright © 2000-2006 Apache Software Foundation. All Rights Reserved.