⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queryparser.cs

📁 Lucene.Net 版本源码 测试通过
💻 CS
📖 第 1 页 / 共 4 页
字号:
				{
					t = null;
				}
				if (t == null)
					break;
				v.Add(t);
				if (t.GetPositionIncrement() != 0)
					positionCount += t.GetPositionIncrement();
				else
					severalTokensAtSamePosition = true;
			}
			try
			{
				source.Close();
			}
			catch (System.IO.IOException e)
			{
				// ignore
			}
			
			if (v.Count == 0)
				return null;
			else if (v.Count == 1)
			{
				t = (Lucene.Net.Analysis.Token) v[0];
				return new TermQuery(new Term(field, t.TermText()));
			}
			else
			{
				if (severalTokensAtSamePosition)
				{
					if (positionCount == 1)
					{
						// no phrase query:
						BooleanQuery q = new BooleanQuery(true);
						for (int i = 0; i < v.Count; i++)
						{
							t = (Lucene.Net.Analysis.Token) v[i];
							TermQuery currentQuery = new TermQuery(new Term(field, t.TermText()));
							q.Add(currentQuery, BooleanClause.Occur.SHOULD);
						}
						return q;
					}
					else
					{
						// phrase query:
						MultiPhraseQuery mpq = new MultiPhraseQuery();
						mpq.SetSlop(phraseSlop);
						System.Collections.ArrayList multiTerms = new System.Collections.ArrayList();
						for (int i = 0; i < v.Count; i++)
						{
							t = (Lucene.Net.Analysis.Token) v[i];
							if (t.GetPositionIncrement() == 1 && multiTerms.Count > 0)
							{
								mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
								multiTerms.Clear();
							}
							multiTerms.Add(new Term(field, t.TermText()));
						}
						mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
						return mpq;
					}
				}
				else
				{
					PhraseQuery q = new PhraseQuery();
					q.SetSlop(phraseSlop);
					for (int i = 0; i < v.Count; i++)
					{
						q.Add(new Term(field, ((Lucene.Net.Analysis.Token) v[i]).TermText()));
					}
					return q;
				}
			}
		}
		
		
		/// <summary> Base implementation delegates to {@link #GetFieldQuery(String,String)}.
		/// This method may be overridden, for example, to return
		/// a SpanNearQuery instead of a PhraseQuery.
		/// 
		/// </summary>
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetFieldQuery(System.String field, System.String queryText, int slop)
		{
			Query query = GetFieldQuery(field, queryText);
			
			if (query is PhraseQuery)
			{
				((PhraseQuery) query).SetSlop(slop);
			}
			if (query is MultiPhraseQuery)
			{
				((MultiPhraseQuery) query).SetSlop(slop);
			}
			
			return query;
		}
		
		
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetRangeQuery(System.String field, System.String part1, System.String part2, bool inclusive)
		{
			if (lowercaseExpandedTerms)
			{
				part1 = part1.ToLower();
				part2 = part2.ToLower();
			}
			try
			{
                System.DateTime d1;
                System.DateTime d2;

                try
                {
                    d1 = System.DateTime.Parse(part1, locale);
                }
                catch (System.Exception)
                {
                    d1 = System.DateTime.Parse(part1);
                }
                try
                {
                    d2 = System.DateTime.Parse(part2, locale);
                }
                catch (System.Exception)
                {
                    d2 = System.DateTime.Parse(part2);
                }

                if (inclusive)
                {
                    // The user can only specify the date, not the time, so make sure
                    // the time is set to the latest possible time of that date to really
                    // include all documents:
                    System.Globalization.Calendar cal = new System.Globalization.GregorianCalendar();
                    System.DateTime tempDate = d2;
                    d2 = d2.AddHours(23 - tempDate.Hour);
                    d2 = d2.AddMinutes(59 - tempDate.Minute);
                    d2 = d2.AddSeconds(59 - tempDate.Second);
                    d2 = d2.AddMilliseconds(999 - tempDate.Millisecond);
                }
                DateTools.Resolution resolution = GetDateResolution(field);
				if (resolution == null)
				{
					// no default or field specific date resolution has been set,
					// use deprecated DateField to maintain compatibilty with
					// pre-1.9 Lucene versions.
					part1 = DateField.DateToString(d1);
					part2 = DateField.DateToString(d2);
				}
				else
				{
					part1 = DateTools.DateToString(d1, resolution);
					part2 = DateTools.DateToString(d2, resolution);
				}
			}
			catch (System.Exception e)
			{
			}
			
			if (useOldRangeQuery)
			{
				return new RangeQuery(new Term(field, part1), new Term(field, part2), inclusive);
			}
			else
			{
				return new ConstantScoreRangeQuery(field, part1, part2, inclusive, inclusive);
			}
		}
		
		/// <summary> 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.
		/// 
		/// </summary>
		/// <param name="clauses">Vector that contains {@link BooleanClause} instances
		/// to join.
		/// 
		/// </param>
		/// <returns> Resulting {@link Query} object.
		/// </returns>
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetBooleanQuery(System.Collections.ArrayList clauses)
		{
			return GetBooleanQuery(clauses, false);
		}
		
		/// <summary> 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.
		/// 
		/// </summary>
		/// <param name="clauses">Vector that contains {@link BooleanClause} instances
		/// to join.
		/// </param>
		/// <param name="disableCoord">true if coord scoring should be disabled.
		/// 
		/// </param>
		/// <returns> Resulting {@link Query} object.
		/// </returns>
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetBooleanQuery(System.Collections.ArrayList clauses, bool disableCoord)
		{
			BooleanQuery query = new BooleanQuery(disableCoord);
			for (int i = 0; i < clauses.Count; i++)
			{
				query.Add((BooleanClause) clauses[i]);
			}
			return query;
		}
		
		/// <summary> 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)
		/// <p>
		/// 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.
		/// <p>
		/// Can be overridden by extending classes, to provide custom handling for
		/// wildcard queries, which may be necessary due to missing analyzer calls.
		/// 
		/// </summary>
		/// <param name="field">Name of the field query will use.
		/// </param>
		/// <param name="termStr">Term token that contains one or more wild card
		/// characters (? or *), but is not simple prefix term
		/// 
		/// </param>
		/// <returns> Resulting {@link Query} built for the term
		/// </returns>
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetWildcardQuery(System.String field, System.String termStr)
		{
			if ("*".Equals(field))
			{
				if ("*".Equals(termStr))
					return new MatchAllDocsQuery();
			}
			if (!allowLeadingWildcard && (termStr.StartsWith("*") || termStr.StartsWith("?")))
				throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery");
			if (lowercaseExpandedTerms)
			{
				termStr = termStr.ToLower();
			}
			Term t = new Term(field, termStr);
			return new WildcardQuery(t);
		}
		
		/// <summary> Factory method for generating a query (similar to
		/// {@link #getWildcardQuery}). 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.
		/// <p>
		/// 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.
		/// <p>
		/// Can be overridden by extending classes, to provide custom handling for
		/// wild card queries, which may be necessary due to missing analyzer calls.
		/// 
		/// </summary>
		/// <param name="field">Name of the field query will use.
		/// </param>
		/// <param name="termStr">Term token to use for building term for the query
		/// (<b>without</b> trailing '*' character!)
		/// 
		/// </param>
		/// <returns> Resulting {@link Query} built for the term
		/// </returns>
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetPrefixQuery(System.String field, System.String termStr)
		{
			if (!allowLeadingWildcard && termStr.StartsWith("*"))
				throw new ParseException("'*' not allowed as first character in PrefixQuery");
			if (lowercaseExpandedTerms)
			{
				termStr = termStr.ToLower();
			}
			Term t = new Term(field, termStr);
			return new PrefixQuery(t);
		}
		
		
		/// <summary> Factory method for generating a query (similar to
		/// {@link #getWildcardQuery}). Called when parser parses
		/// an input term token that has the fuzzy suffix (~) appended.
		/// 
		/// </summary>
		/// <param name="field">Name of the field query will use.
		/// </param>
		/// <param name="termStr">Term token to use for building term for the query
		/// 
		/// </param>
		/// <returns> Resulting {@link Query} built for the term
		/// </returns>
		/// <exception cref=""> ParseException throw in overridden method to disallow
		/// </exception>
		protected internal virtual Query GetFuzzyQuery(System.String field, System.String termStr, float minSimilarity)
		{
			if (lowercaseExpandedTerms)
			{
				termStr = termStr.ToLower();
			}
			Term t = new Term(field, termStr);
			return new FuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
		}
		
		/// <summary> Returns a String where the escape char has been
		/// removed, or kept only once if there was a double escape.
		/// 
		/// Supports escaped unicode characters, e. g. translates
		/// <code>A</code> to <code>A</code>.
		/// 
		/// </summary>
		private System.String DiscardEscapeChar(System.String input)
		{
			// Create char array to hold unescaped char sequence
			char[] output = new char[input.Length];
			
			// The length of the output can be less than the input
			// due to discarded escape chars. This variable holds
			// the actual length of the output
			int length = 0;
			
			// We remember whether the last processed character was 
			// an escape character
			bool lastCharWasEscapeChar = false;
			
			// The multiplier the current unicode digit must be multiplied with.
			// E. g. the first digit must be multiplied with 16^3, the second with 16^2...
			int codePointMultiplier = 0;
			
			// Used to calculate the codepoint of the escaped unicode character
			int codePoint = 0;
			
			for (int i = 0; i < input.Length; i++)
			{
				char curChar = input[i];
				if (codePointMultiplier > 0)
				{
					codePoint += HexToInt(curChar) * codePointMultiplier;
					codePointMultiplier = SupportClass.Number.URShift(codePointMultiplier, 4);
					if (codePointMultiplier == 0)
					{
						output[length++] = (char) codePoint;
						codePoint = 0;
					}
				}
				else if (lastCharWasEscapeChar)
				{
					if (curChar == 'u')
					{
						// found an escaped unicode character
						codePointMultiplier = 16 * 16 * 16;
					}
					else
					{
						// this character was escaped
						output[length] = curChar;
						length++;
					}
					lastCharWasEscapeChar = false;
				}
				else
				{
					if (curChar == '\\')
					{
						lastCharWasEscapeChar = true;
					}
					else
					{
						output[length] = curChar;
						length++;
					}
				}
			}
			
			if (codePointMultiplier > 0)
			{
				throw new ParseException("Truncated unicode escape sequence.");
			}
			
			if (lastCharWasEscapeChar)
			{
				throw new ParseException("Term can not end with escape character.");
			}
			
			return new System.String(output, 0, length);
		}
		
		/// <summary>Returns the numeric value of the hexadecimal character </summary>
		private static int HexToInt(char c)
		{
			if ('0' <= c && c <= '9')
			{
				return c - '0';
			}
			else if ('a' <= c && c <= 'f')
			{
				return c - 'a' + 10;
			}
			else if ('A' <= c && c <= 'F')
			{
				return c - 'A' + 10;
			}
			else
			{
				throw new ParseException("None-hex character in unicode escape sequence: " + c);
			}
		}
		
		/// <summary> Returns a String where those characters that QueryParser
		/// expects to be escaped are escaped by a preceding <code>\</code>.
		/// </summary>
		public static System.String Escape(System.String s)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			for (int i = 0; i < s.Length; i++)
			{
				char c = s[i];
				// NOTE: keep this in sync with _ESCAPED_CHAR below!
				if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?')
				{
					sb.Append('\\');
				}
				sb.Append(c);
			}
			return sb.ToString();
		}
		
		/// <summary> Command line tool to test QueryParser, using {@link Lucene.Net.Analysis.SimpleAnalyzer}.
		/// Usage:<br>
		/// <code>java Lucene.Net.QueryParsers.QueryParser &lt;input&gt;</code>
		/// </summary>
		[STAThread]
		public static void  Main(System.String[] args)
		{
			if (args.Length == 0)
			{
				System.Console.Out.WriteLine("Usage: java Lucene.Net.QueryParsers.QueryParser <input>");
				System.Environment.Exit(0);
			}
			QueryParser qp = new QueryParser("field", new Lucene.Net.Analysis.SimpleAnalyzer());
			Query q = qp.Parse(args[0]);
			System.Console.Out.WriteLine(q.ToString("field"));
		}
		
		// *   Query  ::= ( Clause )*
		// *   Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
		public int Conjunction()
		{
			int ret = CONJ_NONE;
			switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
			{
				
				case Lucene.Net.QueryParsers.QueryParserConstants.AND: 
				case Lucene.Net.QueryParsers.QueryParserConstants.OR: 
					switch ((jj_ntk == - 1) ? Jj_ntk() : jj_ntk)
					{
						
						case Lucene.Net.QueryParsers.QueryParserConstants.AND: 
							Jj_consume_token(Lucene.Net.QueryParsers.QueryParserConstants.AND);
							ret = CONJ_AND;
							break;
						

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -