public abstract class QueryParserBase extends QueryBuilder implements CommonQueryParserConfiguration
| Modifier and Type | Field and Description |
|---|---|
(package private) boolean |
allowLeadingWildcard |
static QueryParser.Operator |
AND_OPERATOR
Alternative form of QueryParser.Operator.AND
|
(package private) boolean |
autoGeneratePhraseQueries |
(package private) static int |
CONJ_AND |
(package private) static int |
CONJ_NONE |
(package private) static int |
CONJ_OR |
(package private) DateTools.Resolution |
dateResolution |
protected java.lang.String |
field |
(package private) java.util.Map<java.lang.String,DateTools.Resolution> |
fieldToDateResolution |
(package private) float |
fuzzyMinSim |
(package private) int |
fuzzyPrefixLength |
(package private) java.util.Locale |
locale |
(package private) int |
maxDeterminizedStates |
(package private) static int |
MOD_NONE |
(package private) static int |
MOD_NOT |
(package private) static int |
MOD_REQ |
(package private) MultiTermQuery.RewriteMethod |
multiTermRewriteMethod |
(package private) QueryParser.Operator |
operator
The actual operator that parser uses to combine query terms
|
static QueryParser.Operator |
OR_OPERATOR
Alternative form of QueryParser.Operator.OR
|
(package private) int |
phraseSlop |
(package private) java.util.TimeZone |
timeZone |
private static java.util.regex.Pattern |
WILDCARD_PATTERN |
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements| Modifier | Constructor and Description |
|---|---|
protected |
QueryParserBase() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addClause(java.util.List<BooleanClause> clauses,
int conj,
int mods,
Query q) |
protected void |
addMultiTermClauses(java.util.List<BooleanClause> clauses,
Query q)
Adds clauses generated from analysis over text containing whitespace.
|
private PhraseQuery |
addSlopToPhrase(PhraseQuery query,
int slop)
Rebuild a phrase query with a slop value
|
private BytesRef |
analyzeWildcard(java.lang.String field,
java.lang.String termStr) |
(package private) java.lang.String |
discardEscapeChar(java.lang.String input)
Returns a String where the escape char has been
removed, or kept only once if there was a double escape.
|
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 |
getAutoGeneratePhraseQueries() |
protected Query |
getBooleanQuery(java.util.List<BooleanClause> clauses)
Factory method for generating query, given a set of clauses.
|
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() |
protected Query |
getFieldQuery(java.lang.String field,
java.lang.String queryText,
boolean quoted) |
protected Query |
getFieldQuery(java.lang.String field,
java.lang.String queryText,
int slop)
Base implementation delegates to
getFieldQuery(String,String,boolean). |
float |
getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.
|
int |
getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.
|
protected Query |
getFuzzyQuery(java.lang.String field,
java.lang.String termStr,
float minSimilarity)
Factory method for generating a query (similar to
getWildcardQuery(java.lang.String, java.lang.String)). |
java.util.Locale |
getLocale()
Returns current locale, allowing access by subclasses.
|
int |
getMaxDeterminizedStates() |
MultiTermQuery.RewriteMethod |
getMultiTermRewriteMethod() |
int |
getPhraseSlop()
Gets the default slop for phrases.
|
protected Query |
getPrefixQuery(java.lang.String field,
java.lang.String termStr)
Factory method for generating a query (similar to
getWildcardQuery(java.lang.String, java.lang.String)). |
protected Query |
getRangeQuery(java.lang.String field,
java.lang.String part1,
java.lang.String part2,
boolean startInclusive,
boolean endInclusive) |
protected Query |
getRegexpQuery(java.lang.String field,
java.lang.String termStr)
Factory method for generating a query.
|
java.util.TimeZone |
getTimeZone() |
protected Query |
getWildcardQuery(java.lang.String field,
java.lang.String termStr)
Factory method for generating a query.
|
(package private) Query |
handleBareFuzzy(java.lang.String qfield,
Token fuzzySlop,
java.lang.String termImage) |
(package private) Query |
handleBareTokenQuery(java.lang.String qfield,
Token term,
Token fuzzySlop,
boolean prefix,
boolean wildcard,
boolean fuzzy,
boolean regexp) |
(package private) Query |
handleBoost(Query q,
Token boost) |
(package private) Query |
handleQuotedTerm(java.lang.String qfield,
Token term,
Token fuzzySlop) |
(package private) static int |
hexToInt(char c)
Returns the numeric value of the hexadecimal character
|
void |
init(java.lang.String f,
Analyzer a)
Initializes a query parser.
|
protected BooleanClause |
newBooleanClause(Query q,
BooleanClause.Occur occur)
Builds a new BooleanClause instance
|
protected Query |
newFieldQuery(Analyzer analyzer,
java.lang.String field,
java.lang.String queryText,
boolean quoted) |
protected Query |
newFuzzyQuery(Term term,
float minimumSimilarity,
int prefixLength)
Builds a new FuzzyQuery instance
|
protected Query |
newMatchAllDocsQuery()
Builds a new MatchAllDocsQuery instance
|
protected Query |
newPrefixQuery(Term prefix)
Builds a new PrefixQuery instance
|
protected Query |
newRangeQuery(java.lang.String field,
java.lang.String part1,
java.lang.String part2,
boolean startInclusive,
boolean endInclusive)
Builds a new
TermRangeQuery instance |
protected Query |
newRegexpQuery(Term regexp)
Builds a new RegexpQuery instance
|
protected Query |
newWildcardQuery(Term t)
Builds a new WildcardQuery instance
|
Query |
parse(java.lang.String query)
Parses a query string, returning a
Query. |
abstract void |
ReInit(CharStream stream) |
void |
setAllowLeadingWildcard(boolean allowLeadingWildcard)
Set to
true to allow leading wildcard characters. |
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(DateTools.Resolution dateResolution)
Sets the default date resolution used by RangeQueries for fields for which no
specific date resolutions has been set.
|
void |
setDateResolution(java.lang.String fieldName,
DateTools.Resolution dateResolution)
Sets the date resolution used by RangeQueries for a specific field.
|
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 |
setMaxDeterminizedStates(int maxDeterminizedStates) |
void |
setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
By default QueryParser uses
MultiTermQuery.CONSTANT_SCORE_REWRITE
when creating a PrefixQuery, WildcardQuery or TermRangeQuery. |
void |
setPhraseSlop(int phraseSlop)
Sets the default slop for phrases.
|
void |
setTimeZone(java.util.TimeZone timeZone) |
abstract Query |
TopLevelQuery(java.lang.String field) |
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, setEnablePositionIncrementsclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetAnalyzer, getEnablePositionIncrements, setEnablePositionIncrementsstatic final int CONJ_NONE
static final int CONJ_AND
static final int CONJ_OR
static final int MOD_NONE
static final int MOD_NOT
static final int MOD_REQ
public static final QueryParser.Operator AND_OPERATOR
public static final QueryParser.Operator OR_OPERATOR
QueryParser.Operator operator
MultiTermQuery.RewriteMethod multiTermRewriteMethod
boolean allowLeadingWildcard
protected java.lang.String field
int phraseSlop
float fuzzyMinSim
int fuzzyPrefixLength
java.util.Locale locale
java.util.TimeZone timeZone
DateTools.Resolution dateResolution
java.util.Map<java.lang.String,DateTools.Resolution> fieldToDateResolution
boolean autoGeneratePhraseQueries
int maxDeterminizedStates
private static final java.util.regex.Pattern WILDCARD_PATTERN
public void init(java.lang.String f,
Analyzer a)
f - the default field for query terms.a - used to find terms in the query text.public abstract void ReInit(CharStream stream)
public abstract Query TopLevelQuery(java.lang.String field) throws ParseException
ParseExceptionpublic Query parse(java.lang.String query) throws ParseException
Query.query - the query string to be parsed.ParseException - if the parsing failspublic java.lang.String getField()
public final boolean getAutoGeneratePhraseQueries()
setAutoGeneratePhraseQueries(boolean)public void setAutoGeneratePhraseQueries(boolean value)
Set to false if phrase queries should only be generated when surrounded by double quotes.
public float getFuzzyMinSim()
getFuzzyMinSim in interface CommonQueryParserConfigurationpublic void setFuzzyMinSim(float fuzzyMinSim)
setFuzzyMinSim in interface CommonQueryParserConfigurationpublic int getFuzzyPrefixLength()
getFuzzyPrefixLength in interface CommonQueryParserConfigurationpublic void setFuzzyPrefixLength(int fuzzyPrefixLength)
setFuzzyPrefixLength in interface CommonQueryParserConfigurationfuzzyPrefixLength - The fuzzyPrefixLength to set.public void setPhraseSlop(int phraseSlop)
setPhraseSlop in interface CommonQueryParserConfigurationpublic int getPhraseSlop()
getPhraseSlop in interface CommonQueryParserConfigurationpublic void setAllowLeadingWildcard(boolean allowLeadingWildcard)
true 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.
setAllowLeadingWildcard in interface CommonQueryParserConfigurationpublic boolean getAllowLeadingWildcard()
getAllowLeadingWildcard in interface CommonQueryParserConfigurationsetAllowLeadingWildcard(boolean)public void setDefaultOperator(QueryParser.Operator op)
OR_OPERATOR) terms without any modifiers
are considered optional: for example capital of Hungary is equal to
capital OR of OR Hungary.AND_OPERATOR mode terms are considered to be in conjunction: the
above mentioned query is parsed as capital AND of AND Hungarypublic QueryParser.Operator getDefaultOperator()
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
MultiTermQuery.CONSTANT_SCORE_REWRITE
when creating a PrefixQuery, WildcardQuery or TermRangeQuery. This implementation is generally preferable because it
a) Runs faster b) Does not have the scarcity of terms unduly influence score
c) avoids any BooleanQuery.TooManyClauses exception.
However, if your application really needs to use the
old-fashioned BooleanQuery expansion rewriting and the above
points are not relevant then use this to change
the rewrite method.setMultiTermRewriteMethod in interface CommonQueryParserConfigurationpublic MultiTermQuery.RewriteMethod getMultiTermRewriteMethod()
getMultiTermRewriteMethod in interface CommonQueryParserConfigurationsetMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)public void setLocale(java.util.Locale locale)
setLocale in interface CommonQueryParserConfigurationpublic java.util.Locale getLocale()
getLocale in interface CommonQueryParserConfigurationpublic void setTimeZone(java.util.TimeZone timeZone)
setTimeZone in interface CommonQueryParserConfigurationpublic java.util.TimeZone getTimeZone()
getTimeZone in interface CommonQueryParserConfigurationpublic void setDateResolution(DateTools.Resolution dateResolution)
setDateResolution(String, org.apache.lucene.document.DateTools.Resolution).setDateResolution in interface CommonQueryParserConfigurationdateResolution - the default date resolution to setpublic void setDateResolution(java.lang.String fieldName,
DateTools.Resolution dateResolution)
fieldName - field for which the date resolution is to be setdateResolution - date resolution to setpublic DateTools.Resolution getDateResolution(java.lang.String fieldName)
public void setMaxDeterminizedStates(int maxDeterminizedStates)
maxDeterminizedStates - the maximum number of states that
determinizing a regexp query can result in. If the query results in any
more states a TooComplexToDeterminizeException is thrown.public int getMaxDeterminizedStates()
protected void addClause(java.util.List<BooleanClause> clauses, int conj, int mods, Query q)
protected void addMultiTermClauses(java.util.List<BooleanClause> clauses, Query q)
protected Query getFieldQuery(java.lang.String field, java.lang.String queryText, boolean quoted) throws ParseException
ParseException - throw in overridden method to disallowprotected Query newFieldQuery(Analyzer analyzer, java.lang.String field, java.lang.String queryText, boolean quoted) throws ParseException
ParseException - throw in overridden method to disallowprotected Query getFieldQuery(java.lang.String field, java.lang.String queryText, int slop) throws ParseException
getFieldQuery(String,String,boolean).
This method may be overridden, for example, to return
a SpanNearQuery instead of a PhraseQuery.ParseException - throw in overridden method to disallowprivate PhraseQuery addSlopToPhrase(PhraseQuery query, int slop)
protected Query getRangeQuery(java.lang.String field, java.lang.String part1, java.lang.String part2, boolean startInclusive, boolean endInclusive) throws ParseException
ParseExceptionprotected BooleanClause newBooleanClause(Query q, BooleanClause.Occur occur)
q - sub queryoccur - how this clause should occur when matching documentsprotected Query newPrefixQuery(Term prefix)
prefix - Prefix termprotected Query newRegexpQuery(Term regexp)
regexp - Regexp termprotected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)
term - TermminimumSimilarity - minimum similarityprefixLength - prefix lengthprotected Query newRangeQuery(java.lang.String field, java.lang.String part1, java.lang.String part2, boolean startInclusive, boolean endInclusive)
TermRangeQuery instancefield - Fieldpart1 - minpart2 - maxstartInclusive - true if the start of the range is inclusiveendInclusive - true if the end of the range is inclusiveTermRangeQuery instanceprotected Query newMatchAllDocsQuery()
protected Query newWildcardQuery(Term t)
t - wildcard termprotected Query getBooleanQuery(java.util.List<BooleanClause> clauses) throws ParseException
clauses - List that contains BooleanClause instances
to join.Query object.ParseException - throw in overridden method to disallowprotected Query getWildcardQuery(java.lang.String field, java.lang.String termStr) throws ParseException
Depending on settings, prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.
Can be overridden by extending classes, to provide custom handling for wildcard queries, which may be necessary due to missing analyzer calls.
field - Name of the field query will use.termStr - Term token that contains one or more wild card
characters (? or *), but is not simple prefix termQuery built for the termParseException - throw in overridden method to disallowprivate BytesRef analyzeWildcard(java.lang.String field, java.lang.String termStr)
protected Query getRegexpQuery(java.lang.String field, java.lang.String termStr) throws ParseException
Depending on settings, pattern term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with regular expression templates.
Can be overridden by extending classes, to provide custom handling for regular expression queries, which may be necessary due to missing analyzer calls.
field - Name of the field query will use.termStr - Term token that contains a regular expressionQuery built for the termParseException - throw in overridden method to disallowprotected Query getPrefixQuery(java.lang.String field, java.lang.String termStr) throws ParseException
getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term
token that uses prefix notation; that is, contains a single '*' wildcard
character as its last character. Since this is a special case
of generic wildcard term, and such a query can be optimized easily,
this usually results in a different query object.
Depending on settings, a prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.
Can be overridden by extending classes, to provide custom handling for wild card queries, which may be necessary due to missing analyzer calls.
field - Name of the field query will use.termStr - Term token to use for building term for the query
(without trailing '*' character!)Query built for the termParseException - throw in overridden method to disallowprotected Query getFuzzyQuery(java.lang.String field, java.lang.String termStr, float minSimilarity) throws ParseException
getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses
an input term token that has the fuzzy suffix (~) appended.field - Name of the field query will use.termStr - Term token to use for building term for the queryQuery built for the termParseException - throw in overridden method to disallowQuery handleBareTokenQuery(java.lang.String qfield, Token term, Token fuzzySlop, boolean prefix, boolean wildcard, boolean fuzzy, boolean regexp) throws ParseException
ParseExceptionQuery handleBareFuzzy(java.lang.String qfield, Token fuzzySlop, java.lang.String termImage) throws ParseException
ParseExceptionQuery handleQuotedTerm(java.lang.String qfield, Token term, Token fuzzySlop) throws ParseException
ParseExceptionjava.lang.String discardEscapeChar(java.lang.String input)
throws ParseException
\\u0041 to A.ParseExceptionstatic final int hexToInt(char c)
throws ParseException
ParseExceptionpublic static java.lang.String escape(java.lang.String s)
\.