Class QueryParserBase
- java.lang.Object
-
- org.apache.lucene.util.QueryBuilder
-
- org.apache.lucene.queryparser.classic.QueryParserBase
-
- All Implemented Interfaces:
CommonQueryParserConfiguration
- Direct Known Subclasses:
QueryParser
public abstract class QueryParserBase extends QueryBuilder implements CommonQueryParserConfiguration
This class is overridden by QueryParser in QueryParser.jj and acts to separate the majority of the Java code from the .jj grammar file.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
QueryParserBase.MethodRemovedUseAnother
Do not catch this exception in your code, it means you are using methods that you should no longer use.
-
Field Summary
Fields Modifier and Type Field Description static QueryParser.Operator
AND_OPERATOR
Alternative form of QueryParser.Operator.ANDstatic QueryParser.Operator
OR_OPERATOR
Alternative form of QueryParser.Operator.OR
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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\
.boolean
getAllowLeadingWildcard()
boolean
getAnalyzeRangeTerms()
boolean
getAutoGeneratePhraseQueries()
DateTools.Resolution
getDateResolution(java.lang.String fieldName)
Returns the date resolution that is used by RangeQueries for the given field.QueryParser.Operator
getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.java.lang.String
getField()
float
getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.int
getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.java.util.Locale
getLocale()
Returns current locale, allowing access by subclasses.boolean
getLowercaseExpandedTerms()
MultiTermQuery.RewriteMethod
getMultiTermRewriteMethod()
int
getPhraseSlop()
Gets the default slop for phrases.java.util.TimeZone
getTimeZone()
void
init(Version matchVersion, java.lang.String f, Analyzer a)
Initializes a query parser.Query
parse(java.lang.String query)
Parses a query string, returning aQuery
.abstract void
ReInit(CharStream stream)
void
setAllowLeadingWildcard(boolean allowLeadingWildcard)
Set totrue
to allow leading wildcard characters.void
setAnalyzeRangeTerms(boolean analyzeRangeTerms)
Set whether or not to analyze range terms when constructingTermRangeQuery
s.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
setDateResolution(java.lang.String fieldName, DateTools.Resolution dateResolution)
Sets the date resolution used by RangeQueries for a specific field.void
setDateResolution(DateTools.Resolution dateResolution)
Sets the default date resolution used by RangeQueries for fields for which no specific date resolutions has been set.void
setDefaultOperator(QueryParser.Operator op)
Sets the boolean operator of the QueryParser.void
setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries.void
setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries.void
setLocale(java.util.Locale locale)
Set locale used by date range parsing, lowercasing, and other locale-sensitive operations.void
setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not.void
setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
By default QueryParser usesMultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
when creating aPrefixQuery
,WildcardQuery
orTermRangeQuery
.void
setPhraseSlop(int phraseSlop)
Sets the default slop for phrases.void
setTimeZone(java.util.TimeZone timeZone)
abstract Query
TopLevelQuery(java.lang.String field)
-
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
-
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
-
-
Method Detail
-
init
public void init(Version matchVersion, java.lang.String f, Analyzer a)
Initializes a query parser. Called by the QueryParser constructor- Parameters:
matchVersion
- Lucene version to match. See here.f
- the default field for query terms.a
- used to find terms in the query text.
-
ReInit
public abstract void ReInit(CharStream stream)
-
TopLevelQuery
public abstract Query TopLevelQuery(java.lang.String field) throws ParseException
- Throws:
ParseException
-
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
-
getField
public java.lang.String getField()
- Returns:
- Returns the default field.
-
getAutoGeneratePhraseQueries
public final boolean getAutoGeneratePhraseQueries()
- See Also:
setAutoGeneratePhraseQueries(boolean)
-
setAutoGeneratePhraseQueries
public final 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.
-
getFuzzyMinSim
public float getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.- Specified by:
getFuzzyMinSim
in interfaceCommonQueryParserConfiguration
-
setFuzzyMinSim
public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 2f.- Specified by:
setFuzzyMinSim
in interfaceCommonQueryParserConfiguration
-
getFuzzyPrefixLength
public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.- Specified by:
getFuzzyPrefixLength
in interfaceCommonQueryParserConfiguration
- Returns:
- Returns the fuzzyPrefixLength.
-
setFuzzyPrefixLength
public void setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.- Specified by:
setFuzzyPrefixLength
in interfaceCommonQueryParserConfiguration
- 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.- Specified by:
setPhraseSlop
in interfaceCommonQueryParserConfiguration
-
getPhraseSlop
public int getPhraseSlop()
Gets the default slop for phrases.- Specified by:
getPhraseSlop
in interfaceCommonQueryParserConfiguration
-
setAllowLeadingWildcard
public void setAllowLeadingWildcard(boolean allowLeadingWildcard)
Set totrue
to 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.
- Specified by:
setAllowLeadingWildcard
in interfaceCommonQueryParserConfiguration
-
getAllowLeadingWildcard
public boolean getAllowLeadingWildcard()
- Specified by:
getAllowLeadingWildcard
in interfaceCommonQueryParserConfiguration
- See Also:
setAllowLeadingWildcard(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 Hungary
is equal tocapital OR of OR Hungary
.
InAND_OPERATOR
mode terms are considered to be in conjunction: 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
.- Specified by:
setLowercaseExpandedTerms
in interfaceCommonQueryParserConfiguration
-
getLowercaseExpandedTerms
public boolean getLowercaseExpandedTerms()
- Specified by:
getLowercaseExpandedTerms
in interfaceCommonQueryParserConfiguration
- See Also:
setLowercaseExpandedTerms(boolean)
-
setMultiTermRewriteMethod
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
By default QueryParser usesMultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
when creating aPrefixQuery
,WildcardQuery
orTermRangeQuery
. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of terms unduly influence score c) avoids anyBooleanQuery.TooManyClauses
exception. However, if your application really needs to use the old-fashionedBooleanQuery
expansion rewriting and the above points are not relevant then use this to change the rewrite method.- Specified by:
setMultiTermRewriteMethod
in interfaceCommonQueryParserConfiguration
-
getMultiTermRewriteMethod
public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod()
- Specified by:
getMultiTermRewriteMethod
in interfaceCommonQueryParserConfiguration
- See Also:
setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
-
setLocale
public void setLocale(java.util.Locale locale)
Set locale used by date range parsing, lowercasing, and other locale-sensitive operations.- Specified by:
setLocale
in interfaceCommonQueryParserConfiguration
-
getLocale
public java.util.Locale getLocale()
Returns current locale, allowing access by subclasses.- Specified by:
getLocale
in interfaceCommonQueryParserConfiguration
-
setTimeZone
public void setTimeZone(java.util.TimeZone timeZone)
- Specified by:
setTimeZone
in interfaceCommonQueryParserConfiguration
-
getTimeZone
public java.util.TimeZone getTimeZone()
- Specified by:
getTimeZone
in interfaceCommonQueryParserConfiguration
-
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, org.apache.lucene.document.DateTools.Resolution)
.- Specified by:
setDateResolution
in interfaceCommonQueryParserConfiguration
- 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.
-
setAnalyzeRangeTerms
public void setAnalyzeRangeTerms(boolean analyzeRangeTerms)
Set whether or not to analyze range terms when constructingTermRangeQuery
s. For example, setting this to true can enable analyzing terms into collation keys for locale-sensitiveTermRangeQuery
.- Parameters:
analyzeRangeTerms
- whether or not terms should be analyzed for RangeQuerys
-
getAnalyzeRangeTerms
public boolean getAnalyzeRangeTerms()
- Returns:
- whether or not to analyze range terms when constructing
TermRangeQuery
s.
-
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\
.
-
-