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:
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
TermRangeQuerys, 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.Resolutionhas to be set, if you want to useDateToolsfor 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
DateToolsin 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classQueryParser.JJCallsprivate static classQueryParser.LookaheadSuccessstatic classQueryParser.OperatorThe default operator for parsing queries.-
Nested classes/interfaces inherited from class org.apache.lucene.util.QueryBuilder
QueryBuilder.TermAndBoost
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_SPLIT_ON_WHITESPACEdefault split on whitespace behaviorprivate static java.util.Set<java.lang.Integer>disallowedPostMultiTermprivate QueryParser.JJCalls[]jj_2_rtnsprivate intjj_endposprivate java.util.List<int[]>jj_expentriesprivate int[]jj_expentryprivate intjj_gcprivate intjj_genprivate intjj_kindprivate intjj_laprivate int[]jj_la1private static int[]jj_la1_0private static int[]jj_la1_1private Tokenjj_lastposprivate int[]jj_lasttokensprivate booleanjj_lookingAheadWhether we are looking ahead.private static QueryParser.LookaheadSuccessjj_lsTokenjj_ntNext token.private intjj_ntkprivate booleanjj_rescanprivate Tokenjj_scanposprivate booleanjj_semLAprivate booleansplitOnWhitespaceTokentokenCurrent token.QueryParserTokenManagertoken_sourceGenerated Token Manager.-
Fields inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
allowLeadingWildcard, AND_OPERATOR, autoGeneratePhraseQueries, CONJ_AND, CONJ_NONE, CONJ_OR, dateResolution, field, fieldToDateResolution, fuzzyMinSim, fuzzyPrefixLength, locale, maxDeterminizedStates, MOD_NONE, MOD_NOT, MOD_REQ, multiTermRewriteMethod, operator, OR_OPERATOR, phraseSlop, timeZone
-
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
Constructors Modifier Constructor Description QueryParser(java.lang.String f, Analyzer a)Create a query parser.protectedQueryParser(CharStream stream)Constructor with user supplied CharStream.protectedQueryParser(QueryParserTokenManager tm)Constructor with generated Token Manager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static booleanallowedPostMultiTerm(int tokenKind)QueryClause(java.lang.String field)intConjunction()voiddisable_tracing()Disable tracing.voidenable_tracing()Enable tracing.ParseExceptiongenerateParseException()Generate ParseException.TokengetNextToken()Get the next Token.booleangetSplitOnWhitespace()TokengetToken(int index)Get the specific Token.private booleanjj_2_1(int xla)private booleanjj_2_2(int xla)private booleanjj_2_3(int xla)private booleanjj_3_1()private booleanjj_3_2()private booleanjj_3_3()private booleanjj_3R_3()private booleanjj_3R_4()private booleanjj_3R_5()private booleanjj_3R_6()private booleanjj_3R_7()private voidjj_add_error_token(int kind, int pos)private Tokenjj_consume_token(int kind)private static voidjj_la1_init_0()private static voidjj_la1_init_1()private intjj_ntk()private voidjj_rescan_token()private voidjj_save(int index, int xla)private booleanjj_scan_token(int kind)intModifiers()QueryMultiTerm(java.lang.String field, java.util.List<BooleanClause> clauses)Returns the first query if splitOnWhitespace=true or otherwise the entire produced queryQueryQuery(java.lang.String field)voidReInit(CharStream stream)Reinitialise.voidReInit(QueryParserTokenManager tm)Reinitialise.voidsetAutoGeneratePhraseQueries(boolean value)Set to true if phrase queries will be automatically generated when the analyzer returns more than one term from whitespace delimited text.voidsetSplitOnWhitespace(boolean splitOnWhitespace)Whether query text should be split on whitespace prior to analysis.QueryTerm(java.lang.String field)QueryTopLevelQuery(java.lang.String field)-
Methods inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
addClause, addMultiTermClauses, discardEscapeChar, escape, getAllowLeadingWildcard, getAutoGeneratePhraseQueries, getBooleanQuery, getDateResolution, getDefaultOperator, getField, getFieldQuery, getFieldQuery, getFuzzyMinSim, getFuzzyPrefixLength, getFuzzyQuery, getLocale, getMaxDeterminizedStates, getMultiTermRewriteMethod, getPhraseSlop, getPrefixQuery, getRangeQuery, getRegexpQuery, getTimeZone, getWildcardQuery, handleBareFuzzy, handleBareTokenQuery, handleBoost, handleQuotedTerm, hexToInt, init, newBooleanClause, newFieldQuery, newFuzzyQuery, newMatchAllDocsQuery, newPrefixQuery, newRangeQuery, newRegexpQuery, newWildcardQuery, parse, setAllowLeadingWildcard, setDateResolution, setDateResolution, setDefaultOperator, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setMaxDeterminizedStates, 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, createSpanQuery, 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 Detail
-
DEFAULT_SPLIT_ON_WHITESPACE
public static final boolean DEFAULT_SPLIT_ON_WHITESPACE
default split on whitespace behavior- See Also:
- Constant Field Values
-
splitOnWhitespace
private boolean splitOnWhitespace
-
disallowedPostMultiTerm
private static java.util.Set<java.lang.Integer> disallowedPostMultiTerm
-
token_source
public QueryParserTokenManager token_source
Generated Token Manager.
-
token
public Token token
Current token.
-
jj_nt
public Token jj_nt
Next token.
-
jj_ntk
private int jj_ntk
-
jj_scanpos
private Token jj_scanpos
-
jj_lastpos
private Token jj_lastpos
-
jj_la
private int jj_la
-
jj_lookingAhead
private boolean jj_lookingAhead
Whether we are looking ahead.
-
jj_semLA
private boolean jj_semLA
-
jj_gen
private int jj_gen
-
jj_la1
private final int[] jj_la1
-
jj_la1_0
private static int[] jj_la1_0
-
jj_la1_1
private static int[] jj_la1_1
-
jj_2_rtns
private final QueryParser.JJCalls[] jj_2_rtns
-
jj_rescan
private boolean jj_rescan
-
jj_gc
private int jj_gc
-
jj_ls
private static final QueryParser.LookaheadSuccess jj_ls
-
jj_expentries
private java.util.List<int[]> jj_expentries
-
jj_expentry
private int[] jj_expentry
-
jj_kind
private int jj_kind
-
jj_lasttokens
private int[] jj_lasttokens
-
jj_endpos
private int jj_endpos
-
-
Constructor Detail
-
QueryParser
public QueryParser(java.lang.String f, Analyzer a)Create a query parser.- Parameters:
f- the default field for query terms.a- used to find terms in the query text.
-
QueryParser
protected QueryParser(CharStream stream)
Constructor with user supplied CharStream.
-
QueryParser
protected QueryParser(QueryParserTokenManager tm)
Constructor with generated Token Manager.
-
-
Method Detail
-
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:
setAutoGeneratePhraseQueriesin classQueryParserBase
-
getSplitOnWhitespace
public boolean getSplitOnWhitespace()
- See Also:
setSplitOnWhitespace(boolean)
-
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.
-
allowedPostMultiTerm
private static boolean allowedPostMultiTerm(int tokenKind)
-
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:
TopLevelQueryin 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
-
MultiTerm
public final Query MultiTerm(java.lang.String field, java.util.List<BooleanClause> clauses) throws ParseException
Returns the first query if splitOnWhitespace=true or otherwise the entire produced query- Throws:
ParseException
-
jj_2_1
private boolean jj_2_1(int xla)
-
jj_2_2
private boolean jj_2_2(int xla)
-
jj_2_3
private boolean jj_2_3(int xla)
-
jj_3R_3
private boolean jj_3R_3()
-
jj_3R_6
private boolean jj_3R_6()
-
jj_3R_5
private boolean jj_3R_5()
-
jj_3R_4
private boolean jj_3R_4()
-
jj_3_2
private boolean jj_3_2()
-
jj_3_1
private boolean jj_3_1()
-
jj_3R_7
private boolean jj_3R_7()
-
jj_3_3
private boolean jj_3_3()
-
jj_la1_init_0
private static void jj_la1_init_0()
-
jj_la1_init_1
private static void jj_la1_init_1()
-
ReInit
public void ReInit(CharStream stream)
Reinitialise.- Specified by:
ReInitin classQueryParserBase
-
ReInit
public void ReInit(QueryParserTokenManager tm)
Reinitialise.
-
jj_consume_token
private Token jj_consume_token(int kind) throws ParseException
- Throws:
ParseException
-
jj_scan_token
private boolean jj_scan_token(int kind)
-
getNextToken
public final Token getNextToken()
Get the next Token.
-
getToken
public final Token getToken(int index)
Get the specific Token.
-
jj_ntk
private int jj_ntk()
-
jj_add_error_token
private void jj_add_error_token(int kind, int pos)
-
generateParseException
public ParseException generateParseException()
Generate ParseException.
-
enable_tracing
public final void enable_tracing()
Enable tracing.
-
disable_tracing
public final void disable_tracing()
Disable tracing.
-
jj_rescan_token
private void jj_rescan_token()
-
jj_save
private void jj_save(int index, int xla)
-
-