?? queryparser.java
字號:
/* Generated By:JavaCC: Do not edit this line. QueryParser.java */package org.apache.lucene.queryParser;import java.io.IOException;import java.io.StringReader;import java.text.DateFormat;import java.text.Collator;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Vector;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.document.DateField;import org.apache.lucene.document.DateTools;import org.apache.lucene.index.Term;import org.apache.lucene.search.BooleanClause;import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.ConstantScoreRangeQuery;import org.apache.lucene.search.FuzzyQuery;import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.search.MultiPhraseQuery;import org.apache.lucene.search.PhraseQuery;import org.apache.lucene.search.PrefixQuery;import org.apache.lucene.search.Query;import org.apache.lucene.search.RangeQuery;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.WildcardQuery;import org.apache.lucene.util.Parameter;/** * This class is generated by JavaCC. The most important method is * {@link #parse(String)}. * * The syntax for query strings is as follows: * A Query is a series of clauses. * A clause may be prefixed by: * <ul> * <li> a plus (<code>+</code>) or a minus (<code>-</code>) sign, indicating * that the clause is required or prohibited respectively; or * <li> a term followed by a colon, indicating the field to be searched. * This enables one to construct queries which search multiple fields. * </ul> * * A clause may be either: * <ul> * <li> a term, indicating all the documents that contain this term; or * <li> a nested query, enclosed in parentheses. Note that this may be used * with a <code>+</code>/<code>-</code> prefix to require any of a set of * terms. * </ul> * * Thus, in BNF, the query grammar is: * <pre> * Query ::= ( Clause )* * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" ) * </pre> * * <p> * Examples of appropriately formatted queries can be found in the <a * href="http://lucene.apache.org/java/docs/queryparsersyntax.html">query syntax * documentation</a>. * </p> * * <p> * In {@link RangeQuery}s, QueryParser tries to detect date values, e.g. * <tt>date:[6/1/2005 TO 6/4/2005]</tt> 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 {@link #setLocale(Locale) the locale}. * By default a date is converted into a search term using the deprecated * {@link DateField} for compatibility reasons. * To use the new {@link DateTools} to convert dates, a * {@link org.apache.lucene.document.DateTools.Resolution} has to be set. * </p> * <p> * The date resolution that shall be used for RangeQueries can be set * using {@link #setDateResolution(DateTools.Resolution)} * or {@link #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. * </p> * <p> * If you use neither {@link DateField} nor {@link DateTools} in your * index, you can create your own * query parser that inherits QueryParser and overwrites * {@link #getRangeQuery(String, String, String, boolean)} to * use a different method for date conversion. * </p> * * <p>Note that QueryParser is <em>not</em> thread-safe.</p> * */public class QueryParser implements QueryParserConstants { private static final int CONJ_NONE = 0; private static final int CONJ_AND = 1; private static final int CONJ_OR = 2; private static final int MOD_NONE = 0; private static final int MOD_NOT = 10; private static final int MOD_REQ = 11; // make it possible to call setDefaultOperator() without accessing // the nested class: /** Alternative form of QueryParser.Operator.AND */ public static final Operator AND_OPERATOR = Operator.AND; /** Alternative form of QueryParser.Operator.OR */ public static final Operator OR_OPERATOR = Operator.OR; /** The actual operator that parser uses to combine query terms */ private Operator operator = OR_OPERATOR; boolean lowercaseExpandedTerms = true; boolean useOldRangeQuery= false; boolean allowLeadingWildcard = false; boolean enablePositionIncrements = false; Analyzer analyzer; String field; int phraseSlop = 0; float fuzzyMinSim = FuzzyQuery.defaultMinSimilarity; int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength; Locale locale = Locale.getDefault(); // the default date resolution DateTools.Resolution dateResolution = null; // maps field names to date resolutions Map fieldToDateResolution = null; // The collator to use when determining range inclusion, // for use when constructing RangeQuerys and ConstantScoreRangeQuerys. Collator rangeCollator = null; /** The default operator for parsing queries. * Use {@link QueryParser#setDefaultOperator} to change it. */ static public final class Operator extends Parameter { private Operator(String name) { super(name); } static public final Operator OR = new Operator("OR"); static public final Operator AND = new Operator("AND"); } /** Constructs a query parser. * @param f the default field for query terms. * @param a used to find terms in the query text. */ public QueryParser(String f, Analyzer a) { this(new FastCharStream(new StringReader(""))); analyzer = a; field = f; } /** Parses a query string, returning a {@link org.apache.lucene.search.Query}. * @param query the query string to be parsed. * @throws ParseException if the parsing fails */ public Query parse(String query) throws ParseException { ReInit(new FastCharStream(new StringReader(query))); try { // TopLevelQuery is a Query followed by the end-of-input (EOF) Query res = TopLevelQuery(field); return res!=null ? res : newBooleanQuery(false); } catch (ParseException tme) { // rethrow to include the original query: throw new ParseException("Cannot parse '" +query+ "': " + tme.getMessage()); } catch (TokenMgrError tme) { throw new ParseException("Cannot parse '" +query+ "': " + tme.getMessage()); } catch (BooleanQuery.TooManyClauses tmc) { throw new ParseException("Cannot parse '" +query+ "': too many boolean clauses"); } } /** * @return Returns the analyzer. */ public Analyzer getAnalyzer() { return analyzer; } /** * @return Returns the field. */ public String getField() { return field; } /** * Get the minimal similarity for fuzzy queries. */ public float getFuzzyMinSim() { return fuzzyMinSim; } /** * Set the minimum similarity for fuzzy queries. * Default is 0.5f. */ public void setFuzzyMinSim(float fuzzyMinSim) { this.fuzzyMinSim = fuzzyMinSim; } /** * Get the prefix length for fuzzy queries. * @return Returns the fuzzyPrefixLength. */ public int getFuzzyPrefixLength() { return fuzzyPrefixLength; } /** * Set the prefix length for fuzzy queries. Default is 0. * @param fuzzyPrefixLength The fuzzyPrefixLength to set. */ public void setFuzzyPrefixLength(int fuzzyPrefixLength) { this.fuzzyPrefixLength = fuzzyPrefixLength; } /** * Sets the default slop for phrases. If zero, then exact phrase matches * are required. Default value is zero. */ public void setPhraseSlop(int phraseSlop) { this.phraseSlop = phraseSlop; } /** * Gets the default slop for phrases. */ public int getPhraseSlop() { return phraseSlop; } /** * Set to <code>true</code> to allow leading wildcard characters. * <p> * When set, <code>*</code> or <code>?</code> are allowed as * the first character of a PrefixQuery and WildcardQuery. * Note that this can produce very slow * queries on big indexes. * <p> * Default: false. */ public void setAllowLeadingWildcard(boolean allowLeadingWildcard) { this.allowLeadingWildcard = allowLeadingWildcard; } /** * @see #setAllowLeadingWildcard(boolean) */ public boolean getAllowLeadingWildcard() { return allowLeadingWildcard; } /** * Set to <code>true</code> to enable position increments in result query. * <p> * 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. * <p> * Default: false. */ public void setEnablePositionIncrements(boolean enable) { this.enablePositionIncrements = enable; } /** * @see #setEnablePositionIncrements(boolean) */ public boolean getEnablePositionIncrements() { return enablePositionIncrements; } /** * Sets the boolean operator of the QueryParser. * In default mode (<code>OR_OPERATOR</code>) terms without any modifiers * are considered optional: for example <code>capital of Hungary</code> is equal to * <code>capital OR of OR Hungary</code>.<br/> * In <code>AND_OPERATOR</code> mode terms are considered to be in conjunction: the * above mentioned query is parsed as <code>capital AND of AND Hungary</code> */ public void setDefaultOperator(Operator op) { this.operator = op; } /** * Gets implicit operator setting, which will be either AND_OPERATOR * or OR_OPERATOR. */ public Operator getDefaultOperator() { return operator; } /** * Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically * lower-cased or not. Default is <code>true</code>. */ public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms) { this.lowercaseExpandedTerms = lowercaseExpandedTerms; } /** * @see #setLowercaseExpandedTerms(boolean) */ public boolean getLowercaseExpandedTerms() { return lowercaseExpandedTerms; } /** * 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 <code>true</code> * Default is <code>false</code>. */ public void setUseOldRangeQuery(boolean useOldRangeQuery) { this.useOldRangeQuery = useOldRangeQuery; } /** * @see #setUseOldRangeQuery(boolean) */ public boolean getUseOldRangeQuery() { return useOldRangeQuery; } /** * Set locale used by date range parsing. */ public void setLocale(Locale locale) { this.locale = locale; } /** * Returns current locale, allowing access by subclasses. */ public Locale getLocale() { return locale; } /** * 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 {@link #setDateResolution(String, DateTools.Resolution)}. * * @param dateResolution the default date resolution to set */ public void setDateResolution(DateTools.Resolution dateResolution) { this.dateResolution = dateResolution; } /** * Sets the date resolution used by RangeQueries for a specific field. * * @param fieldName field for which the date resolution is to be set * @param dateResolution date resolution to set */ public void setDateResolution(String fieldName, DateTools.Resolution dateResolution) { if (fieldName == null) { throw new IllegalArgumentException("Field cannot be null."); } if (fieldToDateResolution == null) { // lazily initialize HashMap fieldToDateResolution = new HashMap(); } fieldToDateResolution.put(fieldName, dateResolution); } /** * 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. * */ public DateTools.Resolution getDateResolution(String fieldName) { if (fieldName == null) { throw new IllegalArgumentException("Field cannot be null."); } if (fieldToDateResolution == null) { // no field specific date resolutions set; return default date resolution instead return this.dateResolution; } DateTools.Resolution resolution = (DateTools.Resolution) fieldToDateResolution.get(fieldName); if (resolution == null) { // no date resolutions set for the given field; return default date resolution instead resolution = this.dateResolution; } return resolution; } /** * Sets the collator used to determine index term inclusion in ranges * specified either for ConstantScoreRangeQuerys or RangeQuerys (if * {@link #setUseOldRangeQuery(boolean)} is called with a <code>true</code> * value.) * <p/> * <strong>WARNING:</strong> 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. * * @param rc the collator to use when constructing RangeQuerys * and ConstantScoreRangeQuerys */ public void setRangeCollator(Collator rc) { rangeCollator = rc; } /** * @return the collator used to determine index term inclusion in ranges * specified either for ConstantScoreRangeQuerys or RangeQuerys (if * {@link #setUseOldRangeQuery(boolean)} is called with a <code>true</code> * value.) */ public Collator getRangeCollator() { return rangeCollator;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -