public class QueryParser extends QueryParserBase implements QueryParserConstants
QueryParserBase.parse(String)
.
The syntax for query strings is as follows:
A Query is a series of clauses.
A clause may be prefixed by:
+
) or a minus (-
) sign, indicating
that the clause is required or prohibited respectively; or
+
/-
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.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
QueryParser.JJCalls |
private static class |
QueryParser.LookaheadSuccess |
static class |
QueryParser.Operator
The default operator for parsing queries.
|
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_SPLIT_ON_WHITESPACE
default split on whitespace behavior
|
private static java.util.Set<java.lang.Integer> |
disallowedPostMultiTerm |
private QueryParser.JJCalls[] |
jj_2_rtns |
private int |
jj_endpos |
private java.util.List<int[]> |
jj_expentries |
private int[] |
jj_expentry |
private int |
jj_gc |
private int |
jj_gen |
private int |
jj_kind |
private int |
jj_la |
private int[] |
jj_la1 |
private static int[] |
jj_la1_0 |
private static int[] |
jj_la1_1 |
private Token |
jj_lastpos |
private int[] |
jj_lasttokens |
private boolean |
jj_lookingAhead
Whether we are looking ahead.
|
private QueryParser.LookaheadSuccess |
jj_ls |
Token |
jj_nt
Next token.
|
private int |
jj_ntk |
private boolean |
jj_rescan |
private Token |
jj_scanpos |
private boolean |
jj_semLA |
private boolean |
splitOnWhitespace |
Token |
token
Current token.
|
QueryParserTokenManager |
token_source
Generated Token Manager.
|
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
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements
_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
Modifier | Constructor and Description |
---|---|
protected |
QueryParser(CharStream stream)
Constructor with user supplied CharStream.
|
protected |
QueryParser(QueryParserTokenManager tm)
Constructor with generated Token Manager.
|
|
QueryParser(java.lang.String f,
Analyzer a)
Create a query parser.
|
Modifier and Type | Method and Description |
---|---|
private static boolean |
allowedPostMultiTerm(int tokenKind) |
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.
|
boolean |
getSplitOnWhitespace() |
Token |
getToken(int index)
Get the specific Token.
|
private boolean |
jj_2_1(int xla) |
private boolean |
jj_2_2(int xla) |
private boolean |
jj_2_3(int xla) |
private boolean |
jj_3_1() |
private boolean |
jj_3_2() |
private boolean |
jj_3_3() |
private boolean |
jj_3R_3() |
private boolean |
jj_3R_4() |
private boolean |
jj_3R_5() |
private boolean |
jj_3R_6() |
private boolean |
jj_3R_7() |
private void |
jj_add_error_token(int kind,
int pos) |
private Token |
jj_consume_token(int kind) |
private static void |
jj_la1_init_0() |
private static void |
jj_la1_init_1() |
private int |
jj_ntk() |
private void |
jj_rescan_token() |
private void |
jj_save(int index,
int xla) |
private boolean |
jj_scan_token(int kind) |
int |
Modifiers() |
Query |
MultiTerm(java.lang.String field,
java.util.List<BooleanClause> clauses)
Returns the first query if splitOnWhitespace=true or otherwise the entire produced query
|
Query |
Query(java.lang.String field) |
void |
ReInit(CharStream stream)
Reinitialise.
|
void |
ReInit(QueryParserTokenManager tm)
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.
|
Query |
Term(java.lang.String field) |
Query |
TopLevelQuery(java.lang.String field) |
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
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getAnalyzer, getEnablePositionIncrements, setEnablePositionIncrements
public static final boolean DEFAULT_SPLIT_ON_WHITESPACE
private boolean splitOnWhitespace
private static java.util.Set<java.lang.Integer> disallowedPostMultiTerm
public QueryParserTokenManager token_source
public Token token
public Token jj_nt
private int jj_ntk
private Token jj_scanpos
private Token jj_lastpos
private int jj_la
private boolean jj_lookingAhead
private boolean jj_semLA
private int jj_gen
private final int[] jj_la1
private static int[] jj_la1_0
private static int[] jj_la1_1
private final QueryParser.JJCalls[] jj_2_rtns
private boolean jj_rescan
private int jj_gc
private final QueryParser.LookaheadSuccess jj_ls
private java.util.List<int[]> jj_expentries
private int[] jj_expentry
private int jj_kind
private int[] jj_lasttokens
private int jj_endpos
public QueryParser(java.lang.String f, Analyzer a)
f
- the default field for query terms.a
- used to find terms in the query text.protected QueryParser(CharStream stream)
protected QueryParser(QueryParserTokenManager tm)
public void setAutoGeneratePhraseQueries(boolean value)
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.
setAutoGeneratePhraseQueries
in class QueryParserBase
public boolean getSplitOnWhitespace()
setSplitOnWhitespace(boolean)
public void setSplitOnWhitespace(boolean splitOnWhitespace)
false
.
The combination splitOnWhitespace=false and autoGeneratePhraseQueries=true is disallowed. See LUCENE-7533.
private static boolean allowedPostMultiTerm(int tokenKind)
public final int Conjunction() throws ParseException
ParseException
public final int Modifiers() throws ParseException
ParseException
public final Query TopLevelQuery(java.lang.String field) throws ParseException
TopLevelQuery
in class QueryParserBase
ParseException
public final Query Query(java.lang.String field) throws ParseException
ParseException
public final Query Clause(java.lang.String field) throws ParseException
ParseException
public final Query Term(java.lang.String field) throws ParseException
ParseException
public final Query MultiTerm(java.lang.String field, java.util.List<BooleanClause> clauses) throws ParseException
ParseException
private boolean jj_2_1(int xla)
private boolean jj_2_2(int xla)
private boolean jj_2_3(int xla)
private boolean jj_3R_3()
private boolean jj_3R_6()
private boolean jj_3R_5()
private boolean jj_3R_4()
private boolean jj_3_2()
private boolean jj_3_1()
private boolean jj_3R_7()
private boolean jj_3_3()
private static void jj_la1_init_0()
private static void jj_la1_init_1()
public void ReInit(CharStream stream)
ReInit
in class QueryParserBase
public void ReInit(QueryParserTokenManager tm)
private Token jj_consume_token(int kind) throws ParseException
ParseException
private boolean jj_scan_token(int kind)
public final Token getNextToken()
public final Token getToken(int index)
private int jj_ntk()
private void jj_add_error_token(int kind, int pos)
public ParseException generateParseException()
public final void enable_tracing()
public final void disable_tracing()
private void jj_rescan_token()
private void jj_save(int index, int xla)