Class QueryParser
- All Implemented Interfaces:
QueryParserConstants
,CommonQueryParserConfiguration
- Direct Known Subclasses:
ComplexPhraseQueryParser
,ExtendableQueryParser
,MultiFieldQueryParser
QueryParserBase.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 on the locale
.
A DateTools.Resolution
has to be set,
if you want to use DateTools
for date conversion.
The date resolution that shall be used for RangeQueries can be set
using QueryParserBase.setDateResolution(DateTools.Resolution)
or QueryParserBase.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 overwrites
QueryParserBase.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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
The default operator for parsing queries.Nested classes/interfaces inherited from class org.apache.lucene.util.QueryBuilder
QueryBuilder.TermAndBoost
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean
default split on whitespace behaviorNext token.Current token.Generated Token Manager.Fields inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
AND_OPERATOR, field, OR_OPERATOR
Fields inherited from class org.apache.lucene.util.QueryBuilder
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements
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
ModifierConstructorDescriptionQueryParser
(String f, Analyzer a) Create a query parser.protected
QueryParser
(CharStream stream) Constructor with user supplied CharStream.protected
Constructor with generated Token Manager. -
Method Summary
Modifier and TypeMethodDescriptionfinal Query
final int
final void
Disable tracing.final void
Enable tracing.Generate ParseException.final Token
Get the next Token.boolean
final Token
getToken
(int index) Get the specific Token.final int
final Query
MultiTerm
(String field, List<BooleanClause> clauses) Returns the first query if splitOnWhitespace=true or otherwise the entire produced queryfinal Query
void
ReInit
(CharStream stream) Reinitialise.void
Reinitialise.void
setAutoGeneratePhraseQueries
(boolean value) Set to true if phrase queries will be automatically generated when the analyzer returns more than one term from whitespace delimited text.void
setSplitOnWhitespace
(boolean splitOnWhitespace) Whether query text should be split on whitespace prior to analysis.final Query
final Query
TopLevelQuery
(String field) final boolean
Trace enabled.Methods inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
addClause, addMultiTermClauses, escape, getAllowLeadingWildcard, getAutoGeneratePhraseQueries, getBooleanQuery, getDateResolution, getDefaultOperator, getDeterminizeWorkLimit, getField, getFieldQuery, getFieldQuery, getFuzzyDistance, getFuzzyMinSim, getFuzzyPrefixLength, getFuzzyQuery, getLocale, getMultiTermRewriteMethod, getPhraseSlop, getPrefixQuery, getRangeQuery, getRegexpQuery, getTimeZone, getWildcardQuery, init, newBooleanClause, newFieldQuery, newFuzzyQuery, newMatchAllDocsQuery, newPrefixQuery, newRangeQuery, newRegexpQuery, newWildcardQuery, parse, setAllowLeadingWildcard, setDateResolution, setDateResolution, setDefaultOperator, setDeterminizeWorkLimit, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setMultiTermRewriteMethod, setPhraseSlop, setTimeZone
Methods inherited from class org.apache.lucene.util.QueryBuilder
add, analyzeBoolean, analyzeGraphBoolean, analyzeGraphPhrase, analyzeMultiBoolean, analyzeMultiPhrase, analyzePhrase, analyzeTerm, createBooleanQuery, createBooleanQuery, createFieldQuery, createFieldQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, getAnalyzer, getAutoGenerateMultiTermSynonymsPhraseQuery, getEnableGraphQueries, getEnablePositionIncrements, newBooleanQuery, newGraphSynonymQuery, newMultiPhraseQueryBuilder, newSynonymQuery, newTermQuery, setAnalyzer, setAutoGenerateMultiTermSynonymsPhraseQuery, setEnableGraphQueries, setEnablePositionIncrements
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration
getAnalyzer, getEnablePositionIncrements, setEnablePositionIncrements
-
Field Details
-
DEFAULT_SPLIT_ON_WHITESPACE
public static final boolean DEFAULT_SPLIT_ON_WHITESPACEdefault split on whitespace behavior- See Also:
-
token_source
Generated Token Manager. -
token
Current token. -
jj_nt
Next token.
-
-
Constructor Details
-
QueryParser
Create a query parser.- Parameters:
f
- the default field for query terms.a
- used to find terms in the query text.
-
QueryParser
Constructor with user supplied CharStream. -
QueryParser
Constructor with generated Token Manager.
-
-
Method Details
-
setAutoGeneratePhraseQueries
public void setAutoGeneratePhraseQueries(boolean value) Set to true if phrase queries will be automatically generated when the analyzer returns more than one term from whitespace delimited text. NOTE: this behavior may not be suitable for all languages.Set to false if phrase queries should only be generated when surrounded by double quotes.
The combination splitOnWhitespace=false and autoGeneratePhraseQueries=true is disallowed. See LUCENE-7533.
- Overrides:
setAutoGeneratePhraseQueries
in classQueryParserBase
-
getSplitOnWhitespace
public boolean getSplitOnWhitespace()- See Also:
-
setSplitOnWhitespace
public void setSplitOnWhitespace(boolean splitOnWhitespace) Whether query text should be split on whitespace prior to analysis. Default isfalse
.The combination splitOnWhitespace=false and autoGeneratePhraseQueries=true is disallowed. See LUCENE-7533.
-
Conjunction
- Throws:
ParseException
-
Modifiers
- Throws:
ParseException
-
TopLevelQuery
- Specified by:
TopLevelQuery
in classQueryParserBase
- Throws:
ParseException
-
Query
- Throws:
ParseException
-
Clause
- Throws:
ParseException
-
Term
- Throws:
ParseException
-
MultiTerm
Returns the first query if splitOnWhitespace=true or otherwise the entire produced query- Throws:
ParseException
-
ReInit
Reinitialise.- Specified by:
ReInit
in classQueryParserBase
-
ReInit
Reinitialise. -
getNextToken
Get the next Token. -
getToken
Get the specific Token. -
generateParseException
Generate ParseException. -
trace_enabled
public final boolean trace_enabled()Trace enabled. -
enable_tracing
public final void enable_tracing()Enable tracing. -
disable_tracing
public final void disable_tracing()Disable tracing.
-