Class StandardQueryParser
- java.lang.Object
 - 
- org.apache.lucene.queryparser.flexible.core.QueryParserHelper
 - 
- org.apache.lucene.queryparser.flexible.standard.StandardQueryParser
 
 
 
- 
- All Implemented Interfaces:
 CommonQueryParserConfiguration
- Direct Known Subclasses:
 PrecedenceQueryParser
public class StandardQueryParser extends QueryParserHelper implements CommonQueryParserConfiguration
This class is a helper that enables users to easily use the Lucene query parser.
To construct a Query object from a query string, use the
parse(String, String)method:- 
 StandardQueryParser queryParserHelper = new StandardQueryParser(); 
 
Query query = queryParserHelper.parse("a AND b", "defaultField");To change any configuration before parsing the query string do, for example:
- 
 // the query config handler returned by 
 
StandardQueryParseris aStandardQueryConfigHandler
queryParserHelper.getQueryConfigHandler().setAnalyzer(new WhitespaceAnalyzer());The syntax for query strings is as follows (copied from the old QueryParser javadoc):
- 
 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.
The text parser used by this helper is a
StandardSyntaxParser.The query node processor used by this helper is a
StandardQueryNodeProcessorPipeline.The builder used by this helper is a
StandardQueryTreeBuilder. 
- 
- 
Constructor Summary
Constructors Constructor Description StandardQueryParser()Constructs aStandardQueryParserobject.StandardQueryParser(Analyzer analyzer)Constructs aStandardQueryParserobject and sets anAnalyzerto it. 
- 
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleangetAllowLeadingWildcard()AnalyzergetAnalyzer()DateTools.ResolutiongetDateResolution()Returns the defaultDateTools.Resolutionused for certain field when noDateTools.Resolutionis defined for this field.java.util.Map<java.lang.CharSequence,DateTools.Resolution>getDateResolutionMap()Returns the field toDateTools.Resolutionmap used to normalize each date field.StandardQueryConfigHandler.OperatorgetDefaultOperator()Gets implicit operator setting, which will be eitherStandardQueryConfigHandler.Operator.ANDorStandardQueryConfigHandler.Operator.OR.booleangetEnablePositionIncrements()java.util.Map<java.lang.String,java.lang.Float>getFieldsBoost()Returns the field to boost map used to set boost for each field.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()voidgetMultiFields(java.lang.CharSequence[] fields)Returns the fields used to expand the query when the field for a certain query isnullMultiTermQuery.RewriteMethodgetMultiTermRewriteMethod()java.util.Map<java.lang.String,NumericConfig>getNumericConfigMap()intgetPhraseSlop()Gets the default slop for phrases.java.util.TimeZonegetTimeZone()Queryparse(java.lang.String query, java.lang.String defaultField)OverridesQueryParserHelper.parse(String, String)so it casts the return object toQuery.voidsetAllowLeadingWildcard(boolean allowLeadingWildcard)Set totrueto allow leading wildcard characters.voidsetAnalyzer(Analyzer analyzer)voidsetDateResolution(java.util.Map<java.lang.CharSequence,DateTools.Resolution> dateRes)Deprecated.this method was renamed tosetDateResolutionMap(Map)voidsetDateResolution(DateTools.Resolution dateResolution)Sets the defaultDateTools.Resolutionused for certain field when noDateTools.Resolutionis defined for this field.voidsetDateResolutionMap(java.util.Map<java.lang.CharSequence,DateTools.Resolution> dateRes)Sets theDateTools.Resolutionused for each fieldvoidsetDefaultOperator(StandardQueryConfigHandler.Operator operator)Sets the boolean operator of the QueryParser.voidsetDefaultPhraseSlop(int defaultPhraseSlop)Deprecated.renamed tosetPhraseSlop(int)voidsetEnablePositionIncrements(boolean enabled)Set totrueto enable position increments in result query.voidsetFieldsBoost(java.util.Map<java.lang.String,java.lang.Float> boosts)Sets the boost used for each field.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)Set totrueto allow leading wildcard characters.voidsetMultiFields(java.lang.CharSequence[] fields)Set the fields a query should be expanded to when the field isnullvoidsetMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)By default, it usesMultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULTwhen creating a prefix, wildcard and range queries.voidsetNumericConfigMap(java.util.Map<java.lang.String,NumericConfig> numericConfigMap)voidsetPhraseSlop(int defaultPhraseSlop)Sets the default slop for phrases.voidsetTimeZone(java.util.TimeZone timeZone)java.lang.StringtoString()- 
Methods inherited from class org.apache.lucene.queryparser.flexible.core.QueryParserHelper
getQueryBuilder, getQueryConfigHandler, getQueryNodeProcessor, getSyntaxParser, setQueryBuilder, setQueryConfigHandler, setQueryNodeProcessor, setSyntaxParser 
 - 
 
 - 
 
