fr.gouv.culture.sdx.search.lucene.queryparser
Class DefaultQueryParser

java.lang.Object
  extended by fr.gouv.culture.sdx.search.lucene.queryparser.DefaultQueryParser
All Implemented Interfaces:
DefaultQueryParserConstants, QueryParser

public class DefaultQueryParser
extends java.lang.Object
implements QueryParser, DefaultQueryParserConstants

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:

A clause may be either: Thus, in BNF, the query grammar is:
   Query  ::= ( Clause )*
   Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
 

Examples of appropriately formatted queries can be found in the query syntax documentation.

In RangeQuerys, 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.

Author:
Brian Goetz, Peter Halacsy, Tatu Saloranta

Nested Class Summary
static class DefaultQueryParser.Operator
          The default operator for parsing queries.
 
Field Summary
 Analyzer analyzer
           
static DefaultQueryParser.Operator AND_OPERATOR
          Alternative form of QueryParser.Operator.AND
 Token jj_nt
           
 boolean lookingAhead
           
static DefaultQueryParser.Operator OR_OPERATOR
          Alternative form of QueryParser.Operator.OR
 SearchLocations sLocs
           
 Token token
           
 DefaultQueryParserTokenManager token_source
           
 
Fields inherited from interface fr.gouv.culture.sdx.search.lucene.queryparser.QueryParser
DEFAULT_OPERATOR, DEFAULT_PHRASE_SLOP, OPERATOR_AND, OPERATOR_OR
 
Fields inherited from interface fr.gouv.culture.sdx.search.lucene.queryparser.DefaultQueryParserConstants
_ESCAPED_CHAR, _NUM_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, AND, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NO_ANALYSIS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, RangeEx, RANGEEX_END, RANGEEX_GOOP, RANGEEX_QUOTED, RANGEEX_START, RANGEEX_TO, RangeIn, RANGEIN_END, RANGEIN_GOOP, RANGEIN_QUOTED, RANGEIN_START, RANGEIN_TO, RPAREN, SPECIAL_ANALYZER, STAR, TERM, tokenImage, WILDTERM
 
Constructor Summary
DefaultQueryParser()
          Constructs a query parser.
DefaultQueryParser(CharStream stream)
           
DefaultQueryParser(DefaultQueryParserTokenManager tm)
           
DefaultQueryParser(java.lang.String f, Analyzer a)
          Constructs a query parser.
 
Method Summary
protected  void addClause(java.util.Vector clauses, int conj, int mods, org.apache.lucene.search.Query q)
           
 org.apache.lucene.search.Query Clause(java.lang.String field)
           
 int Conjunction()
           
 void disable_tracing()
           
 void enable_tracing()
           
static java.lang.String escape(java.lang.String s)
          Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.
 ParseException generateParseException()
           
 boolean getAllowLeadingWildcard()
           
 Analyzer getAnalyzer()
           
protected  org.apache.lucene.search.Query getBooleanQuery(java.util.Vector clauses)
          Factory method for generating query, given a set of clauses.
protected  org.apache.lucene.search.Query getBooleanQuery(java.util.Vector clauses, boolean disableCoord)
          Factory method for generating query, given a set of clauses.
 org.apache.lucene.document.DateTools.Resolution getDateResolution(java.lang.String fieldName)
          Returns the date resolution that is used by RangeQueries for the given field.
 DefaultQueryParser.Operator getDefaultOperator()
          Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.
 java.lang.String getField()
           
protected  org.apache.lucene.search.Query getFieldQuery(java.lang.String field, Analyzer analyzer, java.lang.String queryText)
           
protected  org.apache.lucene.search.Query getFieldQuery(java.lang.String field, java.lang.String queryText)
           
protected  org.apache.lucene.search.Query getFieldQuery(java.lang.String field, java.lang.String queryText, int slop)
          Base implementation delegates to getFieldQuery(String,String).
 float getFuzzyMinSim()
          Get the minimal similarity for fuzzy queries.
 int getFuzzyPrefixLength()
          Get the prefix length for fuzzy queries.
protected  org.apache.lucene.search.Query getFuzzyQuery(java.lang.String field, java.lang.String termStr, float minSimilarity)
          Factory method for generating a query (similar to getWildcardQuery(java.lang.String, java.lang.String)).
 java.util.Locale getLocale()
          Returns current locale, allowing access by subclasses.
 boolean getLowercaseExpandedTerms()
           
 Token getNextToken()
           
 int getOperator()
          Get's the operator
 int getPhraseSlop()
          Gets the default slop for phrases.
protected  org.apache.lucene.search.Query getPrefixQuery(java.lang.String field, java.lang.String termStr)
          Factory method for generating a query (similar to getWildcardQuery(java.lang.String, java.lang.String)).
protected  org.apache.lucene.search.Query getRangeQuery(java.lang.String field, java.lang.String part1, java.lang.String part2, boolean inclusive)
           
 Token getToken(int index)
           
protected  org.apache.lucene.search.Query getUnanalyzedQuery(java.lang.String field, java.lang.String queryText)
           
 boolean getUseOldRangeQuery()
           
protected  org.apache.lucene.search.Query getWildcardQuery(java.lang.String field, Analyzer analyzer, java.lang.String termStr)
           
protected  org.apache.lucene.search.Query getWildcardQuery(java.lang.String field, java.lang.String termStr)
          Factory method for generating a query.
static void main(java.lang.String[] args)
          Command line tool to test QueryParser, using SimpleAnalyzer.
 int Modifiers()
           
 org.apache.lucene.search.Query parse(java.lang.String query)
          Parses a query string, returning a Query.
 org.apache.lucene.search.Query Query(java.lang.String field)
           
 void ReInit(CharStream stream)
           
 void ReInit(DefaultQueryParserTokenManager tm)
           
 void setAllowLeadingWildcard(boolean allowLeadingWildcard)
          Set to true to allow * and ?
 void setDateResolution(org.apache.lucene.document.DateTools.Resolution dateResolution)
          Sets the default date resolution used by RangeQueries for fields for which no specific date resolutions has been set.
 void setDateResolution(java.lang.String fieldName, org.apache.lucene.document.DateTools.Resolution dateResolution)
          Sets the date resolution used by RangeQueries for a specific field.
 void setDefaultOperator(DefaultQueryParser.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(java.util.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 setOperator(int op)
          Sets the boolean operator of the QueryParser.
 void setPhraseSlop(int phraseSlop)
          Sets the default slop for phrases.
 void setUp(Analyzer a, int phraseSlop, int operator)
          Sets the fields of the query parser
 void setUp(java.lang.String f, Analyzer a)
          Sets the fields of the query parser
 void setUp(java.lang.String f, Analyzer a, int phraseSlop, int operator)
          Sets the fields of the query parser
 void setUp(java.lang.String f, Analyzer a, SearchLocations sl, int phraseSlop, int operator)
          Sets the fields of the query parser
 void setUseOldRangeQuery(boolean useOldRangeQuery)
          By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery for range queries.
 org.apache.lucene.search.Query Term(java.lang.String field)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AND_OPERATOR

public static final DefaultQueryParser.Operator AND_OPERATOR
Alternative form of QueryParser.Operator.AND


OR_OPERATOR

public static final DefaultQueryParser.Operator OR_OPERATOR
Alternative form of QueryParser.Operator.OR


sLocs

public SearchLocations sLocs

analyzer

public Analyzer analyzer

token_source

public DefaultQueryParserTokenManager token_source

token

public Token token

jj_nt

public Token jj_nt

lookingAhead

public boolean lookingAhead
Constructor Detail

DefaultQueryParser

public DefaultQueryParser()
Constructs a query parser.


DefaultQueryParser

public DefaultQueryParser(java.lang.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.

DefaultQueryParser

public DefaultQueryParser(CharStream stream)

DefaultQueryParser

public DefaultQueryParser(DefaultQueryParserTokenManager tm)
Method Detail

parse

public org.apache.lucene.search.Query parse(java.lang.String query)
                                     throws ParseException
Parses a query string, returning a Query.

Parameters:
query - the query string to be parsed.
Throws:
ParseException - if the parsing fails

setOperator

public void setOperator(int op)
Sets the boolean operator of the QueryParser. In classic mode (DEFAULT_OPERATOR_OR) terms without any modifiers are considered optional: for example capital of Hungary is equal to capital OR of OR Hungary.
In DEFAULT_OPERATOR_AND terms are considered to be in conjuction: the above mentioned query is parsed as capital AND of AND Hungary


getOperator

public int getOperator()
Description copied from interface: QueryParser
Get's the operator


setUp

public void setUp(java.lang.String f,
                  Analyzer a)
Sets the fields of the query parser

Parameters:
f - the default field for query terms.
a - used to find terms in the query text.

setUp

public void setUp(Analyzer a,
                  int phraseSlop,
                  int operator)
Sets the fields of the query parser

Parameters:
a - used to find terms in the query text.
phraseSlop - the slop
operator - the operator

setUp

public void setUp(java.lang.String f,
                  Analyzer a,
                  int phraseSlop,
                  int operator)
Sets the fields of the query parser

Parameters:
f - the default field for query terms.
a - used to find terms in the query text.
phraseSlop - the slop
operator - the operator

setUp

public void setUp(java.lang.String f,
                  Analyzer a,
                  SearchLocations sl,
                  int phraseSlop,
                  int operator)
Sets the fields of the query parser

Parameters:
f - the default field for query terms.
a - used to find terms in the query text.
sl - SearchLocations
phraseSlop - the slop
operator - the operator

getAnalyzer

public Analyzer getAnalyzer()
Returns:
Returns the analyzer.

getField

public java.lang.String getField()
Returns:
Returns the field.

getFuzzyMinSim

public float getFuzzyMinSim()
Get the minimal similarity for fuzzy queries.


setFuzzyMinSim

public void setFuzzyMinSim(float fuzzyMinSim)
Set the minimum similarity for fuzzy queries. Default is 0.5f.


getFuzzyPrefixLength

public int getFuzzyPrefixLength()
Get the prefix length for fuzzy queries.

Returns:
Returns the fuzzyPrefixLength.

setFuzzyPrefixLength

public void setFuzzyPrefixLength(int fuzzyPrefixLength)
Set the prefix length for fuzzy queries. Default is 0.

Parameters:
fuzzyPrefixLength - The fuzzyPrefixLength to set.

setPhraseSlop

public void setPhraseSlop(int phraseSlop)
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.


getPhraseSlop

public int getPhraseSlop()
Gets the default slop for phrases.


setAllowLeadingWildcard

public void setAllowLeadingWildcard(boolean allowLeadingWildcard)
Set to true to allow * and ? as the first character of a PrefixQuery and WildcardQuery. Note that this can produce very slow queries on big indexes. Default: false.


getAllowLeadingWildcard

public boolean getAllowLeadingWildcard()
See Also:
setAllowLeadingWildcard(boolean)

setDefaultOperator

public void setDefaultOperator(DefaultQueryParser.Operator op)
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 conjuction: the above mentioned query is parsed as capital AND of AND Hungary


getDefaultOperator

public DefaultQueryParser.Operator getDefaultOperator()
Gets implicit operator setting, which will be either AND_OPERATOR or OR_OPERATOR.


setLowercaseExpandedTerms

public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default is true.


getLowercaseExpandedTerms

public boolean getLowercaseExpandedTerms()
See Also:
setLowercaseExpandedTerms(boolean)

getUnanalyzedQuery

protected org.apache.lucene.search.Query getUnanalyzedQuery(java.lang.String field,
                                                            java.lang.String queryText)

setUseOldRangeQuery

public void setUseOldRangeQuery(boolean useOldRangeQuery)
By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery for range queries. This implementation is generally preferable because it a) Runs faster b) Does not have the scarcity of range terms unduly influence score c) avoids any "TooManyBooleanClauses" exception. However, if your application really needs to use the old-fashioned RangeQuery and the above points are not required then set this option to true Default is false.


getUseOldRangeQuery

public boolean getUseOldRangeQuery()
See Also:
setUseOldRangeQuery(boolean)

setLocale

public void setLocale(java.util.Locale locale)
Set locale used by date range parsing.


getLocale

public java.util.Locale getLocale()
Returns current locale, allowing access by subclasses.


setDateResolution

public void setDateResolution(org.apache.lucene.document.DateTools.Resolution dateResolution)
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).

Parameters:
dateResolution - the default date resolution to set

setDateResolution

public void setDateResolution(java.lang.String fieldName,
                              org.apache.lucene.document.DateTools.Resolution dateResolution)
Sets the date resolution used by RangeQueries for a specific field.

Parameters:
field - field for which the date resolution is to be set
dateResolution - date resolution to set

getDateResolution

public org.apache.lucene.document.DateTools.Resolution getDateResolution(java.lang.String fieldName)
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.


addClause

protected void addClause(java.util.Vector clauses,
                         int conj,
                         int mods,
                         org.apache.lucene.search.Query q)

getFieldQuery

protected org.apache.lucene.search.Query getFieldQuery(java.lang.String field,
                                                       java.lang.String queryText)
                                                throws ParseException
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

protected org.apache.lucene.search.Query getFieldQuery(java.lang.String field,
                                                       Analyzer analyzer,
                                                       java.lang.String queryText)
                                                throws ParseException
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

protected org.apache.lucene.search.Query getFieldQuery(java.lang.String field,
                                                       java.lang.String queryText,
                                                       int slop)
                                                throws ParseException
Base implementation delegates to getFieldQuery(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

getRangeQuery

protected org.apache.lucene.search.Query getRangeQuery(java.lang.String field,
                                                       java.lang.String part1,
                                                       java.lang.String part2,
                                                       boolean inclusive)
                                                throws ParseException
Throws:
ParseException - throw in overridden method to disallow

getBooleanQuery

protected org.apache.lucene.search.Query getBooleanQuery(java.util.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 contains BooleanClause instances to join.
Returns:
Resulting Query object.
Throws:
ParseException - throw in overridden method to disallow

getBooleanQuery

protected org.apache.lucene.search.Query getBooleanQuery(java.util.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 contains BooleanClause instances to join.
disableCoord - true if coord scoring should be disabled.
Returns:
Resulting Query object.
Throws:
ParseException - throw in overridden method to disallow

getWildcardQuery

protected org.apache.lucene.search.Query getWildcardQuery(java.lang.String field,
                                                          java.lang.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

getWildcardQuery

protected org.apache.lucene.search.Query getWildcardQuery(java.lang.String field,
                                                          Analyzer analyzer,
                                                          java.lang.String termStr)
                                                   throws ParseException
Throws:
ParseException

getPrefixQuery

protected org.apache.lucene.search.Query getPrefixQuery(java.lang.String field,
                                                        java.lang.String termStr)
                                                 throws ParseException
Factory method for generating a query (similar to getWildcardQuery(java.lang.String, java.lang.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

getFuzzyQuery

protected org.apache.lucene.search.Query getFuzzyQuery(java.lang.String field,
                                                       java.lang.String termStr,
                                                       float minSimilarity)
                                                throws ParseException
Factory method for generating a query (similar to getWildcardQuery(java.lang.String, java.lang.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

escape

public static java.lang.String escape(java.lang.String s)
Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.


main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Command line tool to test QueryParser, using SimpleAnalyzer. Usage:
java org.apache.lucene.queryParser.QueryParser <input>

Throws:
java.lang.Exception

Conjunction

public final int Conjunction()
                      throws ParseException
Throws:
ParseException

Modifiers

public final int Modifiers()
                    throws ParseException
Throws:
ParseException

Query

public final org.apache.lucene.search.Query Query(java.lang.String field)
                                           throws ParseException
Throws:
ParseException

Clause

public final org.apache.lucene.search.Query Clause(java.lang.String field)
                                            throws ParseException
Throws:
ParseException

Term

public final org.apache.lucene.search.Query Term(java.lang.String field)
                                          throws ParseException
Throws:
ParseException

ReInit

public void ReInit(CharStream stream)

ReInit

public void ReInit(DefaultQueryParserTokenManager tm)

getNextToken

public final Token getNextToken()

getToken

public final Token getToken(int index)

generateParseException

public ParseException generateParseException()

enable_tracing

public final void enable_tracing()

disable_tracing

public final void disable_tracing()


Copyright © 2000-2010 Ministere de la culture et de la communication / AJLSM. All Rights Reserved.