Class QueryParser
- java.lang.Object
-
- org.apache.lucene.queryParser.QueryParser
-
- All Implemented Interfaces:
- QueryParserConstants
- Direct Known Subclasses:
- MultiFieldQueryParser
public class QueryParser extends java.lang.Object implements QueryParserConstants
This class is generated by JavaCC. The most important method isparse(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
RangeQuerys, 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. By default a date is converted into a search term using the deprecatedDateFieldfor compatibility reasons. To use the newDateToolsto convert dates, aDateTools.Resolutionhas to be set.The date resolution that shall be used for RangeQueries can be set using
setDateResolution(DateTools.Resolution)orsetDateResolution(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 use neither
DateFieldnorDateToolsin your index, you can create your own query parser that inherits QueryParser and overwritesgetRangeQuery(String, String, String, boolean)to use a different method for date conversion.Note that QueryParser is not thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classQueryParser.OperatorThe default operator for parsing queries.
-
Field Summary
Fields Modifier and Type Field and Description static QueryParser.OperatorAND_OPERATORAlternative form of QueryParser.Operator.ANDTokenjj_ntbooleanlookingAheadstatic QueryParser.OperatorOR_OPERATORAlternative form of QueryParser.Operator.ORTokentokenQueryParserTokenManagertoken_source-
Fields inherited from interface org.apache.lucene.queryParser.QueryParserConstants
_ESCAPED_CHAR, _NUM_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, AND, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, RangeEx, RANGEEX_END, RANGEEX_GOOP, RANGEEX_QUOTED, RANGEEX_START, RANGEEX_TO, RangeIn, RANGEIN_END, RANGEIN_GOOP, RANGEIN_QUOTED, RANGEIN_START, RANGEIN_TO, RPAREN, STAR, TERM, tokenImage, WILDTERM
-
-
Constructor Summary
Constructors Constructor and Description QueryParser(CharStream stream)QueryParser(QueryParserTokenManager tm)QueryParser(java.lang.String f, Analyzer a)Constructs a query parser.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description QueryClause(java.lang.String field)intConjunction()voiddisable_tracing()voidenable_tracing()static java.lang.Stringescape(java.lang.String s)Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding\.ParseExceptiongenerateParseException()booleangetAllowLeadingWildcard()AnalyzergetAnalyzer()DateTools.ResolutiongetDateResolution(java.lang.String fieldName)Returns the date resolution that is used by RangeQueries for the given field.QueryParser.OperatorgetDefaultOperator()Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.booleangetEnablePositionIncrements()java.lang.StringgetField()floatgetFuzzyMinSim()Get the minimal similarity for fuzzy queries.intgetFuzzyPrefixLength()Get the prefix length for fuzzy queries.java.util.LocalegetLocale()Returns current locale, allowing access by subclasses.booleangetLowercaseExpandedTerms()TokengetNextToken()intgetPhraseSlop()Gets the default slop for phrases.TokengetToken(int index)booleangetUseOldRangeQuery()static voidmain(java.lang.String[] args)Command line tool to test QueryParser, usingSimpleAnalyzer.intModifiers()Queryparse(java.lang.String query)Parses a query string, returning aQuery.QueryQuery(java.lang.String field)voidReInit(CharStream stream)voidReInit(QueryParserTokenManager tm)voidsetAllowLeadingWildcard(boolean allowLeadingWildcard)Set totrueto allow leading wildcard characters.voidsetDateResolution(DateTools.Resolution dateResolution)Sets the default date resolution used by RangeQueries for fields for which no specific date resolutions has been set.voidsetDateResolution(java.lang.String fieldName, DateTools.Resolution dateResolution)Sets the date resolution used by RangeQueries for a specific field.voidsetDefaultOperator(QueryParser.Operator op)Sets the boolean operator of the QueryParser.voidsetEnablePositionIncrements(boolean enable)Set totrueto enable position increments in result query.voidsetFuzzyMinSim(float fuzzyMinSim)Set the minimum similarity for fuzzy queries.voidsetFuzzyPrefixLength(int fuzzyPrefixLength)Set the prefix length for fuzzy queries.voidsetLocale(java.util.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.voidsetPhraseSlop(int phraseSlop)Sets the default slop for phrases.voidsetUseOldRangeQuery(boolean useOldRangeQuery)By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery for range queries.QueryTerm(java.lang.String field)QueryTopLevelQuery(java.lang.String field)
-
-
-
Field Detail
-
AND_OPERATOR
public static final QueryParser.Operator AND_OPERATOR
Alternative form of QueryParser.Operator.AND
-
OR_OPERATOR
public static final QueryParser.Operator OR_OPERATOR
Alternative form of QueryParser.Operator.OR
-
token_source
public QueryParserTokenManager token_source
-
token
public Token token
-
jj_nt
public Token jj_nt
-
lookingAhead
public boolean lookingAhead
-
-
Constructor Detail
-
QueryParser
public QueryParser(java.lang.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
-
parse
public Query parse(java.lang.String query) throws ParseException
Parses a query string, returning aQuery.- Parameters:
query- the query string to be parsed.- Throws:
ParseException- if the parsing fails
-
getAnalyzer
public Analyzer getAnalyzer()
- Returns:
- Returns the analyzer.
-
getField
public java.lang.String getField()
- Returns:
- Returns the field.
-
getFuzzyMinSim
public float getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.
-
setFuzzyMinSim
public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 0.5f.
-
getFuzzyPrefixLength
public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.- Returns:
- Returns the fuzzyPrefixLength.
-
setFuzzyPrefixLength
public void setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.- Parameters:
fuzzyPrefixLength- The fuzzyPrefixLength to set.
-
setPhraseSlop
public void setPhraseSlop(int phraseSlop)
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.
-
getPhraseSlop
public int getPhraseSlop()
Gets the default slop for phrases.
-
setAllowLeadingWildcard
public void setAllowLeadingWildcard(boolean allowLeadingWildcard)
Set totrueto allow leading wildcard characters.When set,
*or?are allowed as the first character of a PrefixQuery and WildcardQuery. Note that this can produce very slow queries on big indexes.Default: false.
-
getAllowLeadingWildcard
public boolean getAllowLeadingWildcard()
- See Also:
setAllowLeadingWildcard(boolean)
-
setEnablePositionIncrements
public void setEnablePositionIncrements(boolean enable)
Set totrueto enable position increments in result query.When set, result phrase and multi-phrase queries will be aware of position increments. Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token.
Default: false.
-
getEnablePositionIncrements
public boolean getEnablePositionIncrements()
- See Also:
setEnablePositionIncrements(boolean)
-
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 examplecapital of Hungaryis equal tocapital OR of OR Hungary.
InAND_OPERATORmode terms are considered to be in conjuction: the above mentioned query is parsed ascapital AND of AND Hungary
-
getDefaultOperator
public QueryParser.Operator getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
-
setLowercaseExpandedTerms
public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default istrue.
-
getLowercaseExpandedTerms
public boolean getLowercaseExpandedTerms()
- See Also:
setLowercaseExpandedTerms(boolean)
-
setUseOldRangeQuery
public void setUseOldRangeQuery(boolean useOldRangeQuery)
By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery for range queries. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of range terms unduly influence score c) avoids any "TooManyBooleanClauses" exception. However, if your application really needs to use the old-fashioned RangeQuery and the above points are not required then set this option totrueDefault isfalse.
-
getUseOldRangeQuery
public boolean getUseOldRangeQuery()
- See Also:
setUseOldRangeQuery(boolean)
-
setLocale
public void setLocale(java.util.Locale locale)
Set locale used by date range parsing.
-
getLocale
public java.util.Locale getLocale()
Returns current locale, allowing access by subclasses.
-
setDateResolution
public void setDateResolution(DateTools.Resolution dateResolution)
Sets the default date resolution used by RangeQueries for fields for which no specific date resolutions has been set. Field specific resolutions can be set withsetDateResolution(String, DateTools.Resolution).- Parameters:
dateResolution- the default date resolution to set
-
setDateResolution
public void setDateResolution(java.lang.String fieldName, DateTools.Resolution dateResolution)Sets the date resolution used by RangeQueries for a specific field.- Parameters:
fieldName- field for which the date resolution is to be setdateResolution- date resolution to set
-
getDateResolution
public DateTools.Resolution getDateResolution(java.lang.String fieldName)
Returns the date resolution that is used by RangeQueries for the given field. Returns null, if no default or field specific date resolution has been set for the given field.
-
escape
public static java.lang.String escape(java.lang.String s)
Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding\.
-
main
public static void main(java.lang.String[] args) throws java.lang.ExceptionCommand line tool to test QueryParser, usingSimpleAnalyzer. Usage:
java org.apache.lucene.queryParser.QueryParser <input>- Throws:
java.lang.Exception
-
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
- 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)
-
ReInit
public void ReInit(QueryParserTokenManager tm)
-
getNextToken
public final Token getNextToken()
-
getToken
public final Token getToken(int index)
-
generateParseException
public ParseException generateParseException()
-
enable_tracing
public final void enable_tracing()
-
disable_tracing
public final void disable_tracing()
-
-
DataMelt 3.0 © DataMelt by jWork.ORG