- 
- 
Constructor Detail
- 
StandardQueryParser
public StandardQueryParser()
Constructs aStandardQueryParserobject. 
- 
StandardQueryParser
public StandardQueryParser(Analyzer analyzer)
Constructs aStandardQueryParserobject and sets anAnalyzerto it. The same as:- 
 StandardQueryParser qp = new StandardQueryParser();
 qp.getQueryConfigHandler().setAnalyzer(analyzer);
 
 
- Parameters:
 analyzer- the analyzer to be used by this query parser helper
 
 - 
 
- 
Method Detail
- 
toString
public java.lang.String toString()
- Overrides:
 toStringin classjava.lang.Object
 
- 
parse
public Query parse(java.lang.String query, java.lang.String defaultField) throws QueryNodeException
OverridesQueryParserHelper.parse(String, String)so it casts the return object toQuery. For more reference about this method, checkQueryParserHelper.parse(String, String).- Overrides:
 parsein classQueryParserHelper- Parameters:
 query- the query stringdefaultField- the default field used by the text parser- Returns:
 - the object built from the query
 - Throws:
 QueryNodeException- if something wrong happens along the three phases
 
- 
getDefaultOperator
public StandardQueryConfigHandler.Operator getDefaultOperator()
Gets implicit operator setting, which will be eitherStandardQueryConfigHandler.Operator.ANDorStandardQueryConfigHandler.Operator.OR. 
- 
setDefaultOperator
public void setDefaultOperator(StandardQueryConfigHandler.Operator operator)
Sets the boolean operator of the QueryParser. In default mode (StandardQueryConfigHandler.Operator.OR) terms without any modifiers are considered optional: for examplecapital of Hungaryis equal tocapital OR of OR Hungary.
InStandardQueryConfigHandler.Operator.ANDmode terms are considered to be in conjunction: the above mentioned query is parsed ascapital AND of AND Hungary 
- 
setLowercaseExpandedTerms
public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
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:
 setLowercaseExpandedTermsin interfaceCommonQueryParserConfiguration
 
- 
getLowercaseExpandedTerms
public boolean getLowercaseExpandedTerms()
- Specified by:
 getLowercaseExpandedTermsin interfaceCommonQueryParserConfiguration- See Also:
 setLowercaseExpandedTerms(boolean)
 
- 
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
 
- 
setEnablePositionIncrements
public void setEnablePositionIncrements(boolean enabled)
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.
- Specified by:
 setEnablePositionIncrementsin interfaceCommonQueryParserConfiguration
 
- 
getEnablePositionIncrements
public boolean getEnablePositionIncrements()
- Specified by:
 getEnablePositionIncrementsin interfaceCommonQueryParserConfiguration- See Also:
 setEnablePositionIncrements(boolean)
 
- 
setMultiTermRewriteMethod
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
By default, it usesMultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULTwhen creating a prefix, wildcard and range queries. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of terms unduly influence score c) avoids anyTooManyListenersExceptionexception. However, if your application really needs to use the old-fashioned boolean queries expansion rewriting and the above points are not relevant then use this change the rewrite method.- Specified by:
 setMultiTermRewriteMethodin interfaceCommonQueryParserConfiguration
 
- 
getMultiTermRewriteMethod
public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod()
- Specified by:
 getMultiTermRewriteMethodin interfaceCommonQueryParserConfiguration- See Also:
 setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
 
- 
setMultiFields
public void setMultiFields(java.lang.CharSequence[] fields)
Set the fields a query should be expanded to when the field isnull- Parameters:
 fields- the fields used to expand the query
 
- 
getMultiFields
public void getMultiFields(java.lang.CharSequence[] fields)
Returns the fields used to expand the query when the field for a certain query isnull- Parameters:
 fields- the fields used to expand the query
 
- 
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.
 
- 
setNumericConfigMap
public void setNumericConfigMap(java.util.Map<java.lang.String,NumericConfig> numericConfigMap)
 
