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/interfaces inherited from class org.apache.lucene.util.QueryBuilder
QueryBuilder.TermAndBoost
-
-
Field Summary
Fields Modifier and Type Field Description static QueryParser.OperatorAND_OPERATORAlternative form of QueryParser.Operator.ANDprotected Stringfieldstatic QueryParser.OperatorOR_OPERATORAlternative form of QueryParser.Operator.OR-
Fields inherited from class org.apache.lucene.util.QueryBuilder
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedQueryParserBase()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddClause(List<BooleanClause> clauses, int conj, int mods, Query q)protected voidaddMultiTermClauses(List<BooleanClause> clauses, Query q)Adds clauses generated from analysis over text containing whitespace.static Stringescape(String s)Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding\.booleangetAllowLeadingWildcard()booleangetAutoGeneratePhraseQueries()protected QuerygetBooleanQuery(List<BooleanClause> clauses)Factory method for generating query, given a set of clauses.DateTools.ResolutiongetDateResolution(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.intgetDeterminizeWorkLimit()StringgetField()protected QuerygetFieldQuery(String field, String queryText, boolean quoted)protected QuerygetFieldQuery(String field, String queryText, int slop)Base implementation delegates togetFieldQuery(String,String,boolean).protected floatgetFuzzyDistance(Token fuzzyToken, String termStr)Determines the similarity distance for the given fuzzy token and term string.floatgetFuzzyMinSim()Get the minimal similarity for fuzzy queries.intgetFuzzyPrefixLength()Get the prefix length for fuzzy queries.protected QuerygetFuzzyQuery(String field, String termStr, float minSimilarity)Factory method for generating a query (similar togetWildcardQuery(java.lang.String, java.lang.String)).LocalegetLocale()Returns current locale, allowing access by subclasses.MultiTermQuery.RewriteMethodgetMultiTermRewriteMethod()intgetPhraseSlop()Gets the default slop for phrases.protected QuerygetPrefixQuery(String field, String termStr)Factory method for generating a query (similar togetWildcardQuery(java.lang.String, java.lang.String)).protected QuerygetRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive)protected QuerygetRegexpQuery(String field, String termStr)Factory method for generating a query.TimeZonegetTimeZone()protected QuerygetWildcardQuery(String field, String termStr)Factory method for generating a query.voidinit(String f, Analyzer a)Initializes a query parser.protected BooleanClausenewBooleanClause(Query q, BooleanClause.Occur occur)Builds a new BooleanClause instanceprotected QuerynewFieldQuery(Analyzer analyzer, String field, String queryText, boolean quoted)protected QuerynewFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)Builds a new FuzzyQuery instanceprotected QuerynewMatchAllDocsQuery()Builds a new MatchAllDocsQuery instanceprotected QuerynewPrefixQuery(Term prefix)Builds a new PrefixQuery instanceprotected QuerynewRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive)Builds a newTermRangeQueryinstanceprotected QuerynewRegexpQuery(Term regexp)Builds a new RegexpQuery instanceprotected QuerynewWildcardQuery(Term t)Builds a new WildcardQuery instanceQueryparse(String query)Parses a query string, returning aQuery.abstract voidReInit(CharStream stream)voidsetAllowLeadingWildcard(boolean allowLeadingWildcard)Set totrueto allow leading wildcard characters.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.voidsetDateResolution(String fieldName, DateTools.Resolution dateResolution)Sets the date resolution used by RangeQueries for a specific field.voidsetDateResolution(DateTools.Resolution dateResolution)Sets the default date resolution used by RangeQueries for fields for which no specific date resolutions has been set.voidsetDefaultOperator(QueryParser.Operator op)Sets the boolean operator of the QueryParser.voidsetDeterminizeWorkLimit(int determinizeWorkLimit)voidsetFuzzyMinSim(float fuzzyMinSim)Set the minimum similarity for fuzzy queries.voidsetFuzzyPrefixLength(int fuzzyPrefixLength)Set the prefix length for fuzzy queries.voidsetLocale(Locale locale)Set locale used by date range parsing, lowercasing, and other locale-sensitive operations.voidsetMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)By default QueryParser usesMultiTermQuery.CONSTANT_SCORE_BLENDED_REWRITEwhen creating aPrefixQuery,WildcardQueryorTermRangeQuery.voidsetPhraseSlop(int phraseSlop)Sets the default slop for phrases.voidsetTimeZone(TimeZone timeZone)abstract QueryTopLevelQuery(String field)-
Methods inherited from class org.apache.lucene.util.QueryBuilder
add, analyzeBoolean, analyzeGraphBoolean, analyzeGraphPhrase, analyzeMultiBoolean, analyzeMultiPhrase, analyzePhrase, analyzeTerm, createBooleanQuery, createBooleanQuery, createFieldQuery, createFieldQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, 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
-
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
-
field
protected String field
-
-
Method Detail
-
init
public void init(String f, Analyzer a)
Initializes a query parser. Called by the QueryParser constructor- Parameters:
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(String field) throws ParseException
- Throws:
ParseException
-
parse
public Query parse(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 String getField()
- Returns:
- Returns the default field.
-
getAutoGeneratePhraseQueries
public final boolean getAutoGeneratePhraseQueries()
- See Also:
setAutoGeneratePhraseQueries(boolean)
-
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.
-
getFuzzyMinSim
public float getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.- Specified by:
getFuzzyMinSimin interfaceCommonQueryParserConfiguration
-
setFuzzyMinSim
public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 2f.- Specified by:
setFuzzyMinSimin interfaceCommonQueryParserConfiguration
-
getFuzzyPrefixLength
public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.- Specified by:
getFuzzyPrefixLengthin interfaceCommonQueryParserConfiguration- Returns:
- Returns the fuzzyPrefixLength.
-
setFuzzyPrefixLength
public void setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.- Specified by:
setFuzzyPrefixLengthin 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:
setPhraseSlopin interfaceCommonQueryParserConfiguration
-
getPhraseSlop
public int getPhraseSlop()
Gets the default slop for phrases.- Specified by:
getPhraseSlopin interfaceCommonQueryParserConfiguration
-
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.
- Specified by:
setAllowLeadingWildcardin interfaceCommonQueryParserConfiguration
-
getAllowLeadingWildcard
public boolean getAllowLeadingWildcard()
- Specified by:
getAllowLeadingWildcardin 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 Hungaryis equal tocapital OR of OR Hungary.
InAND_OPERATORmode 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.
-
setMultiTermRewriteMethod
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
Description copied from interface:CommonQueryParserConfigurationBy default QueryParser usesMultiTermQuery.CONSTANT_SCORE_BLENDED_REWRITEwhen creating aPrefixQuery,WildcardQueryorTermRangeQuery. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of terms unduly influence score c) avoids anyIndexSearcher.TooManyClausesexception. However, if your application really needs to use the old-fashionedBooleanQueryexpansion rewriting and the above points are not relevant then use this to change the rewrite method. As another alternative, if you prefer all terms to be rewritten as a filter up-front, you can useMultiTermQuery.CONSTANT_SCORE_REWRITE. For more information on the different rewrite methods available, seeMultiTermQuerydocumentation.- Specified by:
setMultiTermRewriteMethodin interfaceCommonQueryParserConfiguration
-
getMultiTermRewriteMethod
public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod()
- Specified by:
getMultiTermRewriteMethodin interfaceCommonQueryParserConfiguration- See Also:
setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
-
setLocale
public void setLocale(Locale locale)
Set locale used by date range parsing, lowercasing, and other locale-sensitive operations.- Specified by:
setLocalein interfaceCommonQueryParserConfiguration
-
getLocale
public Locale getLocale()
Returns current locale, allowing access by subclasses.- Specified by:
getLocalein interfaceCommonQueryParserConfiguration
-
setTimeZone
public void setTimeZone(TimeZone timeZone)
- Specified by:
setTimeZonein interfaceCommonQueryParserConfiguration
-
getTimeZone
public TimeZone getTimeZone()
- Specified by:
getTimeZonein 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:
setDateResolutionin interfaceCommonQueryParserConfiguration- Parameters:
dateResolution- the default date resolution to set
-
setDateResolution
public void setDateResolution(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(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.
-
setDeterminizeWorkLimit
public void setDeterminizeWorkLimit(int determinizeWorkLimit)
- Parameters:
determinizeWorkLimit- the maximum effort that determinizing a regexp query can spend. If the query requires more effort, a TooComplexToDeterminizeException is thrown.
-
getDeterminizeWorkLimit
public int getDeterminizeWorkLimit()
- Returns:
- the maximum effort that determinizing a regexp query can spend. If the query requires more effort, a TooComplexToDeterminizeException is thrown.
-
addClause
protected void addClause(List<BooleanClause> clauses, int conj, int mods, Query q)
-
addMultiTermClauses
protected void addMultiTermClauses(List<BooleanClause> clauses, Query q)
Adds clauses generated from analysis over text containing whitespace. There are no operators, so the query's clauses can either be MUST (if the default operator is AND) or SHOULD (default OR).If all of the clauses in the given Query are TermQuery-s, this method flattens the result by adding the TermQuery-s individually to the output clause list; otherwise, the given Query is added as a single clause including its nested clauses.
-
getFieldQuery
protected Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException
- Throws:
ParseException- throw in overridden method to disallow
-
newFieldQuery
protected Query newFieldQuery(Analyzer analyzer, String field, String queryText, boolean quoted) throws ParseException
- Throws:
ParseException- throw in overridden method to disallow
-
getFieldQuery
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException
Base implementation delegates togetFieldQuery(String,String,boolean). This method may be overridden, for example, to return a SpanNearQuery instead of a PhraseQuery.- Throws:
ParseException- throw in overridden method to disallow
-
getRangeQuery
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException
- Throws:
ParseException
-
newBooleanClause
protected BooleanClause newBooleanClause(Query q, BooleanClause.Occur occur)
Builds a new BooleanClause instance- Parameters:
q- sub queryoccur- how this clause should occur when matching documents- Returns:
- new BooleanClause instance
-
newPrefixQuery
protected Query newPrefixQuery(Term prefix)
Builds a new PrefixQuery instance- Parameters:
prefix- Prefix term- Returns:
- new PrefixQuery instance
-
newRegexpQuery
protected Query newRegexpQuery(Term regexp)
Builds a new RegexpQuery instance- Parameters:
regexp- Regexp term- Returns:
- new RegexpQuery instance
-
newFuzzyQuery
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)
Builds a new FuzzyQuery instance- Parameters:
term- TermminimumSimilarity- minimum similarityprefixLength- prefix length- Returns:
- new FuzzyQuery Instance
-
newRangeQuery
protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive)
Builds a newTermRangeQueryinstance- Parameters:
field- Fieldpart1- minpart2- maxstartInclusive- true if the start of the range is inclusiveendInclusive- true if the end of the range is inclusive- Returns:
- new
TermRangeQueryinstance
-
newMatchAllDocsQuery
protected Query newMatchAllDocsQuery()
Builds a new MatchAllDocsQuery instance- Returns:
- new MatchAllDocsQuery instance
-
newWildcardQuery
protected Query newWildcardQuery(Term t)
Builds a new WildcardQuery instance- Parameters:
t- wildcard term- Returns:
- new WildcardQuery instance
-
getBooleanQuery
protected Query getBooleanQuery(List<BooleanClause> clauses) throws ParseException
Factory method for generating query, given a set of clauses. By default creates a boolean query composed of clauses passed in.Can be overridden by extending classes, to modify query being returned.
- Parameters:
clauses- List that containsBooleanClauseinstances to join.- Returns:
- Resulting
Queryobject. - Throws:
ParseException- throw in overridden method to disallow
-
getWildcardQuery
protected Query getWildcardQuery(String field, String termStr) throws ParseException
Factory method for generating a query. Called when parser parses an input term token that contains one or more wildcard characters (? and *), but is not a prefix term token (one that has just a single * character at the end)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.
- Parameters:
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 term- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getRegexpQuery
protected Query getRegexpQuery(String field, String termStr) throws ParseException
Factory method for generating a query. Called when parser parses an input term token that contains a regular expression query.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.
- Parameters:
field- Name of the field query will use.termStr- Term token that contains a regular expression- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getPrefixQuery
protected Query getPrefixQuery(String field, String termStr) throws ParseException
Factory method for generating a query (similar togetWildcardQuery(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.
- Parameters:
field- Name of the field query will use.termStr- Term token to use for building term for the query (without trailing '*' character!)- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getFuzzyQuery
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException
Factory method for generating a query (similar togetWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term token that has the fuzzy suffix (~) appended.- Parameters:
field- Name of the field query will use.termStr- Term token to use for building term for the query- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getFuzzyDistance
protected float getFuzzyDistance(Token fuzzyToken, String termStr)
Determines the similarity distance for the given fuzzy token and term string.The default implementation uses the string image of the
fuzzyTokenin an attempt to parse it to a primitive float value. Otherwise, the minimal similarity distance is returned. Subclasses can override this method to return a similarity distance, say based on thetermStr, if thefuzzyTokendoes not specify a distance.- Parameters:
fuzzyToken- The Fuzzy tokentermStr- The Term string- Returns:
- The similarity distance
-
-