Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.apache.lucene.queryParser.QueryParser
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
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 the locale
. This
feature also assumes that your index uses the DateField
class to store dates.
If you use a different format (e.g. 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
getRangeQuery(String,String,String,boolean)
.
Note that QueryParser is not thread-safe.
Nested Class Summary | |
static class |
|
Field Summary | |
static QueryParser.Operator |
|
static int |
|
static int |
|
static QueryParser.Operator |
|
Token | |
boolean | |
Token | |
QueryParserTokenManager |
Fields inherited from interface org.apache.lucene.queryParser.QueryParserConstants | |
AND , Boost , CARAT , COLON , DEFAULT , EOF , FUZZY_SLOP , LPAREN , MINUS , NOT , NUMBER , OR , PLUS , PREFIXTERM , QUOTED , RANGEEX_END , RANGEEX_GOOP , RANGEEX_QUOTED , RANGEEX_START , RANGEEX_TO , RANGEIN_END , RANGEIN_GOOP , RANGEIN_QUOTED , RANGEIN_START , RANGEIN_TO , RPAREN , RangeEx , RangeIn , TERM , WILDTERM , _ESCAPED_CHAR , _NUM_CHAR , _TERM_CHAR , _TERM_START_CHAR , _WHITESPACE , tokenImage |
Constructor Summary | |
| |
| |
Method Summary | |
Query |
|
int | |
int | |
Query |
|
void |
|
void | |
Query |
|
protected void | |
void | |
void | |
static String |
|
ParseException | |
Analyzer | |
protected Query |
|
protected Query |
|
QueryParser.Operator |
|
String |
|
protected Query |
|
protected Query |
|
protected Query |
|
protected Query |
|
float |
|
int |
|
protected Query |
|
protected Query |
|
Locale |
|
boolean | |
boolean |
|
Token | |
int |
|
int |
|
protected Query |
|
protected Query |
|
protected Query |
|
Token |
|
protected Query |
|
static void |
|
Query | |
static Query |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
public static final QueryParser.Operator AND_OPERATOR
Alternative form of QueryParser.Operator.AND
public static final int DEFAULT_OPERATOR_AND
Deprecated. use
AND_OPERATOR
instead
- Field Value:
- 1
public static final int DEFAULT_OPERATOR_OR
Deprecated. use
OR_OPERATOR
instead
- Field Value:
- 0
public static final QueryParser.Operator OR_OPERATOR
Alternative form of QueryParser.Operator.OR
public boolean lookingAhead
public QueryParser(String f, Analyzer a)
Constructs a query parser.
- Parameters:
f
- the default field for query terms.a
- used to find terms in the query text.
public final void disable_tracing()
public final void enable_tracing()
public static String escape(String s)
Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding\
.
protected Query getBooleanQuery(Vector clauses) throws ParseException
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.
- Parameters:
clauses
- Vector that containsBooleanClause
instances to join.
- Returns:
- Resulting
Query
object.
- Throws:
ParseException
- throw in overridden method to disallow
protected Query getBooleanQuery(Vector clauses, boolean disableCoord) throws ParseException
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.
- Parameters:
clauses
- Vector that containsBooleanClause
instances to join.disableCoord
- true if coord scoring should be disabled.
- Returns:
- Resulting
Query
object.
- Throws:
ParseException
- throw in overridden method to disallow
public QueryParser.Operator getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
public String getField()
- Returns:
- Returns the field.
protected Query getFieldQuery(String field, String queryText) throws ParseException
- Throws:
ParseException
- throw in overridden method to disallow
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException
Base implementation delegates togetFieldQuery(String,String)
. This method may be overridden, for example, to return a SpanNearQuery instead of a PhraseQuery.
- Throws:
ParseException
- throw in overridden method to disallow
protected Query getFieldQuery(String field, Analyzer analyzer, String queryText) throws ParseException
Deprecated. use
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
protected Query getFieldQuery(String field, Analyzer analyzer, String queryText, int slop) throws ParseException
Deprecated. use
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
public float getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.
public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.
- Returns:
- Returns the fuzzyPrefixLength.
protected Query getFuzzyQuery(String field, String termStr) throws ParseException
Deprecated. use
getFuzzyQuery(String,String,float)
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException
Factory method for generating a query (similar togetWildcardQuery(String,String)
). Called when parser parses an input term token that has the fuzzy suffix (~) appended.
- Parameters:
field
- Name of the field query will use.termStr
- Term token to use for building term for the query
- Returns:
- Resulting
Query
built for the term
- Throws:
ParseException
- throw in overridden method to disallow
public Locale getLocale()
Returns current locale, allowing access by subclasses.
public boolean getLowercaseExpandedTerms()
- See Also:
setLowercaseExpandedTerms(boolean)
public boolean getLowercaseWildcardTerms()
Deprecated. use
getLowercaseExpandedTerms()
instead
public int getOperator()
Deprecated. use
getDefaultOperator()
insteadGets implicit operator setting, which will be either DEFAULT_OPERATOR_AND or DEFAULT_OPERATOR_OR.
public int getPhraseSlop()
Gets the default slop for phrases.
protected Query getPrefixQuery(String field, String termStr) throws ParseException
Factory method for generating a query (similar togetWildcardQuery(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.
- 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
Query
built for the term
- Throws:
ParseException
- throw in overridden method to disallow
protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException
- Throws:
ParseException
- throw in overridden method to disallow
protected Query getRangeQuery(String field, Analyzer analyzer, String part1, String part2, boolean inclusive) throws ParseException
Deprecated. use
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
protected Query getWildcardQuery(String field, String termStr) throws ParseException
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.
- 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
Query
built for the term
- Throws:
ParseException
- throw in overridden method to disallow
public static void main(String[] args) throws Exception
Command line tool to test QueryParser, usingSimpleAnalyzer
. Usage:
java org.apache.lucene.queryParser.QueryParser <input>
public Query parse(String query) throws ParseException
Parses a query string, returning aQuery
.
- Parameters:
query
- the query string to be parsed.
- Throws:
ParseException
- if the parsing fails
public static Query parse(String query, String field, Analyzer analyzer) throws ParseException
Deprecated. Use an instance of QueryParser and the
parse(String)
method instead.Parses a query string, returning aQuery
.
- 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
public void setDefaultOperator(QueryParser.Operator op)
Sets the boolean operator of the QueryParser. In default mode (OR_OPERATOR
) terms without any modifiers are considered optional: for examplecapital of Hungary
is equal tocapital OR of OR Hungary
.
InAND_OPERATOR
mode terms are considered to be in conjuction: the above mentioned query is parsed ascapital AND of AND Hungary
public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 0.5f.
public void setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.
- Parameters:
fuzzyPrefixLength
- The fuzzyPrefixLength to set.
public void setLocale(Locale locale)
Set locale used by date range parsing.
public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default istrue
.
public void setLowercaseWildcardTerms(boolean lowercaseExpandedTerms)
Deprecated. use
setLowercaseExpandedTerms(boolean)
insteadWhether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default istrue
.
public void setOperator(int op)
Deprecated. use
setDefaultOperator(QueryParser.Operator)
insteadSets the boolean operator of the QueryParser. In default mode (DEFAULT_OPERATOR_OR
) terms without any modifiers are considered optional: for examplecapital of Hungary
is equal tocapital OR of OR Hungary
.
InDEFAULT_OPERATOR_AND
terms are considered to be in conjuction: the above mentioned query is parsed ascapital AND of AND Hungary
public void setPhraseSlop(int phraseSlop)
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.