- 
getNumericConfigMap
public java.util.Map<java.lang.String,NumericConfig> getNumericConfigMap()
 
- 
setLocale
public void setLocale(java.util.Locale locale)
Set locale used by date range parsing.- Specified by:
 setLocalein interfaceCommonQueryParserConfiguration
 
- 
getLocale
public java.util.Locale getLocale()
Returns current locale, allowing access by subclasses.- Specified by:
 getLocalein interfaceCommonQueryParserConfiguration
 
- 
setTimeZone
public void setTimeZone(java.util.TimeZone timeZone)
- Specified by:
 setTimeZonein interfaceCommonQueryParserConfiguration
 
- 
getTimeZone
public java.util.TimeZone getTimeZone()
- Specified by:
 getTimeZonein interfaceCommonQueryParserConfiguration
 
- 
setDefaultPhraseSlop
@Deprecated public void setDefaultPhraseSlop(int defaultPhraseSlop)
Deprecated.renamed tosetPhraseSlop(int)Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero. 
- 
setPhraseSlop
public void setPhraseSlop(int defaultPhraseSlop)
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.- Specified by:
 setPhraseSlopin interfaceCommonQueryParserConfiguration
 
- 
setAnalyzer
public void setAnalyzer(Analyzer analyzer)
 
- 
getAnalyzer
public Analyzer getAnalyzer()
- Specified by:
 getAnalyzerin interfaceCommonQueryParserConfiguration
 
- 
getAllowLeadingWildcard
public boolean getAllowLeadingWildcard()
- Specified by:
 getAllowLeadingWildcardin interfaceCommonQueryParserConfiguration- See Also:
 setAllowLeadingWildcard(boolean)
 
- 
getFuzzyMinSim
public float getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.- Specified by:
 getFuzzyMinSimin interfaceCommonQueryParserConfiguration
 
- 
getFuzzyPrefixLength
public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.- Specified by:
 getFuzzyPrefixLengthin interfaceCommonQueryParserConfiguration- Returns:
 - Returns the fuzzyPrefixLength.
 
 
- 
getPhraseSlop
public int getPhraseSlop()
Gets the default slop for phrases.- Specified by:
 getPhraseSlopin interfaceCommonQueryParserConfiguration
 
- 
setFuzzyMinSim
public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is defined onFuzzyQuery.defaultMinSimilarity.- Specified by:
 setFuzzyMinSimin interfaceCommonQueryParserConfiguration
 
- 
setFieldsBoost
public void setFieldsBoost(java.util.Map<java.lang.String,java.lang.Float> boosts)
Sets the boost used for each field.- Parameters:
 boosts- a collection that maps a field to its boost
 
- 
getFieldsBoost
public java.util.Map<java.lang.String,java.lang.Float> getFieldsBoost()
Returns the field to boost map used to set boost for each field.- Returns:
 - the field to boost map
 
 
- 
setDateResolution
public void setDateResolution(DateTools.Resolution dateResolution)
Sets the defaultDateTools.Resolutionused for certain field when noDateTools.Resolutionis defined for this field.- Specified by:
 setDateResolutionin interfaceCommonQueryParserConfiguration- Parameters:
 dateResolution- the defaultDateTools.Resolution
 
- 
getDateResolution
public DateTools.Resolution getDateResolution()
Returns the defaultDateTools.Resolutionused for certain field when noDateTools.Resolutionis defined for this field.- Returns:
 - the default 
DateTools.Resolution 
 
- 
setDateResolution
@Deprecated public void setDateResolution(java.util.Map<java.lang.CharSequence,DateTools.Resolution> dateRes)
Deprecated.this method was renamed tosetDateResolutionMap(Map)Sets theDateTools.Resolutionused for each field- Parameters:
 dateRes- a collection that maps a field to itsDateTools.Resolution
 
- 
getDateResolutionMap
public java.util.Map<java.lang.CharSequence,DateTools.Resolution> getDateResolutionMap()
Returns the field toDateTools.Resolutionmap used to normalize each date field.- Returns:
 - the field to 
DateTools.Resolutionmap 
 
- 
setDateResolutionMap
public void setDateResolutionMap(java.util.Map<java.lang.CharSequence,DateTools.Resolution> dateRes)
Sets theDateTools.Resolutionused for each field- Parameters:
 dateRes- a collection that maps a field to itsDateTools.Resolution
 
 - 
 
 -