2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 /* Generated By:JavaCC: Do not edit this line. QueryParser.java */
22 using Analyzer = Mono.Lucene.Net.Analysis.Analyzer;
23 using CachingTokenFilter = Mono.Lucene.Net.Analysis.CachingTokenFilter;
24 using TokenStream = Mono.Lucene.Net.Analysis.TokenStream;
25 using PositionIncrementAttribute = Mono.Lucene.Net.Analysis.Tokenattributes.PositionIncrementAttribute;
26 using TermAttribute = Mono.Lucene.Net.Analysis.Tokenattributes.TermAttribute;
27 using DateField = Mono.Lucene.Net.Documents.DateField;
28 using DateTools = Mono.Lucene.Net.Documents.DateTools;
29 using Term = Mono.Lucene.Net.Index.Term;
30 using Parameter = Mono.Lucene.Net.Util.Parameter;
31 using BooleanClause = Mono.Lucene.Net.Search.BooleanClause;
32 using BooleanQuery = Mono.Lucene.Net.Search.BooleanQuery;
33 using FuzzyQuery = Mono.Lucene.Net.Search.FuzzyQuery;
34 using MatchAllDocsQuery = Mono.Lucene.Net.Search.MatchAllDocsQuery;
35 using MultiPhraseQuery = Mono.Lucene.Net.Search.MultiPhraseQuery;
36 using MultiTermQuery = Mono.Lucene.Net.Search.MultiTermQuery;
37 using PhraseQuery = Mono.Lucene.Net.Search.PhraseQuery;
38 using PrefixQuery = Mono.Lucene.Net.Search.PrefixQuery;
39 using Query = Mono.Lucene.Net.Search.Query;
40 using TermQuery = Mono.Lucene.Net.Search.TermQuery;
41 using TermRangeQuery = Mono.Lucene.Net.Search.TermRangeQuery;
42 using WildcardQuery = Mono.Lucene.Net.Search.WildcardQuery;
43 using Version = Mono.Lucene.Net.Util.Version;
45 namespace Mono.Lucene.Net.QueryParsers
48 /// <summary> This class is generated by JavaCC. The most important method is
49 /// {@link #Parse(String)}.
51 /// The syntax for query strings is as follows:
52 /// A Query is a series of clauses.
53 /// A clause may be prefixed by:
55 /// <li> a plus (<code>+</code>) or a minus (<code>-</code>) sign, indicating
56 /// that the clause is required or prohibited respectively; or</li>
57 /// <li> a term followed by a colon, indicating the field to be searched.
58 /// This enables one to construct queries which search multiple fields.</li>
61 /// A clause may be either:
63 /// <li> a term, indicating all the documents that contain this term; or</li>
64 /// <li> a nested query, enclosed in parentheses. Note that this may be used
65 /// with a <code>+</code>/<code>-</code> prefix to require any of a set of
69 /// Thus, in BNF, the query grammar is:
71 /// Query ::= ( Clause )*
72 /// Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
76 /// Examples of appropriately formatted queries can be found in the <a
77 /// href="../../../../../../queryparsersyntax.html">query syntax
78 /// documentation</a>.
82 /// In {@link TermRangeQuery}s, QueryParser tries to detect date values, e.g.
83 /// <tt>date:[6/1/2005 TO 6/4/2005]</tt> produces a range query that searches
84 /// for "date" fields between 2005-06-01 and 2005-06-04. Note that the format
85 /// of the accepted input depends on {@link #SetLocale(Locale) the locale}.
86 /// By default a date is converted into a search term using the deprecated
87 /// {@link DateField} for compatibility reasons.
88 /// To use the new {@link DateTools} to convert dates, a
89 /// {@link Mono.Lucene.Net.Documents.DateTools.Resolution} has to be set.
92 /// The date resolution that shall be used for RangeQueries can be set
93 /// using {@link #SetDateResolution(DateTools.Resolution)}
94 /// or {@link #SetDateResolution(String, DateTools.Resolution)}. The former
95 /// sets the default date resolution for all fields, whereas the latter can
96 /// be used to set field specific date resolutions. Field specific date
97 /// resolutions take, if set, precedence over the default date resolution.
100 /// If you use neither {@link DateField} nor {@link DateTools} in your
101 /// index, you can create your own
102 /// query parser that inherits QueryParser and overwrites
103 /// {@link #GetRangeQuery(String, String, String, boolean)} to
104 /// use a different method for date conversion.
107 /// <p/>Note that QueryParser is <em>not</em> thread-safe.<p/>
109 /// <p/><b>NOTE</b>: there is a new QueryParser in contrib, which matches
110 /// the same syntax as this class, but is more modular,
111 /// enabling substantial customization to how a query is created.
113 /// <p/><b>NOTE</b>: there is a new QueryParser in contrib, which matches
114 /// the same syntax as this class, but is more modular,
115 /// enabling substantial customization to how a query is created.
116 /// <b>NOTE</b>: You must specify the required {@link Version} compatibility when
117 /// creating QueryParser:
119 /// <li>As of 2.9, {@link #SetEnablePositionIncrements} is true by default.</li>
122 public class QueryParser : QueryParserConstants
124 private void InitBlock()
126 multiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
127 fuzzyMinSim = FuzzyQuery.defaultMinSimilarity;
128 fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
129 jj_2_rtns = new JJCalls[1];
130 jj_ls = new LookaheadSuccess();
133 private const int CONJ_NONE = 0;
134 private const int CONJ_AND = 1;
135 private const int CONJ_OR = 2;
137 private const int MOD_NONE = 0;
138 private const int MOD_NOT = 10;
139 private const int MOD_REQ = 11;
141 // make it possible to call setDefaultOperator() without accessing
143 /// <summary>Alternative form of QueryParser.Operator.AND </summary>
144 public static readonly Operator AND_OPERATOR = Operator.AND;
145 /// <summary>Alternative form of QueryParser.Operator.OR </summary>
146 public static readonly Operator OR_OPERATOR = Operator.OR;
148 /// <summary>The actual operator that parser uses to combine query terms </summary>
149 private Operator operator_Renamed = OR_OPERATOR;
151 internal bool lowercaseExpandedTerms = true;
152 internal MultiTermQuery.RewriteMethod multiTermRewriteMethod;
153 internal bool allowLeadingWildcard = false;
154 internal bool enablePositionIncrements = true;
156 internal Analyzer analyzer;
157 internal System.String field;
158 internal int phraseSlop = 0;
159 internal float fuzzyMinSim;
160 internal int fuzzyPrefixLength;
161 internal System.Globalization.CultureInfo locale = System.Threading.Thread.CurrentThread.CurrentCulture;
163 // the default date resolution
164 internal DateTools.Resolution dateResolution = null;
165 // maps field names to date resolutions
166 internal System.Collections.IDictionary fieldToDateResolution = null;
168 // The collator to use when determining range inclusion,
169 // for use when constructing RangeQuerys.
170 internal System.Globalization.CompareInfo rangeCollator = null;
172 /// <summary>The default operator for parsing queries.
173 /// Use {@link QueryParser#setDefaultOperator} to change it.
176 public sealed class Operator:Parameter
178 internal Operator(System.String name):base(name)
181 public static readonly Operator OR = new Operator("OR");
182 public static readonly Operator AND = new Operator("AND");
186 /// <summary>Constructs a query parser.</summary>
187 /// <param name="f"> the default field for query terms.
189 /// <param name="a"> used to find terms in the query text.
191 /// <deprecated> Use {@link #QueryParser(Version, String, Analyzer)} instead
193 [Obsolete("Use QueryParser(Version, String, Analyzer) instead")]
194 public QueryParser(System.String f, Analyzer a):this(Version.LUCENE_24, f, a)
198 /// <summary> Constructs a query parser.
201 /// <param name="matchVersion">Lucene version to match. See <a href="#version">above</a>)
203 /// <param name="f">the default field for query terms.
205 /// <param name="a">used to find terms in the query text.
207 public QueryParser(Version matchVersion, System.String f, Analyzer a):this(new FastCharStream(new System.IO.StringReader("")))
211 if (matchVersion.OnOrAfter(Version.LUCENE_29))
213 enablePositionIncrements = true;
217 enablePositionIncrements = false;
221 /// <summary>Parses a query string, returning a {@link Mono.Lucene.Net.Search.Query}.</summary>
222 /// <param name="query"> the query string to be parsed.
224 /// <throws> ParseException if the parsing fails </throws>
225 public virtual Query Parse(System.String query)
227 ReInit(new FastCharStream(new System.IO.StringReader(query)));
230 // TopLevelQuery is a Query followed by the end-of-input (EOF)
231 Query res = TopLevelQuery(field);
232 return res != null?res:NewBooleanQuery(false);
234 catch (ParseException tme)
236 // rethrow to include the original query:
237 ParseException e = new ParseException("Cannot parse '" + query + "': " + tme.Message, tme);
240 catch (TokenMgrError tme)
242 ParseException e = new ParseException("Cannot parse '" + query + "': " + tme.Message, tme);
245 catch (BooleanQuery.TooManyClauses tmc)
247 ParseException e = new ParseException("Cannot parse '" + query + "': too many boolean clauses", tmc);
252 /// <returns> Returns the analyzer.
254 public virtual Analyzer GetAnalyzer()
259 /// <returns> Returns the field.
261 public virtual System.String GetField()
266 /// <summary> Get the minimal similarity for fuzzy queries.</summary>
267 public virtual float GetFuzzyMinSim()
272 /// <summary> Set the minimum similarity for fuzzy queries.
275 public virtual void SetFuzzyMinSim(float fuzzyMinSim)
277 this.fuzzyMinSim = fuzzyMinSim;
280 /// <summary> Get the prefix length for fuzzy queries. </summary>
281 /// <returns> Returns the fuzzyPrefixLength.
283 public virtual int GetFuzzyPrefixLength()
285 return fuzzyPrefixLength;
288 /// <summary> Set the prefix length for fuzzy queries. Default is 0.</summary>
289 /// <param name="fuzzyPrefixLength">The fuzzyPrefixLength to set.
291 public virtual void SetFuzzyPrefixLength(int fuzzyPrefixLength)
293 this.fuzzyPrefixLength = fuzzyPrefixLength;
296 /// <summary> Sets the default slop for phrases. If zero, then exact phrase matches
297 /// are required. Default value is zero.
299 public virtual void SetPhraseSlop(int phraseSlop)
301 this.phraseSlop = phraseSlop;
304 /// <summary> Gets the default slop for phrases.</summary>
305 public virtual int GetPhraseSlop()
311 /// <summary> Set to <code>true</code> to allow leading wildcard characters.
313 /// When set, <code>*</code> or <code>?</code> are allowed as
314 /// the first character of a PrefixQuery and WildcardQuery.
315 /// Note that this can produce very slow
316 /// queries on big indexes.
320 public virtual void SetAllowLeadingWildcard(bool allowLeadingWildcard)
322 this.allowLeadingWildcard = allowLeadingWildcard;
325 /// <seealso cref="SetAllowLeadingWildcard(boolean)">
327 public virtual bool GetAllowLeadingWildcard()
329 return allowLeadingWildcard;
332 /// <summary> Set to <code>true</code> to enable position increments in result query.
334 /// When set, result phrase and multi-phrase queries will
335 /// be aware of position increments.
336 /// Useful when e.g. a StopFilter increases the position increment of
337 /// the token that follows an omitted token.
341 public virtual void SetEnablePositionIncrements(bool enable)
343 this.enablePositionIncrements = enable;
346 /// <seealso cref="SetEnablePositionIncrements(boolean)">
348 public virtual bool GetEnablePositionIncrements()
350 return enablePositionIncrements;
353 /// <summary> Sets the boolean operator of the QueryParser.
354 /// In default mode (<code>OR_OPERATOR</code>) terms without any modifiers
355 /// are considered optional: for example <code>capital of Hungary</code> is equal to
356 /// <code>capital OR of OR Hungary</code>.<br/>
357 /// In <code>AND_OPERATOR</code> mode terms are considered to be in conjunction: the
358 /// above mentioned query is parsed as <code>capital AND of AND Hungary</code>
360 public virtual void SetDefaultOperator(Operator op)
362 this.operator_Renamed = op;
366 /// <summary> Gets implicit operator setting, which will be either AND_OPERATOR
369 public virtual Operator GetDefaultOperator()
371 return operator_Renamed;
375 /// <summary> Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically
376 /// lower-cased or not. Default is <code>true</code>.
378 public virtual void SetLowercaseExpandedTerms(bool lowercaseExpandedTerms)
380 this.lowercaseExpandedTerms = lowercaseExpandedTerms;
384 /// <seealso cref="SetLowercaseExpandedTerms(boolean)">
386 public virtual bool GetLowercaseExpandedTerms()
388 return lowercaseExpandedTerms;
391 /// <deprecated> Please use {@link #setMultiTermRewriteMethod} instead.
393 [Obsolete("Please use SetMultiTermRewriteMethod instead.")]
394 public virtual void SetUseOldRangeQuery(bool useOldRangeQuery)
396 if (useOldRangeQuery)
398 SetMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
402 SetMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
407 /// <deprecated> Please use {@link #getMultiTermRewriteMethod} instead.
409 [Obsolete("Please use GetMultiTermRewriteMethod} instead.")]
410 public virtual bool GetUseOldRangeQuery()
412 if (GetMultiTermRewriteMethod() == MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE)
422 /// <summary> By default QueryParser uses {@link MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
423 /// when creating a PrefixQuery, WildcardQuery or RangeQuery. This implementation is generally preferable because it
424 /// a) Runs faster b) Does not have the scarcity of terms unduly influence score
425 /// c) avoids any "TooManyBooleanClauses" exception.
426 /// However, if your application really needs to use the
427 /// old-fashioned BooleanQuery expansion rewriting and the above
428 /// points are not relevant then use this to change
429 /// the rewrite method.
431 public virtual void SetMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method)
433 multiTermRewriteMethod = method;
437 /// <seealso cref="setMultiTermRewriteMethod">
439 public virtual MultiTermQuery.RewriteMethod GetMultiTermRewriteMethod()
441 return multiTermRewriteMethod;
444 /// <summary> Set locale used by date range parsing.</summary>
445 public virtual void SetLocale(System.Globalization.CultureInfo locale)
447 this.locale = locale;
450 /// <summary> Returns current locale, allowing access by subclasses.</summary>
451 public virtual System.Globalization.CultureInfo GetLocale()
456 /// <summary> Sets the default date resolution used by RangeQueries for fields for which no
457 /// specific date resolutions has been set. Field specific resolutions can be set
458 /// with {@link #SetDateResolution(String, DateTools.Resolution)}.
461 /// <param name="dateResolution">the default date resolution to set
463 public virtual void SetDateResolution(DateTools.Resolution dateResolution)
465 this.dateResolution = dateResolution;
468 /// <summary> Sets the date resolution used by RangeQueries for a specific field.
471 /// <param name="fieldName">field for which the date resolution is to be set
473 /// <param name="dateResolution">date resolution to set
475 public virtual void SetDateResolution(System.String fieldName, DateTools.Resolution dateResolution)
477 if (fieldName == null)
479 throw new System.ArgumentException("Field cannot be null.");
482 if (fieldToDateResolution == null)
484 // lazily initialize HashMap
485 fieldToDateResolution = new System.Collections.Hashtable();
488 fieldToDateResolution[fieldName] = dateResolution;
491 /// <summary> Returns the date resolution that is used by RangeQueries for the given field.
492 /// Returns null, if no default or field specific date resolution has been set
493 /// for the given field.
496 public virtual DateTools.Resolution GetDateResolution(System.String fieldName)
498 if (fieldName == null)
500 throw new System.ArgumentException("Field cannot be null.");
503 if (fieldToDateResolution == null)
505 // no field specific date resolutions set; return default date resolution instead
506 return this.dateResolution;
509 DateTools.Resolution resolution = (DateTools.Resolution) fieldToDateResolution[fieldName];
510 if (resolution == null)
512 // no date resolutions set for the given field; return default date resolution instead
513 resolution = this.dateResolution;
519 /// <summary> Sets the collator used to determine index term inclusion in ranges
522 /// <strong>WARNING:</strong> Setting the rangeCollator to a non-null
523 /// collator using this method will cause every single index Term in the
524 /// Field referenced by lowerTerm and/or upperTerm to be examined.
525 /// Depending on the number of index Terms in this Field, the operation could
529 /// <param name="rc"> the collator to use when constructing RangeQuerys
531 public virtual void SetRangeCollator(System.Globalization.CompareInfo rc)
536 /// <returns> the collator used to determine index term inclusion in ranges
539 public virtual System.Globalization.CompareInfo GetRangeCollator()
541 return rangeCollator;
544 /// <deprecated> use {@link #AddClause(List, int, int, Query)} instead.
546 [Obsolete("use AddClause(List, int, int, Query) instead.")]
547 protected internal virtual void AddClause(System.Collections.ArrayList clauses, int conj, int mods, Query q)
549 AddClause((System.Collections.IList) clauses, conj, mods, q);
552 protected internal virtual void AddClause(System.Collections.IList clauses, int conj, int mods, Query q)
554 bool required, prohibited;
556 // If this term is introduced by AND, make the preceding term required,
557 // unless it's already prohibited
558 if (clauses.Count > 0 && conj == CONJ_AND)
560 BooleanClause c = (BooleanClause) clauses[clauses.Count - 1];
561 if (!c.IsProhibited())
562 c.SetOccur(BooleanClause.Occur.MUST);
565 if (clauses.Count > 0 && operator_Renamed == AND_OPERATOR && conj == CONJ_OR)
567 // If this term is introduced by OR, make the preceding term optional,
568 // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
569 // notice if the input is a OR b, first term is parsed as required; without
570 // this modification a OR b would parsed as +a OR b
571 BooleanClause c = (BooleanClause) clauses[clauses.Count - 1];
572 if (!c.IsProhibited())
573 c.SetOccur(BooleanClause.Occur.SHOULD);
576 // We might have been passed a null query; the term might have been
577 // filtered away by the analyzer.
581 if (operator_Renamed == OR_OPERATOR)
583 // We set REQUIRED if we're introduced by AND or +; PROHIBITED if
584 // introduced by NOT or -; make sure not to set both.
585 prohibited = (mods == MOD_NOT);
586 required = (mods == MOD_REQ);
587 if (conj == CONJ_AND && !prohibited)
594 // We set PROHIBITED if we're introduced by NOT or -; We set REQUIRED
595 // if not PROHIBITED and not introduced by OR
596 prohibited = (mods == MOD_NOT);
597 required = (!prohibited && conj != CONJ_OR);
599 if (required && !prohibited)
600 clauses.Add(NewBooleanClause(q, BooleanClause.Occur.MUST));
601 else if (!required && !prohibited)
602 clauses.Add(NewBooleanClause(q, BooleanClause.Occur.SHOULD));
603 else if (!required && prohibited)
604 clauses.Add(NewBooleanClause(q, BooleanClause.Occur.MUST_NOT));
606 throw new System.SystemException("Clause cannot be both required and prohibited");
610 /// <exception cref="ParseException">throw in overridden method to disallow
612 public /*protected internal*/ virtual Query GetFieldQuery(System.String field, System.String queryText)
614 // Use the analyzer to get all the tokens, and then build a TermQuery,
615 // PhraseQuery, or nothing based on the term count
620 source = analyzer.ReusableTokenStream(field, new System.IO.StringReader(queryText));
623 catch (System.IO.IOException e)
625 source = analyzer.TokenStream(field, new System.IO.StringReader(queryText));
627 CachingTokenFilter buffer = new CachingTokenFilter(source);
628 TermAttribute termAtt = null;
629 PositionIncrementAttribute posIncrAtt = null;
632 bool success = false;
638 catch (System.IO.IOException e)
640 // success==false if we hit an exception
644 if (buffer.HasAttribute(typeof(TermAttribute)))
646 termAtt = (TermAttribute) buffer.GetAttribute(typeof(TermAttribute));
648 if (buffer.HasAttribute(typeof(PositionIncrementAttribute)))
650 posIncrAtt = (PositionIncrementAttribute) buffer.GetAttribute(typeof(PositionIncrementAttribute));
654 int positionCount = 0;
655 bool severalTokensAtSamePosition = false;
657 bool hasMoreTokens = false;
662 hasMoreTokens = buffer.IncrementToken();
663 while (hasMoreTokens)
666 int positionIncrement = (posIncrAtt != null)?posIncrAtt.GetPositionIncrement():1;
667 if (positionIncrement != 0)
669 positionCount += positionIncrement;
673 severalTokensAtSamePosition = true;
675 hasMoreTokens = buffer.IncrementToken();
678 catch (System.IO.IOException e)
685 // rewind the buffer stream
688 // close original stream - all tokens buffered
691 catch (System.IO.IOException e)
698 else if (numTokens == 1)
700 System.String term = null;
703 bool hasNext = buffer.IncrementToken();
704 System.Diagnostics.Debug.Assert(hasNext == true);
705 term = termAtt.Term();
707 catch (System.IO.IOException e)
709 // safe to ignore, because we know the number of tokens
711 return NewTermQuery(new Term(field, term));
715 if (severalTokensAtSamePosition)
717 if (positionCount == 1)
720 BooleanQuery q = NewBooleanQuery(true);
721 for (int i = 0; i < numTokens; i++)
723 System.String term = null;
726 bool hasNext = buffer.IncrementToken();
727 System.Diagnostics.Debug.Assert(hasNext == true);
728 term = termAtt.Term();
730 catch (System.IO.IOException e)
732 // safe to ignore, because we know the number of tokens
735 Query currentQuery = NewTermQuery(new Term(field, term));
736 q.Add(currentQuery, BooleanClause.Occur.SHOULD);
743 MultiPhraseQuery mpq = NewMultiPhraseQuery();
744 mpq.SetSlop(phraseSlop);
745 System.Collections.ArrayList multiTerms = new System.Collections.ArrayList();
747 for (int i = 0; i < numTokens; i++)
749 System.String term = null;
750 int positionIncrement = 1;
753 bool hasNext = buffer.IncrementToken();
754 System.Diagnostics.Debug.Assert(hasNext == true);
755 term = termAtt.Term();
756 if (posIncrAtt != null)
758 positionIncrement = posIncrAtt.GetPositionIncrement();
761 catch (System.IO.IOException e)
763 // safe to ignore, because we know the number of tokens
766 if (positionIncrement > 0 && multiTerms.Count > 0)
768 if (enablePositionIncrements)
770 mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)), position);
774 mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
778 position += positionIncrement;
779 multiTerms.Add(new Term(field, term));
781 if (enablePositionIncrements)
783 mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)), position);
787 mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
794 PhraseQuery pq = NewPhraseQuery();
795 pq.SetSlop(phraseSlop);
799 for (int i = 0; i < numTokens; i++)
801 System.String term = null;
802 int positionIncrement = 1;
806 bool hasNext = buffer.IncrementToken();
807 System.Diagnostics.Debug.Assert(hasNext == true);
808 term = termAtt.Term();
809 if (posIncrAtt != null)
811 positionIncrement = posIncrAtt.GetPositionIncrement();
814 catch (System.IO.IOException e)
816 // safe to ignore, because we know the number of tokens
819 if (enablePositionIncrements)
821 position += positionIncrement;
822 pq.Add(new Term(field, term), position);
826 pq.Add(new Term(field, term));
836 /// <summary> Base implementation delegates to {@link #GetFieldQuery(String,String)}.
837 /// This method may be overridden, for example, to return
838 /// a SpanNearQuery instead of a PhraseQuery.
841 /// <exception cref="ParseException">throw in overridden method to disallow
843 protected internal virtual Query GetFieldQuery(System.String field, System.String queryText, int slop)
845 Query query = GetFieldQuery(field, queryText);
847 if (query is PhraseQuery)
849 ((PhraseQuery) query).SetSlop(slop);
851 if (query is MultiPhraseQuery)
853 ((MultiPhraseQuery) query).SetSlop(slop);
860 /// <exception cref="ParseException">throw in overridden method to disallow
862 protected internal virtual Query GetRangeQuery(System.String field, System.String part1, System.String part2, bool inclusive)
864 if (lowercaseExpandedTerms)
866 part1 = part1.ToLower();
867 part2 = part2.ToLower();
876 d1 = System.DateTime.Parse(part1, locale);
878 catch (System.Exception)
880 d1 = System.DateTime.Parse(part1);
884 d2 = System.DateTime.Parse(part2, locale);
886 catch (System.Exception)
888 d2 = System.DateTime.Parse(part2);
893 // The user can only specify the date, not the time, so make sure
894 // the time is set to the latest possible time of that date to really
895 // include all documents:
896 System.Globalization.Calendar cal = new System.Globalization.GregorianCalendar();
897 System.DateTime tempDate = d2;
898 d2 = d2.AddHours(23 - tempDate.Hour);
899 d2 = d2.AddMinutes(59 - tempDate.Minute);
900 d2 = d2.AddSeconds(59 - tempDate.Second);
901 d2 = d2.AddMilliseconds(999 - tempDate.Millisecond);
903 DateTools.Resolution resolution = GetDateResolution(field);
904 if (resolution == null)
906 // no default or field specific date resolution has been set,
907 // use deprecated DateField to maintain compatibility with
908 // pre-1.9 Lucene versions.
909 part1 = DateField.DateToString(d1);
910 part2 = DateField.DateToString(d2);
914 part1 = DateTools.DateToString(d1, resolution);
915 part2 = DateTools.DateToString(d2, resolution);
918 catch (System.Exception)
922 return NewRangeQuery(field, part1, part2, inclusive);
925 /// <summary> Builds a new BooleanQuery instance</summary>
926 /// <param name="disableCoord">disable coord
928 /// <returns> new BooleanQuery instance
930 protected internal virtual BooleanQuery NewBooleanQuery(bool disableCoord)
932 return new BooleanQuery(disableCoord);
935 /// <summary> Builds a new BooleanClause instance</summary>
936 /// <param name="q">sub query
938 /// <param name="occur">how this clause should occur when matching documents
940 /// <returns> new BooleanClause instance
942 protected internal virtual BooleanClause NewBooleanClause(Query q, BooleanClause.Occur occur)
944 return new BooleanClause(q, occur);
947 /// <summary> Builds a new TermQuery instance</summary>
948 /// <param name="term">term
950 /// <returns> new TermQuery instance
952 protected internal virtual Query NewTermQuery(Term term)
954 return new TermQuery(term);
957 /// <summary> Builds a new PhraseQuery instance</summary>
958 /// <returns> new PhraseQuery instance
960 protected internal virtual PhraseQuery NewPhraseQuery()
962 return new PhraseQuery();
965 /// <summary> Builds a new MultiPhraseQuery instance</summary>
966 /// <returns> new MultiPhraseQuery instance
968 protected internal virtual MultiPhraseQuery NewMultiPhraseQuery()
970 return new MultiPhraseQuery();
973 /// <summary> Builds a new PrefixQuery instance</summary>
974 /// <param name="prefix">Prefix term
976 /// <returns> new PrefixQuery instance
978 protected internal virtual Query NewPrefixQuery(Term prefix)
980 PrefixQuery query = new PrefixQuery(prefix);
981 query.SetRewriteMethod(multiTermRewriteMethod);
985 /// <summary> Builds a new FuzzyQuery instance</summary>
986 /// <param name="term">Term
988 /// <param name="minimumSimilarity">minimum similarity
990 /// <param name="prefixLength">prefix length
992 /// <returns> new FuzzyQuery Instance
994 protected internal virtual Query NewFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)
996 // FuzzyQuery doesn't yet allow constant score rewrite
997 return new FuzzyQuery(term, minimumSimilarity, prefixLength);
1000 /// <summary> Builds a new TermRangeQuery instance</summary>
1001 /// <param name="field">Field
1003 /// <param name="part1">min
1005 /// <param name="part2">max
1007 /// <param name="inclusive">true if range is inclusive
1009 /// <returns> new TermRangeQuery instance
1011 protected internal virtual Query NewRangeQuery(System.String field, System.String part1, System.String part2, bool inclusive)
1013 TermRangeQuery query = new TermRangeQuery(field, part1, part2, inclusive, inclusive, rangeCollator);
1014 query.SetRewriteMethod(multiTermRewriteMethod);
1018 /// <summary> Builds a new MatchAllDocsQuery instance</summary>
1019 /// <returns> new MatchAllDocsQuery instance
1021 protected internal virtual Query NewMatchAllDocsQuery()
1023 return new MatchAllDocsQuery();
1026 /// <summary> Builds a new WildcardQuery instance</summary>
1027 /// <param name="t">wildcard term
1029 /// <returns> new WildcardQuery instance
1031 protected internal virtual Query NewWildcardQuery(Term t)
1033 WildcardQuery query = new WildcardQuery(t);
1034 query.SetRewriteMethod(multiTermRewriteMethod);
1038 /// <summary> Factory method for generating query, given a set of clauses.
1039 /// By default creates a boolean query composed of clauses passed in.
1041 /// Can be overridden by extending classes, to modify query being
1045 /// <param name="clauses">List that contains {@link BooleanClause} instances
1049 /// <returns> Resulting {@link Query} object.
1051 /// <exception cref="ParseException">throw in overridden method to disallow
1053 /// <deprecated> use {@link #GetBooleanQuery(List)} instead
1055 [Obsolete("use GetBooleanQuery(List) instead")]
1056 protected internal virtual Query GetBooleanQuery(System.Collections.ArrayList clauses)
1058 return GetBooleanQuery((System.Collections.IList) clauses, false);
1061 /// <summary> Factory method for generating query, given a set of clauses.
1062 /// By default creates a boolean query composed of clauses passed in.
1064 /// Can be overridden by extending classes, to modify query being
1068 /// <param name="clauses">List that contains {@link BooleanClause} instances
1072 /// <returns> Resulting {@link Query} object.
1074 /// <exception cref="ParseException">throw in overridden method to disallow
1076 protected internal virtual Query GetBooleanQuery(System.Collections.IList clauses)
1078 return GetBooleanQuery(clauses, false);
1081 /// <summary> Factory method for generating query, given a set of clauses.
1082 /// By default creates a boolean query composed of clauses passed in.
1084 /// Can be overridden by extending classes, to modify query being
1088 /// <param name="clauses">List that contains {@link BooleanClause} instances
1091 /// <param name="disableCoord">true if coord scoring should be disabled.
1094 /// <returns> Resulting {@link Query} object.
1096 /// <exception cref="ParseException">throw in overridden method to disallow
1098 /// <deprecated> use {@link #GetBooleanQuery(List, boolean)} instead
1100 [Obsolete("use GetBooleanQuery(List, bool) instead")]
1101 protected internal virtual Query GetBooleanQuery(System.Collections.ArrayList clauses, bool disableCoord)
1103 return GetBooleanQuery((System.Collections.IList) clauses, disableCoord);
1106 /// <summary> Factory method for generating query, given a set of clauses.
1107 /// By default creates a boolean query composed of clauses passed in.
1109 /// Can be overridden by extending classes, to modify query being
1113 /// <param name="clauses">List that contains {@link BooleanClause} instances
1116 /// <param name="disableCoord">true if coord scoring should be disabled.
1119 /// <returns> Resulting {@link Query} object.
1121 /// <exception cref="ParseException">throw in overridden method to disallow
1123 protected internal virtual Query GetBooleanQuery(System.Collections.IList clauses, bool disableCoord)
1125 if (clauses.Count == 0)
1127 return null; // all clause words were filtered away by the analyzer.
1129 BooleanQuery query = NewBooleanQuery(disableCoord);
1130 for (int i = 0; i < clauses.Count; i++)
1132 query.Add((BooleanClause) clauses[i]);
1137 /// <summary> Factory method for generating a query. Called when parser
1138 /// parses an input term token that contains one or more wildcard
1139 /// characters (? and *), but is not a prefix term token (one
1140 /// that has just a single * character at the end)
1142 /// Depending on settings, prefix term may be lower-cased
1143 /// automatically. It will not go through the default Analyzer,
1144 /// however, since normal Analyzers are unlikely to work properly
1145 /// with wildcard templates.
1147 /// Can be overridden by extending classes, to provide custom handling for
1148 /// wildcard queries, which may be necessary due to missing analyzer calls.
1151 /// <param name="field">Name of the field query will use.
1153 /// <param name="termStr">Term token that contains one or more wild card
1154 /// characters (? or *), but is not simple prefix term
1157 /// <returns> Resulting {@link Query} built for the term
1159 /// <exception cref="ParseException">throw in overridden method to disallow
1161 public /*protected internal*/ virtual Query GetWildcardQuery(System.String field, System.String termStr)
1163 if ("*".Equals(field))
1165 if ("*".Equals(termStr))
1166 return NewMatchAllDocsQuery();
1168 if (!allowLeadingWildcard && (termStr.StartsWith("*") || termStr.StartsWith("?")))
1169 throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery");
1170 if (lowercaseExpandedTerms)
1172 termStr = termStr.ToLower();
1174 Term t = new Term(field, termStr);
1175 return NewWildcardQuery(t);
1178 /// <summary> Factory method for generating a query (similar to
1179 /// {@link #getWildcardQuery}). Called when parser parses an input term
1180 /// token that uses prefix notation; that is, contains a single '*' wildcard
1181 /// character as its last character. Since this is a special case
1182 /// of generic wildcard term, and such a query can be optimized easily,
1183 /// this usually results in a different query object.
1185 /// Depending on settings, a prefix term may be lower-cased
1186 /// automatically. It will not go through the default Analyzer,
1187 /// however, since normal Analyzers are unlikely to work properly
1188 /// with wildcard templates.
1190 /// Can be overridden by extending classes, to provide custom handling for
1191 /// wild card queries, which may be necessary due to missing analyzer calls.
1194 /// <param name="field">Name of the field query will use.
1196 /// <param name="termStr">Term token to use for building term for the query
1197 /// (<b>without</b> trailing '*' character!)
1200 /// <returns> Resulting {@link Query} built for the term
1202 /// <exception cref="ParseException">throw in overridden method to disallow
1204 public /*protected internal*/ virtual Query GetPrefixQuery(System.String field, System.String termStr)
1206 if (!allowLeadingWildcard && termStr.StartsWith("*"))
1207 throw new ParseException("'*' not allowed as first character in PrefixQuery");
1208 if (lowercaseExpandedTerms)
1210 termStr = termStr.ToLower();
1212 Term t = new Term(field, termStr);
1213 return NewPrefixQuery(t);
1216 /// <summary> Factory method for generating a query (similar to
1217 /// {@link #getWildcardQuery}). Called when parser parses
1218 /// an input term token that has the fuzzy suffix (~) appended.
1221 /// <param name="field">Name of the field query will use.
1223 /// <param name="termStr">Term token to use for building term for the query
1226 /// <returns> Resulting {@link Query} built for the term
1228 /// <exception cref="ParseException">throw in overridden method to disallow
1230 public /*protected internal*/ virtual Query GetFuzzyQuery(System.String field, System.String termStr, float minSimilarity)
1232 if (lowercaseExpandedTerms)
1234 termStr = termStr.ToLower();
1236 Term t = new Term(field, termStr);
1237 return NewFuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
1240 /// <summary> Returns a String where the escape char has been
1241 /// removed, or kept only once if there was a double escape.
1243 /// Supports escaped unicode characters, e. g. translates
1244 /// <code>\\u0041</code> to <code>A</code>.
1247 private System.String DiscardEscapeChar(System.String input)
1249 // Create char array to hold unescaped char sequence
1250 char[] output = new char[input.Length];
1252 // The length of the output can be less than the input
1253 // due to discarded escape chars. This variable holds
1254 // the actual length of the output
1257 // We remember whether the last processed character was
1258 // an escape character
1259 bool lastCharWasEscapeChar = false;
1261 // The multiplier the current unicode digit must be multiplied with.
1262 // E. g. the first digit must be multiplied with 16^3, the second with 16^2...
1263 int codePointMultiplier = 0;
1265 // Used to calculate the codepoint of the escaped unicode character
1268 for (int i = 0; i < input.Length; i++)
1270 char curChar = input[i];
1271 if (codePointMultiplier > 0)
1273 codePoint += HexToInt(curChar) * codePointMultiplier;
1274 codePointMultiplier = SupportClass.Number.URShift(codePointMultiplier, 4);
1275 if (codePointMultiplier == 0)
1277 output[length++] = (char) codePoint;
1281 else if (lastCharWasEscapeChar)
1285 // found an escaped unicode character
1286 codePointMultiplier = 16 * 16 * 16;
1290 // this character was escaped
1291 output[length] = curChar;
1294 lastCharWasEscapeChar = false;
1298 if (curChar == '\\')
1300 lastCharWasEscapeChar = true;
1304 output[length] = curChar;
1310 if (codePointMultiplier > 0)
1312 throw new ParseException("Truncated unicode escape sequence.");
1315 if (lastCharWasEscapeChar)
1317 throw new ParseException("Term can not end with escape character.");
1320 return new System.String(output, 0, length);
1323 /// <summary>Returns the numeric value of the hexadecimal character </summary>
1324 private static int HexToInt(char c)
1326 if ('0' <= c && c <= '9')
1330 else if ('a' <= c && c <= 'f')
1332 return c - 'a' + 10;
1334 else if ('A' <= c && c <= 'F')
1336 return c - 'A' + 10;
1340 throw new ParseException("None-hex character in unicode escape sequence: " + c);
1344 /// <summary> Returns a String where those characters that QueryParser
1345 /// expects to be escaped are escaped by a preceding <code>\</code>.
1347 public static System.String Escape(System.String s)
1349 System.Text.StringBuilder sb = new System.Text.StringBuilder();
1350 for (int i = 0; i < s.Length; i++)
1353 // These characters are part of the query syntax and must be escaped
1354 if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' || c == '|' || c == '&')
1360 return sb.ToString();
1363 /// <summary> Command line tool to test QueryParser, using {@link Mono.Lucene.Net.Analysis.SimpleAnalyzer}.
1365 /// <code>java Mono.Lucene.Net.QueryParsers.QueryParser <input></code>
1368 public static void Main(System.String[] args)
1370 if (args.Length == 0)
1372 System.Console.Out.WriteLine("Usage: java Mono.Lucene.Net.QueryParsers.QueryParser <input>");
1373 System.Environment.Exit(0);
1375 QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "field", new Mono.Lucene.Net.Analysis.SimpleAnalyzer());
1376 Query q = qp.Parse(args[0]);
1377 System.Console.Out.WriteLine(q.ToString("field"));
1380 // * Query ::= ( Clause )*
1381 // * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
1382 public int Conjunction()
1384 int ret = CONJ_NONE;
1385 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1388 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.AND:
1389 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.OR:
1390 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1393 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.AND:
1394 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.AND);
1398 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.OR:
1399 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.OR);
1405 Jj_consume_token(- 1);
1406 throw new ParseException();
1421 throw new System.ApplicationException("Missing return statement in function");
1424 public int Modifiers()
1427 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1430 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NOT:
1431 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PLUS:
1432 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.MINUS:
1433 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1436 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PLUS:
1437 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.PLUS);
1441 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.MINUS:
1442 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.MINUS);
1446 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NOT:
1447 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NOT);
1453 Jj_consume_token(- 1);
1454 throw new ParseException();
1469 throw new System.ApplicationException("Missing return statement in function");
1472 // This makes sure that there is no garbage after the query string
1473 public Query TopLevelQuery(System.String field)
1477 Jj_consume_token(0);
1482 throw new System.ApplicationException("Missing return statement in function");
1485 public Query Query(System.String field)
1487 System.Collections.IList clauses = new System.Collections.ArrayList();
1488 Query q, firstQuery = null;
1492 AddClause(clauses, CONJ_NONE, mods, q);
1493 if (mods == MOD_NONE)
1497 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1500 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.AND:
1501 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.OR:
1502 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NOT:
1503 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PLUS:
1504 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.MINUS:
1505 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.LPAREN:
1506 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR:
1507 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.QUOTED:
1508 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1509 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
1510 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
1511 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START:
1512 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START:
1513 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
1519 goto label_1_brk; // {{Aroush-2.9}} this goto maybe misplaced
1522 conj = Conjunction();
1525 AddClause(clauses, conj, mods, q);
1528 label_1_brk: ; // {{Aroush-2.9}} this lable maybe misplaced
1530 if (clauses.Count == 1 && firstQuery != null)
1539 return GetBooleanQuery(clauses);
1542 throw new System.ApplicationException("Missing return statement in function");
1545 public Query Clause(System.String field)
1548 Token fieldToken = null, boost = null;
1551 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1554 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1555 fieldToken = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM);
1556 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.COLON);
1557 field = DiscardEscapeChar(fieldToken.image);
1560 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR:
1561 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR);
1562 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.COLON);
1568 Jj_consume_token(- 1);
1569 throw new ParseException();
1577 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1580 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR:
1581 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.QUOTED:
1582 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1583 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
1584 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
1585 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START:
1586 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START:
1587 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
1591 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.LPAREN:
1592 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.LPAREN);
1594 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RPAREN);
1595 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1598 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1599 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1600 boost = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1613 Jj_consume_token(- 1);
1614 throw new ParseException();
1619 float f = (float) 1.0;
1622 f = (float) SupportClass.Single.Parse(boost.image);
1625 catch (System.Exception ignored)
1633 throw new System.ApplicationException("Missing return statement in function");
1636 public Query Term(System.String field)
1638 Token term, boost = null, fuzzySlop = null, goop1, goop2;
1639 bool prefix = false;
1640 bool wildcard = false;
1643 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1646 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR:
1647 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1648 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
1649 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
1650 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
1651 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1654 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM:
1655 term = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM);
1658 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR:
1659 term = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR);
1663 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM:
1664 term = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.PREFIXTERM);
1668 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM:
1669 term = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.WILDTERM);
1673 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER:
1674 term = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1679 Jj_consume_token(- 1);
1680 throw new ParseException();
1683 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1686 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP:
1687 fuzzySlop = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP);
1697 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1700 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1701 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1702 boost = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1703 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1706 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP:
1707 fuzzySlop = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP);
1712 jj_la1[10] = jj_gen;
1720 jj_la1[11] = jj_gen;
1725 System.String termImage = DiscardEscapeChar(term.image);
1728 q = GetWildcardQuery(field, termImage);
1732 q = GetPrefixQuery(field, DiscardEscapeChar(term.image.Substring(0, (term.image.Length - 1) - (0))));
1736 float fms = fuzzyMinSim;
1739 fms = (float) SupportClass.Single.Parse(fuzzySlop.image.Substring(1));
1741 catch (System.Exception ignored)
1744 if (fms < 0.0f || fms > 1.0f)
1748 throw new ParseException("Minimum similarity for a FuzzyQuery has to be between 0.0f and 1.0f !");
1751 q = GetFuzzyQuery(field, termImage, fms);
1755 q = GetFieldQuery(field, termImage);
1759 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START:
1760 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_START);
1761 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1764 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP:
1765 goop1 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP);
1768 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED:
1769 goop1 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED);
1773 jj_la1[12] = jj_gen;
1774 Jj_consume_token(- 1);
1775 throw new ParseException();
1778 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1781 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_TO:
1782 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_TO);
1786 jj_la1[13] = jj_gen;
1791 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1794 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP:
1795 goop2 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_GOOP);
1798 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED:
1799 goop2 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED);
1803 jj_la1[14] = jj_gen;
1804 Jj_consume_token(- 1);
1805 throw new ParseException();
1808 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_END);
1809 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1812 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1813 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1814 boost = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1818 jj_la1[15] = jj_gen;
1823 if (goop1.kind == Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED)
1825 goop1.image = goop1.image.Substring(1, (goop1.image.Length - 1) - (1));
1827 if (goop2.kind == Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEIN_QUOTED)
1829 goop2.image = goop2.image.Substring(1, (goop2.image.Length - 1) - (1));
1831 q = GetRangeQuery(field, DiscardEscapeChar(goop1.image), DiscardEscapeChar(goop2.image), true);
1834 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START:
1835 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_START);
1836 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1839 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP:
1840 goop1 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP);
1843 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED:
1844 goop1 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED);
1848 jj_la1[16] = jj_gen;
1849 Jj_consume_token(- 1);
1850 throw new ParseException();
1853 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1856 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_TO:
1857 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_TO);
1861 jj_la1[17] = jj_gen;
1866 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1869 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP:
1870 goop2 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_GOOP);
1873 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED:
1874 goop2 = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED);
1878 jj_la1[18] = jj_gen;
1879 Jj_consume_token(- 1);
1880 throw new ParseException();
1883 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_END);
1884 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1887 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1888 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1889 boost = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1893 jj_la1[19] = jj_gen;
1898 if (goop1.kind == Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED)
1900 goop1.image = goop1.image.Substring(1, (goop1.image.Length - 1) - (1));
1902 if (goop2.kind == Mono.Lucene.Net.QueryParsers.QueryParserConstants.RANGEEX_QUOTED)
1904 goop2.image = goop2.image.Substring(1, (goop2.image.Length - 1) - (1));
1907 q = GetRangeQuery(field, DiscardEscapeChar(goop1.image), DiscardEscapeChar(goop2.image), false);
1910 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.QUOTED:
1911 term = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.QUOTED);
1912 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1915 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP:
1916 fuzzySlop = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.FUZZY_SLOP);
1920 jj_la1[20] = jj_gen;
1925 switch ((jj_ntk == - 1)?Jj_ntk():jj_ntk)
1928 case Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT:
1929 Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.CARAT);
1930 boost = Jj_consume_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.NUMBER);
1934 jj_la1[21] = jj_gen;
1941 if (fuzzySlop != null)
1945 s = (int) SupportClass.Single.Parse(fuzzySlop.image.Substring(1));
1947 catch (System.Exception ignored)
1951 q = GetFieldQuery(field, DiscardEscapeChar(term.image.Substring(1, (term.image.Length - 1) - (1))), s);
1955 jj_la1[22] = jj_gen;
1956 Jj_consume_token(- 1);
1957 throw new ParseException();
1962 float f = (float) 1.0;
1965 f = (float) SupportClass.Single.Parse(boost.image);
1967 catch (System.Exception ignored)
1969 /* Should this be handled somehow? (defaults to "no boost", if
1970 * boost number is invalid)
1974 // avoid boosting null queries, such as those caused by stop words
1984 throw new System.ApplicationException("Missing return statement in function");
1987 private bool Jj_2_1(int xla)
1989 jj_la = xla; jj_lastpos = jj_scanpos = token;
1994 catch (LookaheadSuccess ls)
2004 private bool Jj_3R_2()
2006 if (Jj_scan_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.TERM))
2008 if (Jj_scan_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.COLON))
2013 private bool Jj_3_1()
2026 private bool Jj_3R_3()
2028 if (Jj_scan_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.STAR))
2030 if (Jj_scan_token(Mono.Lucene.Net.QueryParsers.QueryParserConstants.COLON))
2035 /// <summary>Generated Token Manager. </summary>
2036 public QueryParserTokenManager token_source;
2037 /// <summary>Current token. </summary>
2039 /// <summary>Next token. </summary>
2040 private Token jj_nt;
2042 private Token jj_scanpos, jj_lastpos;
2045 private int[] jj_la1 = new int[23];
2046 private static int[] jj_la1_0;
2047 private static int[] jj_la1_1;
2048 private static void Jj_la1_init_0()
2050 jj_la1_0 = new int[]{0x300, 0x300, 0x1c00, 0x1c00, 0x3ed3f00, 0x90000, 0x20000, 0x3ed2000, 0x2690000, 0x100000, 0x100000, 0x20000, 0x30000000, 0x4000000, 0x30000000, 0x20000, 0x0, 0x40000000, 0x0, 0x20000, 0x100000, 0x20000, 0x3ed0000};
2052 private static void Jj_la1_init_1()
2054 jj_la1_1 = new int[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0};
2056 private JJCalls[] jj_2_rtns;
2057 private bool jj_rescan = false;
2058 private int jj_gc = 0;
2060 /// <summary>Constructor with user supplied CharStream. </summary>
2061 protected internal QueryParser(CharStream stream)
2064 token_source = new QueryParserTokenManager(stream);
2065 token = new Token();
2068 for (int i = 0; i < 23; i++)
2070 for (int i = 0; i < jj_2_rtns.Length; i++)
2071 jj_2_rtns[i] = new JJCalls();
2074 /// <summary>Reinitialise. </summary>
2075 public virtual void ReInit(CharStream stream)
2077 token_source.ReInit(stream);
2078 token = new Token();
2081 for (int i = 0; i < 23; i++)
2083 for (int i = 0; i < jj_2_rtns.Length; i++)
2084 jj_2_rtns[i] = new JJCalls();
2087 /// <summary>Constructor with generated Token Manager. </summary>
2088 protected internal QueryParser(QueryParserTokenManager tm)
2092 token = new Token();
2095 for (int i = 0; i < 23; i++)
2097 for (int i = 0; i < jj_2_rtns.Length; i++)
2098 jj_2_rtns[i] = new JJCalls();
2101 /// <summary>Reinitialise. </summary>
2102 public virtual void ReInit(QueryParserTokenManager tm)
2105 token = new Token();
2108 for (int i = 0; i < 23; i++)
2110 for (int i = 0; i < jj_2_rtns.Length; i++)
2111 jj_2_rtns[i] = new JJCalls();
2114 private Token Jj_consume_token(int kind)
2117 if ((oldToken = token).next != null)
2120 token = token.next = token_source.GetNextToken();
2122 if (token.kind == kind)
2128 for (int i = 0; i < jj_2_rtns.Length; i++)
2130 JJCalls c = jj_2_rtns[i];
2143 throw GenerateParseException();
2147 private sealed class LookaheadSuccess:System.ApplicationException
2150 private LookaheadSuccess jj_ls;
2151 private bool Jj_scan_token(int kind)
2153 if (jj_scanpos == jj_lastpos)
2156 if (jj_scanpos.next == null)
2158 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.GetNextToken();
2162 jj_lastpos = jj_scanpos = jj_scanpos.next;
2167 jj_scanpos = jj_scanpos.next;
2171 int i = 0; Token tok = token;
2172 while (tok != null && tok != jj_scanpos)
2174 i++; tok = tok.next;
2177 Jj_add_error_token(kind, i);
2179 if (jj_scanpos.kind != kind)
2181 if (jj_la == 0 && jj_scanpos == jj_lastpos)
2187 /// <summary>Get the next Token. </summary>
2188 public Token GetNextToken()
2190 if (token.next != null)
2193 token = token.next = token_source.GetNextToken();
2199 /// <summary>Get the specific Token. </summary>
2200 public Token GetToken(int index)
2203 for (int i = 0; i < index; i++)
2208 t = t.next = token_source.GetNextToken();
2213 private int Jj_ntk()
2215 if ((jj_nt = token.next) == null)
2216 return (jj_ntk = (token.next = token_source.GetNextToken()).kind);
2218 return (jj_ntk = jj_nt.kind);
2221 private System.Collections.IList jj_expentries = new System.Collections.ArrayList();
2222 private int[] jj_expentry;
2223 private int jj_kind = - 1;
2224 private int[] jj_lasttokens = new int[100];
2225 private int jj_endpos;
2227 private void Jj_add_error_token(int kind, int pos)
2231 if (pos == jj_endpos + 1)
2233 jj_lasttokens[jj_endpos++] = kind;
2235 else if (jj_endpos != 0)
2237 jj_expentry = new int[jj_endpos];
2238 for (int i = 0; i < jj_endpos; i++)
2240 jj_expentry[i] = jj_lasttokens[i];
2243 jj_lasttokens[(jj_endpos = pos) - 1] = kind;
2247 /// <summary>Generate ParseException. </summary>
2248 public virtual ParseException GenerateParseException()
2250 jj_expentries.Clear();
2251 bool[] la1tokens = new bool[34];
2254 la1tokens[jj_kind] = true;
2257 for (int i = 0; i < 23; i++)
2259 if (jj_la1[i] == jj_gen)
2261 for (int j = 0; j < 32; j++)
2263 if ((jj_la1_0[i] & (1 << j)) != 0)
2265 la1tokens[j] = true;
2267 if ((jj_la1_1[i] & (1 << j)) != 0)
2269 la1tokens[32 + j] = true;
2274 for (int i = 0; i < 34; i++)
2278 jj_expentry = new int[1];
2280 jj_expentries.Add(jj_expentry);
2285 Jj_add_error_token(0, 0);
2286 int[][] exptokseq = new int[jj_expentries.Count][];
2287 for (int i = 0; i < jj_expentries.Count; i++)
2289 exptokseq[i] = (int[]) jj_expentries[i];
2291 return new ParseException(token, exptokseq, Mono.Lucene.Net.QueryParsers.QueryParserConstants.tokenImage);
2294 /// <summary>Enable tracing. </summary>
2295 public void Enable_tracing()
2299 /// <summary>Disable tracing. </summary>
2300 public void Disable_tracing()
2304 private void Jj_rescan_token()
2307 for (int i = 0; i < 1; i++)
2311 JJCalls p = jj_2_rtns[i];
2316 jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
2320 case 0: Jj_3_1(); break;
2327 catch (LookaheadSuccess ls)
2334 private void Jj_save(int index, int xla)
2336 JJCalls p = jj_2_rtns[index];
2337 while (p.gen > jj_gen)
2341 p = p.next = new JJCalls(); break;
2345 p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
2348 internal sealed class JJCalls
2351 internal Token first;
2353 internal JJCalls next;
2355 static QueryParser()