Class StandardQueryParser
- All Implemented Interfaces:
CommonQueryParserConfiguration
- Direct Known Subclasses:
PrecedenceQueryParser
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
StandardQueryParser
is a
StandardQueryConfigHandler
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
.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs aStandardQueryParser
object.StandardQueryParser
(Analyzer analyzer) Constructs aStandardQueryParser
object and sets anAnalyzer
to it. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns the defaultDateTools.Resolution
used for certain field when noDateTools.Resolution
is defined for this field.Returns the field toDateTools.Resolution
map used to normalize each date field.Gets implicit operator setting, which will be eitherStandardQueryConfigHandler.Operator.AND
orStandardQueryConfigHandler.Operator.OR
.boolean
Returns the field to boost map used to set boost for each field.float
Get the minimal similarity for fuzzy queries.int
Get the prefix length for fuzzy queries.Returns current locale, allowing access by subclasses.boolean
void
getMultiFields
(CharSequence[] fields) Returns the fields used to expand the query when the field for a certain query isnull
int
Gets the default slop for phrases.OverridesQueryParserHelper.parse(String, String)
so it casts the return object toQuery
.void
setAllowLeadingWildcard
(boolean allowLeadingWildcard) Set totrue
to allow leading wildcard characters.void
setAnalyzer
(Analyzer analyzer) void
setDateResolution
(Map<CharSequence, DateTools.Resolution> dateRes) Deprecated.void
setDateResolution
(DateTools.Resolution dateResolution) Sets the defaultDateTools.Resolution
used for certain field when noDateTools.Resolution
is defined for this field.void
Sets theDateTools.Resolution
used for each fieldvoid
Sets the boolean operator of the QueryParser.void
setDefaultPhraseSlop
(int defaultPhraseSlop) Deprecated.renamed tosetPhraseSlop(int)
void
setEnablePositionIncrements
(boolean enabled) Set totrue
to enable position increments in result query.void
setFieldsBoost
(Map<String, Float> boosts) Sets the boost used for each field.void
setFuzzyMinSim
(float fuzzyMinSim) Set the minimum similarity for fuzzy queries.void
setFuzzyPrefixLength
(int fuzzyPrefixLength) Set the prefix length for fuzzy queries.void
Set locale used by date range parsing.void
setLowercaseExpandedTerms
(boolean lowercaseExpandedTerms) Set totrue
to allow leading wildcard characters.void
setMultiFields
(CharSequence[] fields) Set the fields a query should be expanded to when the field isnull
void
By default, it usesMultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
when creating a prefix, wildcard and range queries.void
setNumericConfigMap
(Map<String, NumericConfig> numericConfigMap) void
setPhraseSlop
(int defaultPhraseSlop) Sets the default slop for phrases.void
setTimeZone
(TimeZone timeZone) toString()
Methods inherited from class org.apache.lucene.queryparser.flexible.core.QueryParserHelper
getQueryBuilder, getQueryConfigHandler, getQueryNodeProcessor, getSyntaxParser, setQueryBuilder, setQueryConfigHandler, setQueryNodeProcessor, setSyntaxParser
-
Constructor Details
-
StandardQueryParser
public StandardQueryParser()Constructs aStandardQueryParser
object. -
StandardQueryParser
Constructs aStandardQueryParser
object and sets anAnalyzer
to 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 Details
-
toString
-
parse
OverridesQueryParserHelper.parse(String, String)
so it casts the return object toQuery
. For more reference about this method, checkQueryParserHelper.parse(String, String)
.- Overrides:
parse
in 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
Gets implicit operator setting, which will be eitherStandardQueryConfigHandler.Operator.AND
orStandardQueryConfigHandler.Operator.OR
. -
setDefaultOperator
Sets the boolean operator of the QueryParser. In default mode (StandardQueryConfigHandler.Operator.OR
) terms without any modifiers are considered optional: for examplecapital of Hungary
is equal tocapital OR of OR Hungary
.
InStandardQueryConfigHandler.Operator.AND
mode 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 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:
setLowercaseExpandedTerms
in interfaceCommonQueryParserConfiguration
-
getLowercaseExpandedTerms
public boolean getLowercaseExpandedTerms()- Specified by:
getLowercaseExpandedTerms
in interfaceCommonQueryParserConfiguration
- See Also:
-
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
-
setEnablePositionIncrements
public void setEnablePositionIncrements(boolean enabled) Set totrue
to 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:
setEnablePositionIncrements
in interfaceCommonQueryParserConfiguration
-
getEnablePositionIncrements
public boolean getEnablePositionIncrements()- Specified by:
getEnablePositionIncrements
in interfaceCommonQueryParserConfiguration
- See Also:
-
setMultiTermRewriteMethod
By default, it usesMultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
when 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 anyTooManyListenersException
exception. 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:
setMultiTermRewriteMethod
in interfaceCommonQueryParserConfiguration
-
getMultiTermRewriteMethod
- Specified by:
getMultiTermRewriteMethod
in interfaceCommonQueryParserConfiguration
- See Also:
-
setMultiFields
Set the fields a query should be expanded to when the field isnull
- Parameters:
fields
- the fields used to expand the query
-
getMultiFields
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:
setFuzzyPrefixLength
in interfaceCommonQueryParserConfiguration
- Parameters:
fuzzyPrefixLength
- The fuzzyPrefixLength to set.
-
setNumericConfigMap
-
getNumericConfigMap
-
setLocale
Set locale used by date range parsing.- Specified by:
setLocale
in interfaceCommonQueryParserConfiguration
-
getLocale
Returns current locale, allowing access by subclasses.- Specified by:
getLocale
in interfaceCommonQueryParserConfiguration
-
setTimeZone
- Specified by:
setTimeZone
in interfaceCommonQueryParserConfiguration
-
getTimeZone
- Specified by:
getTimeZone
in interfaceCommonQueryParserConfiguration
-
setDefaultPhraseSlop
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:
setPhraseSlop
in interfaceCommonQueryParserConfiguration
-
setAnalyzer
-
getAnalyzer
- Specified by:
getAnalyzer
in interfaceCommonQueryParserConfiguration
-
getAllowLeadingWildcard
public boolean getAllowLeadingWildcard()- Specified by:
getAllowLeadingWildcard
in interfaceCommonQueryParserConfiguration
- See Also:
-
getFuzzyMinSim
public float getFuzzyMinSim()Get the minimal similarity for fuzzy queries.- Specified by:
getFuzzyMinSim
in interfaceCommonQueryParserConfiguration
-
getFuzzyPrefixLength
public int getFuzzyPrefixLength()Get the prefix length for fuzzy queries.- Specified by:
getFuzzyPrefixLength
in interfaceCommonQueryParserConfiguration
- Returns:
- Returns the fuzzyPrefixLength.
-
getPhraseSlop
public int getPhraseSlop()Gets the default slop for phrases.- Specified by:
getPhraseSlop
in interfaceCommonQueryParserConfiguration
-
setFuzzyMinSim
public void setFuzzyMinSim(float fuzzyMinSim) Set the minimum similarity for fuzzy queries. Default is defined onFuzzyQuery.defaultMinSimilarity
.- Specified by:
setFuzzyMinSim
in interfaceCommonQueryParserConfiguration
-
setFieldsBoost
Sets the boost used for each field.- Parameters:
boosts
- a collection that maps a field to its boost
-
getFieldsBoost
Returns the field to boost map used to set boost for each field.- Returns:
- the field to boost map
-
setDateResolution
Sets the defaultDateTools.Resolution
used for certain field when noDateTools.Resolution
is defined for this field.- Specified by:
setDateResolution
in interfaceCommonQueryParserConfiguration
- Parameters:
dateResolution
- the defaultDateTools.Resolution
-
getDateResolution
Returns the defaultDateTools.Resolution
used for certain field when noDateTools.Resolution
is defined for this field.- Returns:
- the default
DateTools.Resolution
-
setDateResolution
Deprecated.this method was renamed tosetDateResolutionMap(Map)
Sets theDateTools.Resolution
used for each field- Parameters:
dateRes
- a collection that maps a field to itsDateTools.Resolution
-
getDateResolutionMap
Returns the field toDateTools.Resolution
map used to normalize each date field.- Returns:
- the field to
DateTools.Resolution
map
-
setDateResolutionMap
Sets theDateTools.Resolution
used for each field- Parameters:
dateRes
- a collection that maps a field to itsDateTools.Resolution
-
setDateResolutionMap(Map)