org.apache.lucene.queryParser
public class QueryParser extends Object implements QueryParserConstants
+
) 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 {@link RangeQuery}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 accpeted input depends on {@link #setLocale(Locale) the locale}. This feature also assumes that your index uses the {@link DateField} class to store dates. If you use a different format (e.g. {@link DateTools}) and you still want QueryParser to turn local dates in range queries into valid queries you need to create your own query parser that inherits QueryParser and overwrites {@link #getRangeQuery(String, String, String, boolean)}.
Note that QueryParser is not thread-safe.
Nested Class Summary | |
---|---|
static class | QueryParser.Operator The default operator for parsing queries.
|
Field Summary | |
---|---|
static QueryParser.Operator | AND_OPERATOR Alternative form of QueryParser.Operator.AND |
static int | DEFAULT_OPERATOR_AND |
static int | DEFAULT_OPERATOR_OR |
Token | jj_nt |
boolean | lookingAhead |
static QueryParser.Operator | OR_OPERATOR Alternative form of QueryParser.Operator.OR |
Token | token |
QueryParserTokenManager | token_source |
Constructor Summary | |
---|---|
QueryParser(String f, Analyzer a) Constructs a query parser. | |
QueryParser(CharStream stream) | |
QueryParser(QueryParserTokenManager tm) |
Method Summary | |
---|---|
protected void | addClause(Vector clauses, int conj, int mods, Query q) |
Query | Clause(String field) |
int | Conjunction() |
void | disable_tracing() |
void | enable_tracing() |
static String | escape(String s)
Returns a String where those characters that QueryParser
expects to be escaped are escaped by a preceding \ . |
ParseException | generateParseException() |
Analyzer | getAnalyzer() |
protected Query | getBooleanQuery(Vector clauses)
Factory method for generating query, given a set of clauses.
|
protected Query | getBooleanQuery(Vector clauses, boolean disableCoord)
Factory method for generating query, given a set of clauses.
|
QueryParser.Operator | getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR
or OR_OPERATOR. |
String | getField() |
protected Query | getFieldQuery(String field, Analyzer analyzer, String queryText)
Note that parameter analyzer is ignored. |
protected Query | getFieldQuery(String field, String queryText) |
protected Query | getFieldQuery(String field, Analyzer analyzer, String queryText, int slop)
Note that parameter analyzer is ignored. |
protected Query | getFieldQuery(String field, String queryText, int slop)
Base implementation delegates to {@link #getFieldQuery(String,String)}.
|
float | getFuzzyMinSim()
Get the minimal similarity for fuzzy queries. |
int | getFuzzyPrefixLength()
Get the prefix length for fuzzy queries. |
protected Query | getFuzzyQuery(String field, String termStr) |
protected Query | getFuzzyQuery(String field, String termStr, float minSimilarity)
Factory method for generating a query (similar to
{@link #getWildcardQuery}). |
Locale | getLocale()
Returns current locale, allowing access by subclasses. |
boolean | getLowercaseExpandedTerms() |
boolean | getLowercaseWildcardTerms() |
Token | getNextToken() |
int | getOperator()
Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND
or DEFAULT_OPERATOR_OR. |
int | getPhraseSlop()
Gets the default slop for phrases. |
protected Query | getPrefixQuery(String field, String termStr)
Factory method for generating a query (similar to
{@link #getWildcardQuery}). |
protected Query | getRangeQuery(String field, Analyzer analyzer, String part1, String part2, boolean inclusive)
Note that parameter analyzer is ignored. |
protected Query | getRangeQuery(String field, String part1, String part2, boolean inclusive) |
Token | getToken(int index) |
protected Query | getWildcardQuery(String field, String termStr)
Factory method for generating a query. |
static void | main(String[] args)
Command line tool to test QueryParser, using {@link org.apache.lucene.analysis.SimpleAnalyzer}.
|
int | Modifiers() |
static Query | parse(String query, String field, Analyzer analyzer) Parses a query string, returning a {@link org.apache.lucene.search.Query}. |
Query | parse(String query) Parses a query string, returning a {@link org.apache.lucene.search.Query}. |
Query | Query(String field) |
void | ReInit(CharStream stream) |
void | ReInit(QueryParserTokenManager tm) |
void | setDefaultOperator(QueryParser.Operator op)
Sets the boolean operator of the QueryParser.
|
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)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
lower-cased or not. |
void | setLowercaseWildcardTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
lower-cased or not. |
void | setOperator(int op)
Sets the boolean operator of the QueryParser.
|
void | setPhraseSlop(int phraseSlop)
Sets the default slop for phrases. |
Query | Term(String field) |
Deprecated: use {@link #AND_OPERATOR} instead
Deprecated: use {@link #OR_OPERATOR} instead
Parameters: f the default field for query terms. a used to find terms in the query text.
\
.Returns: Returns the analyzer.
Parameters: clauses Vector that contains {@link BooleanClause} instances to join.
Returns: Resulting {@link Query} object.
Throws: ParseException throw in overridden method to disallow
Parameters: clauses Vector that contains {@link BooleanClause} instances to join. disableCoord true if coord scoring should be disabled.
Returns: Resulting {@link Query} object.
Throws: ParseException throw in overridden method to disallow
Returns: Returns the field.
Deprecated: use {@link #getFieldQuery(String, String)}
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.Throws: ParseException throw in overridden method to disallow
Throws: ParseException throw in overridden method to disallow
Deprecated: use {@link #getFieldQuery(String, String, int)}
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.Throws: ParseException throw in overridden method to disallow
Throws: ParseException throw in overridden method to disallow
Returns: Returns the fuzzyPrefixLength.
Deprecated: use {@link #getFuzzyQuery(String, String, float)}
Parameters: field Name of the field query will use. termStr Term token to use for building term for the query
Returns: Resulting {@link Query} built for the term
Throws: ParseException throw in overridden method to disallow
See Also: QueryParser
Deprecated: use {@link #getLowercaseExpandedTerms()} instead
Deprecated: use {@link #getDefaultOperator()} instead
Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND or DEFAULT_OPERATOR_OR.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 {@link Query} built for the term
Throws: ParseException throw in overridden method to disallow
Deprecated: use {@link #getRangeQuery(String, String, String, boolean)}
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.Throws: ParseException throw in overridden method to disallow
Throws: ParseException throw in overridden method to disallow
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 {@link Query} built for the term
Throws: ParseException throw in overridden method to disallow
java org.apache.lucene.queryParser.QueryParser <input>
Deprecated: Use an instance of QueryParser and the {@link #parse(String)} method instead.
Parses a query string, returning a {@link org.apache.lucene.search.Query}.Parameters: query the query string to be parsed. field the default field for query terms. analyzer used to find terms in the query text.
Throws: ParseException if the parsing fails
Parameters: query the query string to be parsed.
Throws: ParseException if the parsing fails
OR_OPERATOR
) terms without any modifiers
are considered optional: for example capital of Hungary
is equal to
capital OR of OR Hungary
.AND_OPERATOR
mode terms are considered to be in conjuction: the
above mentioned query is parsed as capital AND of AND Hungary
Parameters: fuzzyPrefixLength The fuzzyPrefixLength to set.
true
.Deprecated: use {@link #setLowercaseExpandedTerms(boolean)} instead
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default istrue
.Deprecated: use {@link #setDefaultOperator(QueryParser.Operator)} instead
Sets the boolean operator of the QueryParser. In default mode (DEFAULT_OPERATOR_OR
) terms without any modifiers
are considered optional: for example capital of Hungary
is equal to
capital OR of OR Hungary
.DEFAULT_OPERATOR_AND
terms are considered to be in conjuction: the
above mentioned query is parsed as capital AND of AND Hungary