Class QueryParser
- java.lang.Object
-
- org.apache.lucene.util.QueryBuilder
-
- org.apache.lucene.queryparser.classic.QueryParserBase
-
- org.apache.lucene.queryparser.classic.QueryParser
-
- All Implemented Interfaces:
QueryParserConstants
,CommonQueryParserConfiguration
- Direct Known Subclasses:
AnalyzingQueryParser
,ComplexPhraseQueryParser
,ExtendableQueryParser
,MultiFieldQueryParser
public class QueryParser extends QueryParserBase implements QueryParserConstants
This class is generated by JavaCC. The most important method isQueryParserBase.parse(String)
. The syntax for query strings is as follows: A Query is a series of clauses. A clause may be prefixed by:- a plus (
+
) or a minus (-
) sign, indicating that the clause is required or prohibited respectively; or - a term followed by a colon, indicating the field to be searched. This enables one to construct queries which search multiple fields.
- a term, indicating all the documents that contain this term; or
- a nested query, enclosed in parentheses. Note that this may be used
with a
+
/-
prefix to require any of a set of terms.
Query ::= ( Clause )* Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
Examples of appropriately formatted queries can be found in the query syntax documentation.
In
TermRangeQuery
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 onthe locale
. ADateTools.Resolution
has to be set, if you want to useDateTools
for date conversion.The date resolution that shall be used for RangeQueries can be set using
QueryParserBase.setDateResolution(DateTools.Resolution)
orQueryParserBase.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 don't use
DateTools
in your index, you can create your own query parser that inherits QueryParser and overwritesQueryParserBase.getRangeQuery(String, String, String, boolean, boolean)
to use a different method for date conversion.Note that QueryParser is not thread-safe.
NOTE: there is a new QueryParser in contrib, which matches the same syntax as this class, but is more modular, enabling substantial customization to how a query is created.
NOTE: You must specify the required
Version
compatibility when creating QueryParser:- As of 3.1,
QueryParserBase.setAutoGeneratePhraseQueries(boolean)
is false by default.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
QueryParser.Operator
The default operator for parsing queries.-
Nested classes/interfaces inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
QueryParserBase.MethodRemovedUseAnother
-
-
Field Summary
Fields Modifier and Type Field Description Token
jj_nt
Next token.Token
token
Current token.QueryParserTokenManager
token_source
Generated Token Manager.-
Fields inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
AND_OPERATOR, OR_OPERATOR
-
Fields inherited from interface org.apache.lucene.queryparser.classic.QueryParserConstants
_ESCAPED_CHAR, _NUM_CHAR, _QUOTED_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, AND, BAREOPER, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, Range, RANGE_GOOP, RANGE_QUOTED, RANGE_TO, RANGEEX_END, RANGEEX_START, RANGEIN_END, RANGEIN_START, REGEXPTERM, RPAREN, STAR, TERM, tokenImage, WILDTERM
-
-
Constructor Summary
Constructors Constructor Description QueryParser(Version matchVersion, java.lang.String f, Analyzer a)
Create a query parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Query
Clause(java.lang.String field)
int
Conjunction()
void
disable_tracing()
Disable tracing.void
enable_tracing()
Enable tracing.ParseException
generateParseException()
Generate ParseException.Token
getNextToken()
Get the next Token.Token
getToken(int index)
Get the specific Token.int
Modifiers()
Query
Query(java.lang.String field)
void
ReInit(CharStream stream)
Reinitialise.void
ReInit(QueryParserTokenManager tm)
Reinitialise.Query
Term(java.lang.String field)
Query
TopLevelQuery(java.lang.String field)
-
Methods inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
escape, getAllowLeadingWildcard, getAnalyzeRangeTerms, getAutoGeneratePhraseQueries, getDateResolution, getDefaultOperator, getField, getFuzzyMinSim, getFuzzyPrefixLength, getLocale, getLowercaseExpandedTerms, getMultiTermRewriteMethod, getPhraseSlop, getTimeZone, init, parse, setAllowLeadingWildcard, setAnalyzeRangeTerms, setAutoGeneratePhraseQueries, setDateResolution, setDateResolution, setDefaultOperator, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setLowercaseExpandedTerms, setMultiTermRewriteMethod, setPhraseSlop, setTimeZone
-
Methods inherited from class org.apache.lucene.util.QueryBuilder
createBooleanQuery, createBooleanQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, getAnalyzer, getEnablePositionIncrements, setAnalyzer, setEnablePositionIncrements
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration
getAnalyzer, getEnablePositionIncrements, setEnablePositionIncrements
-
-
-
-
Field Detail
-
token_source
public QueryParserTokenManager token_source
Generated Token Manager.
-
token
public Token token
Current token.
-
jj_nt
public Token jj_nt
Next token.
-
-
Method Detail
-
Conjunction
public final int Conjunction() throws ParseException
- Throws:
ParseException
-
Modifiers
public final int Modifiers() throws ParseException
- Throws:
ParseException
-
TopLevelQuery
public final Query TopLevelQuery(java.lang.String field) throws ParseException
- Specified by:
TopLevelQuery
in classQueryParserBase
- Throws:
ParseException
-
Query
public final Query Query(java.lang.String field) throws ParseException
- Throws:
ParseException
-
Clause
public final Query Clause(java.lang.String field) throws ParseException
- Throws:
ParseException
-
Term
public final Query Term(java.lang.String field) throws ParseException
- Throws:
ParseException
-
ReInit
public void ReInit(CharStream stream)
Reinitialise.- Specified by:
ReInit
in classQueryParserBase
-
ReInit
public void ReInit(QueryParserTokenManager tm)
Reinitialise.
-
getNextToken
public final Token getNextToken()
Get the next Token.
-
getToken
public final Token getToken(int index)
Get the specific Token.
-
generateParseException
public ParseException generateParseException()
Generate ParseException.
-
enable_tracing
public final void enable_tracing()
Enable tracing.
-
disable_tracing
public final void disable_tracing()
Disable tracing.
-
-