public class StandardQueryParser extends QueryParserHelper implements CommonQueryParserConfiguration
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 byStandardQueryParser
is 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:
+
) or a minus (-
) sign, indicating that
the clause is required or prohibited respectively; or
+
/-
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 and Description |
---|
StandardQueryParser()
Constructs a
StandardQueryParser object. |
StandardQueryParser(Analyzer analyzer)
Constructs a
StandardQueryParser object and sets an
Analyzer to it. |
Modifier and Type | Method and Description |
---|---|
boolean |
getAllowLeadingWildcard() |
Analyzer |
getAnalyzer() |
DateTools.Resolution |
getDateResolution()
Returns the default
DateTools.Resolution used for certain field when
no DateTools.Resolution is defined for this field. |
Map<CharSequence,DateTools.Resolution> |
getDateResolutionMap()
Returns the field to
DateTools.Resolution map used to normalize each date field. |
StandardQueryConfigHandler.Operator |
getDefaultOperator()
Gets implicit operator setting, which will be either
StandardQueryConfigHandler.Operator.AND
or StandardQueryConfigHandler.Operator.OR . |
boolean |
getEnablePositionIncrements() |
Map<String,Float> |
getFieldsBoost()
Returns the field to boost map used to set boost for each field.
|
float |
getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.
|
int |
getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.
|
Locale |
getLocale()
Returns current locale, allowing access by subclasses.
|
boolean |
getLowercaseExpandedTerms() |
void |
getMultiFields(CharSequence[] fields)
Returns the fields used to expand the query when the field for a
certain query is
null |
MultiTermQuery.RewriteMethod |
getMultiTermRewriteMethod() |
Map<String,NumericConfig> |
getNumericConfigMap() |
int |
getPhraseSlop()
Gets the default slop for phrases.
|
TimeZone |
getTimeZone() |
Query |
parse(String query,
String defaultField)
Overrides
QueryParserHelper.parse(String, String) so it casts the
return object to Query . |
void |
setAllowLeadingWildcard(boolean allowLeadingWildcard)
Set to
true to allow leading wildcard characters. |
void |
setAnalyzer(Analyzer analyzer) |
void |
setDateResolution(DateTools.Resolution dateResolution)
Sets the default
DateTools.Resolution used for certain field when
no DateTools.Resolution is defined for this field. |
void |
setDateResolution(Map<CharSequence,DateTools.Resolution> dateRes)
Deprecated.
this method was renamed to
setDateResolutionMap(Map) |
void |
setDateResolutionMap(Map<CharSequence,DateTools.Resolution> dateRes)
Sets the
DateTools.Resolution used for each field |
void |
setDefaultOperator(StandardQueryConfigHandler.Operator operator)
Sets the boolean operator of the QueryParser.
|
void |
setDefaultPhraseSlop(int defaultPhraseSlop)
Deprecated.
renamed to
setPhraseSlop(int) |
void |
setEnablePositionIncrements(boolean enabled)
Set to
true 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 |
setLocale(Locale locale)
Set locale used by date range parsing.
|
void |
setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
Set to
true to allow leading wildcard characters. |
void |
setMultiFields(CharSequence[] fields)
Set the fields a query should be expanded to when the field is
null |
void |
setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
By default, it uses
MultiTermQuery.CONSTANT_SCORE_REWRITE 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) |
String |
toString() |
getQueryBuilder, getQueryConfigHandler, getQueryNodeProcessor, getSyntaxParser, setQueryBuilder, setQueryConfigHandler, setQueryNodeProcessor, setSyntaxParser
public StandardQueryParser()
StandardQueryParser
object.public StandardQueryParser(Analyzer analyzer)
StandardQueryParser
object and sets an
Analyzer
to it. The same as:
StandardQueryParser qp = new StandardQueryParser(); qp.getQueryConfigHandler().setAnalyzer(analyzer);
analyzer
- the analyzer to be used by this query parser helperpublic Query parse(String query, String defaultField) throws QueryNodeException
QueryParserHelper.parse(String, String)
so it casts the
return object to Query
. For more reference about this method, check
QueryParserHelper.parse(String, String)
.parse
in class QueryParserHelper
query
- the query stringdefaultField
- the default field used by the text parserQueryNodeException
- if something wrong happens along the three phasespublic StandardQueryConfigHandler.Operator getDefaultOperator()
StandardQueryConfigHandler.Operator.AND
or StandardQueryConfigHandler.Operator.OR
.public void setDefaultOperator(StandardQueryConfigHandler.Operator operator)
StandardQueryConfigHandler.Operator.OR
) terms without any modifiers are considered optional:
for example capital of Hungary
is equal to
capital OR of OR Hungary
.StandardQueryConfigHandler.Operator.AND
mode terms are considered to be in conjunction: the
above mentioned query is parsed as capital AND of AND Hungary
public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
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.
setLowercaseExpandedTerms
in interface CommonQueryParserConfiguration
public boolean getLowercaseExpandedTerms()
getLowercaseExpandedTerms
in interface CommonQueryParserConfiguration
setLowercaseExpandedTerms(boolean)
public 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 CommonQueryParserConfiguration
public void setEnablePositionIncrements(boolean enabled)
true
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.
setEnablePositionIncrements
in interface CommonQueryParserConfiguration
public boolean getEnablePositionIncrements()
getEnablePositionIncrements
in interface CommonQueryParserConfiguration
setEnablePositionIncrements(boolean)
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
MultiTermQuery.CONSTANT_SCORE_REWRITE
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 any TooManyListenersException
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.setMultiTermRewriteMethod
in interface CommonQueryParserConfiguration
public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod()
getMultiTermRewriteMethod
in interface CommonQueryParserConfiguration
setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
public void setMultiFields(CharSequence[] fields)
null
fields
- the fields used to expand the querypublic void getMultiFields(CharSequence[] fields)
null
fields
- the fields used to expand the querypublic void setFuzzyPrefixLength(int fuzzyPrefixLength)
setFuzzyPrefixLength
in interface CommonQueryParserConfiguration
fuzzyPrefixLength
- The fuzzyPrefixLength to set.public void setNumericConfigMap(Map<String,NumericConfig> numericConfigMap)
public Map<String,NumericConfig> getNumericConfigMap()
public void setLocale(Locale locale)
setLocale
in interface CommonQueryParserConfiguration
public Locale getLocale()
getLocale
in interface CommonQueryParserConfiguration
public void setTimeZone(TimeZone timeZone)
setTimeZone
in interface CommonQueryParserConfiguration
public TimeZone getTimeZone()
getTimeZone
in interface CommonQueryParserConfiguration
@Deprecated public void setDefaultPhraseSlop(int defaultPhraseSlop)
setPhraseSlop(int)
public void setPhraseSlop(int defaultPhraseSlop)
setPhraseSlop
in interface CommonQueryParserConfiguration
public void setAnalyzer(Analyzer analyzer)
public Analyzer getAnalyzer()
getAnalyzer
in interface CommonQueryParserConfiguration
public boolean getAllowLeadingWildcard()
getAllowLeadingWildcard
in interface CommonQueryParserConfiguration
setAllowLeadingWildcard(boolean)
public float getFuzzyMinSim()
getFuzzyMinSim
in interface CommonQueryParserConfiguration
public int getFuzzyPrefixLength()
getFuzzyPrefixLength
in interface CommonQueryParserConfiguration
public int getPhraseSlop()
getPhraseSlop
in interface CommonQueryParserConfiguration
public void setFuzzyMinSim(float fuzzyMinSim)
FuzzyQuery.defaultMinSimilarity
.setFuzzyMinSim
in interface CommonQueryParserConfiguration
public void setFieldsBoost(Map<String,Float> boosts)
boosts
- a collection that maps a field to its boostpublic Map<String,Float> getFieldsBoost()
public void setDateResolution(DateTools.Resolution dateResolution)
DateTools.Resolution
used for certain field when
no DateTools.Resolution
is defined for this field.setDateResolution
in interface CommonQueryParserConfiguration
dateResolution
- the default DateTools.Resolution
public DateTools.Resolution getDateResolution()
DateTools.Resolution
used for certain field when
no DateTools.Resolution
is defined for this field.DateTools.Resolution
@Deprecated public void setDateResolution(Map<CharSequence,DateTools.Resolution> dateRes)
setDateResolutionMap(Map)
DateTools.Resolution
used for each fielddateRes
- a collection that maps a field to its DateTools.Resolution
public Map<CharSequence,DateTools.Resolution> getDateResolutionMap()
DateTools.Resolution
map used to normalize each date field.DateTools.Resolution
mappublic void setDateResolutionMap(Map<CharSequence,DateTools.Resolution> dateRes)
DateTools.Resolution
used for each fielddateRes
- a collection that maps a field to its DateTools.Resolution
Copyright © 2000-2015 Apache Software Foundation. All Rights Reserved.