org.apache.lucene.queryParser

Class QueryParser

public class QueryParser extends Object implements QueryParserConstants

This class is generated by JavaCC. The most important method is {@link #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 {@link 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 accpeted input depends on {@link #setLocale(Locale) the locale}. This feature also assumes that your index uses the {@link DateField} class to store dates. If you use a different format (e.g. {@link DateTools}) and you still want QueryParser to turn local dates in range queries into valid queries you need to create your own query parser that inherits QueryParser and overwrites {@link #getRangeQuery(String, String, String, boolean)}.

Note that QueryParser is not thread-safe.

Author: Brian Goetz Peter Halacsy Tatu Saloranta

Nested Class Summary
static classQueryParser.Operator
The default operator for parsing queries.
Field Summary
static QueryParser.OperatorAND_OPERATOR
Alternative form of QueryParser.Operator.AND
static intDEFAULT_OPERATOR_AND
static intDEFAULT_OPERATOR_OR
Tokenjj_nt
booleanlookingAhead
static QueryParser.OperatorOR_OPERATOR
Alternative form of QueryParser.Operator.OR
Tokentoken
QueryParserTokenManagertoken_source
Constructor Summary
QueryParser(String f, Analyzer a)
Constructs a query parser.
QueryParser(CharStream stream)
QueryParser(QueryParserTokenManager tm)
Method Summary
protected voidaddClause(Vector clauses, int conj, int mods, Query q)
QueryClause(String field)
intConjunction()
voiddisable_tracing()
voidenable_tracing()
static Stringescape(String s)
Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.
ParseExceptiongenerateParseException()
AnalyzergetAnalyzer()
protected QuerygetBooleanQuery(Vector clauses)
Factory method for generating query, given a set of clauses.
protected QuerygetBooleanQuery(Vector clauses, boolean disableCoord)
Factory method for generating query, given a set of clauses.
QueryParser.OperatorgetDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
StringgetField()
protected QuerygetFieldQuery(String field, Analyzer analyzer, String queryText)
Note that parameter analyzer is ignored.
protected QuerygetFieldQuery(String field, String queryText)
protected QuerygetFieldQuery(String field, Analyzer analyzer, String queryText, int slop)
Note that parameter analyzer is ignored.
protected QuerygetFieldQuery(String field, String queryText, int slop)
Base implementation delegates to {@link #getFieldQuery(String,String)}.
floatgetFuzzyMinSim()
Get the minimal similarity for fuzzy queries.
intgetFuzzyPrefixLength()
Get the prefix length for fuzzy queries.
protected QuerygetFuzzyQuery(String field, String termStr)
protected QuerygetFuzzyQuery(String field, String termStr, float minSimilarity)
Factory method for generating a query (similar to {@link #getWildcardQuery}).
LocalegetLocale()
Returns current locale, allowing access by subclasses.
booleangetLowercaseExpandedTerms()
booleangetLowercaseWildcardTerms()
TokengetNextToken()
intgetOperator()
Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND or DEFAULT_OPERATOR_OR.
intgetPhraseSlop()
Gets the default slop for phrases.
protected QuerygetPrefixQuery(String field, String termStr)
Factory method for generating a query (similar to {@link #getWildcardQuery}).
protected QuerygetRangeQuery(String field, Analyzer analyzer, String part1, String part2, boolean inclusive)
Note that parameter analyzer is ignored.
protected QuerygetRangeQuery(String field, String part1, String part2, boolean inclusive)
TokengetToken(int index)
protected QuerygetWildcardQuery(String field, String termStr)
Factory method for generating a query.
static voidmain(String[] args)
Command line tool to test QueryParser, using {@link org.apache.lucene.analysis.SimpleAnalyzer}.
intModifiers()
static Queryparse(String query, String field, Analyzer analyzer)
Parses a query string, returning a {@link org.apache.lucene.search.Query}.
Queryparse(String query)
Parses a query string, returning a {@link org.apache.lucene.search.Query}.
QueryQuery(String field)
voidReInit(CharStream stream)
voidReInit(QueryParserTokenManager tm)
voidsetDefaultOperator(QueryParser.Operator op)
Sets the boolean operator of the QueryParser.
voidsetFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries.
voidsetFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries.
voidsetLocale(Locale locale)
Set locale used by date range parsing.
voidsetLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not.
voidsetLowercaseWildcardTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not.
voidsetOperator(int op)
Sets the boolean operator of the QueryParser.
voidsetPhraseSlop(int phraseSlop)
Sets the default slop for phrases.
QueryTerm(String field)

Field Detail

AND_OPERATOR

public static final QueryParser.Operator AND_OPERATOR
Alternative form of QueryParser.Operator.AND

DEFAULT_OPERATOR_AND

public static final int DEFAULT_OPERATOR_AND

Deprecated: use {@link #AND_OPERATOR} instead

DEFAULT_OPERATOR_OR

public static final int DEFAULT_OPERATOR_OR

Deprecated: use {@link #OR_OPERATOR} instead

jj_nt

public Token jj_nt

lookingAhead

public boolean lookingAhead

OR_OPERATOR

public static final QueryParser.Operator OR_OPERATOR
Alternative form of QueryParser.Operator.OR

token

public Token token

token_source

public QueryParserTokenManager token_source

Constructor Detail

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 Detail

addClause

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

Clause

public final Query Clause(String field)

Conjunction

public final int Conjunction()

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 by a preceding \.

generateParseException

public ParseException generateParseException()

getAnalyzer

public Analyzer getAnalyzer()

Returns: Returns the analyzer.

getBooleanQuery

protected Query getBooleanQuery(Vector clauses)
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 {@link BooleanClause} instances to join.

Returns: Resulting {@link Query} object.

Throws: ParseException throw in overridden method to disallow

getBooleanQuery

protected Query getBooleanQuery(Vector clauses, boolean disableCoord)
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 {@link BooleanClause} instances to join. disableCoord true if coord scoring should be disabled.

Returns: Resulting {@link Query} object.

Throws: ParseException throw in overridden method to disallow

getDefaultOperator

public QueryParser.Operator getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.

getField

public String getField()

Returns: Returns the field.

getFieldQuery

protected Query getFieldQuery(String field, Analyzer analyzer, String queryText)

Deprecated: use {@link #getFieldQuery(String, String)}

Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.

Throws: ParseException throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field, String queryText)

Throws: ParseException throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field, Analyzer analyzer, String queryText, int slop)

Deprecated: use {@link #getFieldQuery(String, String, int)}

Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.

Throws: ParseException throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field, String queryText, int slop)
Base implementation delegates to {@link #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

getFuzzyMinSim

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

getFuzzyPrefixLength

public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.

Returns: Returns the fuzzyPrefixLength.

getFuzzyQuery

protected Query getFuzzyQuery(String field, String termStr)

Deprecated: use {@link #getFuzzyQuery(String, String, float)}

getFuzzyQuery

protected Query getFuzzyQuery(String field, String termStr, float minSimilarity)
Factory method for generating a query (similar to {@link #getWildcardQuery}). 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 {@link Query} built for the term

Throws: ParseException throw in overridden method to disallow

getLocale

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

getLowercaseExpandedTerms

public boolean getLowercaseExpandedTerms()

See Also: QueryParser

getLowercaseWildcardTerms

public boolean getLowercaseWildcardTerms()

Deprecated: use {@link #getLowercaseExpandedTerms()} instead

getNextToken

public final Token getNextToken()

getOperator

public int getOperator()

Deprecated: use {@link #getDefaultOperator()} instead

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)
Factory method for generating a query (similar to {@link #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.

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 {@link Query} built for the term

Throws: ParseException throw in overridden method to disallow

getRangeQuery

protected Query getRangeQuery(String field, Analyzer analyzer, String part1, String part2, boolean inclusive)

Deprecated: use {@link #getRangeQuery(String, String, String, boolean)}

Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.

Throws: ParseException throw in overridden method to disallow

getRangeQuery

protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive)

Throws: ParseException throw in overridden method to disallow

getToken

public final Token getToken(int index)

getWildcardQuery

protected Query getWildcardQuery(String field, String termStr)
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 {@link Query} built for the term

Throws: ParseException throw in overridden method to disallow

main

public static void main(String[] args)
Command line tool to test QueryParser, using {@link org.apache.lucene.analysis.SimpleAnalyzer}. Usage:
java org.apache.lucene.queryParser.QueryParser <input>

Modifiers

public final int Modifiers()

parse

public static Query parse(String query, String field, Analyzer analyzer)

Deprecated: Use an instance of QueryParser and the {@link #parse(String)} method instead.

Parses a query string, returning a {@link org.apache.lucene.search.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

parse

public Query parse(String query)
Parses a query string, returning a {@link org.apache.lucene.search.Query}.

Parameters: query the query string to be parsed.

Throws: ParseException if the parsing fails

Query

public final Query Query(String field)

ReInit

public void ReInit(CharStream stream)

ReInit

public void ReInit(QueryParserTokenManager tm)

setDefaultOperator

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

setFuzzyMinSim

public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 0.5f.

setFuzzyPrefixLength

public void setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.

Parameters: fuzzyPrefixLength The fuzzyPrefixLength to set.

setLocale

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

setLowercaseExpandedTerms

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.

setLowercaseWildcardTerms

public void setLowercaseWildcardTerms(boolean lowercaseExpandedTerms)

Deprecated: use {@link #setLowercaseExpandedTerms(boolean)} instead

Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default is true.

setOperator

public void setOperator(int op)

Deprecated: use {@link #setDefaultOperator(QueryParser.Operator)} instead

Sets the boolean operator of the QueryParser. In default 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.

Term

public final Query Term(String field)
Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.