SpanNotQuery.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:6k
源码类别:

搜索引擎

开发平台:

C#

  1. /*
  2.  * Copyright 2004 The Apache Software Foundation
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. using System;
  17. using IndexReader = Lucene.Net.Index.IndexReader;
  18. using Query = Lucene.Net.Search.Query;
  19. using ToStringUtils = Lucene.Net.Util.ToStringUtils;
  20. namespace Lucene.Net.Search.Spans
  21. {
  22. /// <summary>Removes matches which overlap with another SpanQuery. </summary>
  23. [Serializable]
  24. public class SpanNotQuery : SpanQuery
  25. {
  26. private class AnonymousClassSpans : Spans
  27. {
  28. public AnonymousClassSpans(Lucene.Net.Index.IndexReader reader, SpanNotQuery enclosingInstance)
  29. {
  30. InitBlock(reader, enclosingInstance);
  31. }
  32. private void  InitBlock(Lucene.Net.Index.IndexReader reader, SpanNotQuery enclosingInstance)
  33. {
  34. this.reader = reader;
  35. this.enclosingInstance = enclosingInstance;
  36. includeSpans = Enclosing_Instance.include.GetSpans(reader);
  37. excludeSpans = Enclosing_Instance.exclude.GetSpans(reader);
  38. }
  39. private Lucene.Net.Index.IndexReader reader;
  40. private SpanNotQuery enclosingInstance;
  41. public SpanNotQuery Enclosing_Instance
  42. {
  43. get
  44. {
  45. return enclosingInstance;
  46. }
  47. }
  48. private Spans includeSpans;
  49. private bool moreInclude = true;
  50. private Spans excludeSpans;
  51. private bool moreExclude = true;
  52. public virtual bool Next()
  53. {
  54. if (moreInclude)
  55. // move to next include
  56. moreInclude = includeSpans.Next();
  57. while (moreInclude && moreExclude)
  58. {
  59. if (includeSpans.Doc() > excludeSpans.Doc())
  60. // skip exclude
  61. moreExclude = excludeSpans.SkipTo(includeSpans.Doc());
  62. while (moreExclude && includeSpans.Doc() == excludeSpans.Doc() && excludeSpans.End() <= includeSpans.Start())
  63. {
  64. moreExclude = excludeSpans.Next(); // increment exclude
  65. }
  66. if (!moreExclude || includeSpans.Doc() != excludeSpans.Doc() || includeSpans.End() <= excludeSpans.Start())
  67. break; // we found a match
  68. moreInclude = includeSpans.Next(); // intersected: keep scanning
  69. }
  70. return moreInclude;
  71. }
  72. public virtual bool SkipTo(int target)
  73. {
  74. if (moreInclude)
  75. // skip include
  76. moreInclude = includeSpans.SkipTo(target);
  77. if (!moreInclude)
  78. return false;
  79. if (moreExclude && includeSpans.Doc() > excludeSpans.Doc())
  80. moreExclude = excludeSpans.SkipTo(includeSpans.Doc());
  81. while (moreExclude && includeSpans.Doc() == excludeSpans.Doc() && excludeSpans.End() <= includeSpans.Start())
  82. {
  83. moreExclude = excludeSpans.Next(); // increment exclude
  84. }
  85. if (!moreExclude || includeSpans.Doc() != excludeSpans.Doc() || includeSpans.End() <= excludeSpans.Start())
  86. return true; // we found a match
  87. return Next(); // scan to next match
  88. }
  89. public virtual int Doc()
  90. {
  91. return includeSpans.Doc();
  92. }
  93. public virtual int Start()
  94. {
  95. return includeSpans.Start();
  96. }
  97. public virtual int End()
  98. {
  99. return includeSpans.End();
  100. }
  101. public override System.String ToString()
  102. {
  103. return "spans(" + Enclosing_Instance.ToString() + ")";
  104. }
  105. }
  106. private SpanQuery include;
  107. private SpanQuery exclude;
  108. /// <summary>Construct a SpanNotQuery matching spans from <code>include</code> which
  109. /// have no overlap with spans from <code>exclude</code>.
  110. /// </summary>
  111. public SpanNotQuery(SpanQuery include, SpanQuery exclude)
  112. {
  113. this.include = include;
  114. this.exclude = exclude;
  115. if (!include.GetField().Equals(exclude.GetField()))
  116. throw new System.ArgumentException("Clauses must have same field.");
  117. }
  118. /// <summary>Return the SpanQuery whose matches are filtered. </summary>
  119. public virtual SpanQuery GetInclude()
  120. {
  121. return include;
  122. }
  123. /// <summary>Return the SpanQuery whose matches must not overlap those returned. </summary>
  124. public virtual SpanQuery GetExclude()
  125. {
  126. return exclude;
  127. }
  128. public override System.String GetField()
  129. {
  130. return include.GetField();
  131. }
  132. public override System.Collections.ICollection GetTerms()
  133. {
  134. return include.GetTerms();
  135. }
  136. public override System.String ToString(System.String field)
  137. {
  138. System.Text.StringBuilder buffer = new System.Text.StringBuilder();
  139. buffer.Append("spanNot(");
  140. buffer.Append(include.ToString(field));
  141. buffer.Append(", ");
  142. buffer.Append(exclude.ToString(field));
  143. buffer.Append(")");
  144. buffer.Append(ToStringUtils.Boost(GetBoost()));
  145. return buffer.ToString();
  146. }
  147. public override Spans GetSpans(IndexReader reader)
  148. {
  149. return new AnonymousClassSpans(reader, this);
  150. }
  151. public override Query Rewrite(IndexReader reader)
  152. {
  153. SpanNotQuery clone = null;
  154. SpanQuery rewrittenInclude = (SpanQuery) include.Rewrite(reader);
  155. if (rewrittenInclude != include)
  156. {
  157. clone = (SpanNotQuery) this.Clone();
  158. clone.include = rewrittenInclude;
  159. }
  160. SpanQuery rewrittenExclude = (SpanQuery) exclude.Rewrite(reader);
  161. if (rewrittenExclude != exclude)
  162. {
  163. if (clone == null)
  164. clone = (SpanNotQuery) this.Clone();
  165. clone.exclude = rewrittenExclude;
  166. }
  167. if (clone != null)
  168. {
  169. return clone; // some clauses rewrote
  170. }
  171. else
  172. {
  173. return this; // no clauses rewrote
  174. }
  175. }
  176. }
  177. }