java.lang.Object | |
↳ | org.apache.lucene.queryParser.QueryParser |
Known Direct Subclasses |
This class is generated by JavaCC. The most important method is
parse(String)
.
The syntax for query strings is as follows:
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.
In TermRangeQuery
s, QueryParser tries to detect date values, e.g.
date:[6/1/2005 TO 6/4/2005] produces a range query that searches
for "date" fields between 2005-06-01 and 2005-06-04. Note that the format
of the accepted input depends on the locale
.
By default a date is converted into a search term using the deprecated
DateField
for compatibility reasons.
To use the new DateTools
to convert dates, a
DateTools.Resolution
has to be set.
The date resolution that shall be used for RangeQueries can be set
using setDateResolution(DateTools.Resolution)
or setDateResolution(String, DateTools.Resolution)
. The former
sets the default date resolution for all fields, whereas the latter can
be used to set field specific date resolutions. Field specific date
resolutions take, if set, precedence over the default date resolution.
If you use neither DateField
nor DateTools
in your
index, you can create your own
query parser that inherits QueryParser and overwrites
getRangeQuery(String, String, String, boolean)
to
use a different method for date conversion.
Note that QueryParser is not thread-safe.
NOTE: there is a new QueryParser in contrib, which matches the same syntax as this class, but is more modular, enabling substantial customization to how a query is created.
NOTE: You must specify the required Version
compatibility when creating QueryParser:
setEnablePositionIncrements(boolean)
is true by
default.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
QueryParser.Operator | The default operator for parsing queries. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From interface
org.apache.lucene.queryParser.QueryParserConstants
|
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AND_OPERATOR | Alternative form of QueryParser.Operator.AND | ||||||||||
OR_OPERATOR | Alternative form of QueryParser.Operator.OR | ||||||||||
jj_nt | Next token. | ||||||||||
token | Current token. | ||||||||||
token_source | Generated Token Manager. |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From interface
org.apache.lucene.queryParser.QueryParserConstants
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a query parser.
|
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructor with user supplied CharStream.
| |||||||||||
Constructor with generated Token Manager.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Reinitialise.
| |||||||||||
Reinitialise.
| |||||||||||
Disable tracing.
| |||||||||||
Enable tracing.
| |||||||||||
Returns a String where those characters that QueryParser
expects to be escaped are escaped by a preceding
\ . | |||||||||||
Generate ParseException.
| |||||||||||
Returns the date resolution that is used by RangeQueries for the given field.
| |||||||||||
Gets implicit operator setting, which will be either AND_OPERATOR
or OR_OPERATOR.
| |||||||||||
Get the minimal similarity for fuzzy queries.
| |||||||||||
Get the prefix length for fuzzy queries.
| |||||||||||
Returns current locale, allowing access by subclasses.
| |||||||||||
Get the next Token.
| |||||||||||
Gets the default slop for phrases.
| |||||||||||
Get the specific Token.
| |||||||||||
Command line tool to test QueryParser, using
SimpleAnalyzer . | |||||||||||
Parses a query string, returning a
Query . | |||||||||||
Set to
true to allow leading wildcard characters. | |||||||||||
Sets the default date resolution used by RangeQueries for fields for which no
specific date resolutions has been set.
| |||||||||||
Sets the date resolution used by RangeQueries for a specific field.
| |||||||||||
Sets the boolean operator of the QueryParser.
| |||||||||||
Set to
true to enable position increments in result query. | |||||||||||
Set the minimum similarity for fuzzy queries.
| |||||||||||
Set the prefix length for fuzzy queries.
| |||||||||||
Set locale used by date range parsing.
| |||||||||||
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
lower-cased or not.
| |||||||||||
By default QueryParser uses
CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
when creating a PrefixQuery, WildcardQuery or RangeQuery. | |||||||||||
Sets the default slop for phrases.
| |||||||||||
Sets the collator used to determine index term inclusion in ranges
for RangeQuerys.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Factory method for generating query, given a set of clauses.
| |||||||||||
Factory method for generating query, given a set of clauses.
| |||||||||||
Base implementation delegates to
getFieldQuery(String, String) . | |||||||||||
Factory method for generating a query (similar to
getWildcardQuery(String, String) ). | |||||||||||
Factory method for generating a query (similar to
getWildcardQuery(String, String) ). | |||||||||||
Factory method for generating a query.
| |||||||||||
Builds a new BooleanClause instance
| |||||||||||
Builds a new BooleanQuery instance
| |||||||||||
Builds a new FuzzyQuery instance
| |||||||||||
Builds a new MatchAllDocsQuery instance
| |||||||||||
Builds a new MultiPhraseQuery instance
| |||||||||||
Builds a new PhraseQuery instance
| |||||||||||
Builds a new PrefixQuery instance
| |||||||||||
Builds a new TermRangeQuery instance
| |||||||||||
Builds a new TermQuery instance
| |||||||||||
Builds a new WildcardQuery instance
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Constructs a query parser.
matchVersion | Lucene version to match. See above) |
---|---|
f | the default field for query terms. |
a | used to find terms in the query text. |
Disable tracing.
Enable tracing.
Returns a String where those characters that QueryParser
expects to be escaped are escaped by a preceding \
.
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.
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
Get the minimal similarity for fuzzy queries.
Get the prefix length for fuzzy queries.
Gets the default slop for phrases.
Command line tool to test QueryParser, using SimpleAnalyzer
.
Usage:
java org.apache.lucene.queryParser.QueryParser <input>
Exception |
---|
Parses a query string, returning a Query
.
query | the query string to be parsed. |
---|
ParseException | if the parsing fails |
---|
Set to 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.
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
with setDateResolution(String, DateTools.Resolution)
.
dateResolution | the default date resolution to set |
---|
Sets the date resolution used by RangeQueries for a specific field.
fieldName | field for which the date resolution is to be set |
---|---|
dateResolution | date resolution to set |
Sets the boolean operator of the QueryParser.
In default mode (OR_OPERATOR
) terms without any modifiers
are considered optional: for example capital of Hungary
is equal to
capital OR of OR Hungary
.
In AND_OPERATOR
mode terms are considered to be in conjunction: the
above mentioned query is parsed as capital AND of AND Hungary
Set to 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.
Set the minimum similarity for fuzzy queries. Default is 0.5f.
Set the prefix length for fuzzy queries. Default is 0.
fuzzyPrefixLength | The fuzzyPrefixLength to set. |
---|
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
lower-cased or not. Default is true
.
By default QueryParser uses CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
when creating a PrefixQuery, WildcardQuery or RangeQuery. This implementation is generally preferable because it
a) Runs faster b) Does not have the scarcity of terms unduly influence score
c) avoids any "TooManyBooleanClauses" 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.
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.
Sets the collator used to determine index term inclusion in ranges for RangeQuerys.
WARNING: Setting the rangeCollator to a non-null collator using this method will cause every single index Term in the Field referenced by lowerTerm and/or upperTerm to be examined. Depending on the number of index Terms in this Field, the operation could be very slow.rc | the collator to use when constructing RangeQuerys |
---|
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.
clauses | List that contains BooleanClause instances
to join. |
---|---|
disableCoord | true if coord scoring should be disabled. |
Query
object.ParseException | throw in overridden method to disallow |
---|
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.
clauses | List that contains BooleanClause instances
to join. |
---|
Query
object.ParseException | throw in overridden method to disallow |
---|
ParseException | throw in overridden method to disallow |
---|
Base implementation delegates to getFieldQuery(String, String)
.
This method may be overridden, for example, to return
a SpanNearQuery instead of a PhraseQuery.
ParseException | throw in overridden method to disallow |
---|
Factory method for generating a query (similar to
getWildcardQuery(String, 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 query |
Query
built for the termParseException | throw in overridden method to disallow |
---|
Factory method for generating a query (similar to
getWildcardQuery(String, 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 disallow |
---|
ParseException | throw in overridden method to disallow |
---|
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.
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 |
Query
built for the termParseException | throw in overridden method to disallow |
---|
Builds a new BooleanClause instance
q | sub query |
---|---|
occur | how this clause should occur when matching documents |
Builds a new BooleanQuery instance
disableCoord | disable coord |
---|
Builds a new FuzzyQuery instance
term | Term |
---|---|
minimumSimilarity | minimum similarity |
prefixLength | prefix length |
Builds a new MatchAllDocsQuery instance
Builds a new MultiPhraseQuery instance
Builds a new PhraseQuery instance
Builds a new PrefixQuery instance
prefix | Prefix term |
---|
Builds a new TermRangeQuery instance
field | Field |
---|---|
part1 | min |
part2 | max |
inclusive | true if range is inclusive |
Builds a new TermQuery instance
term | term |
---|
Builds a new WildcardQuery instance
t | wildcard term |
---|