2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
18 namespace Lucene
.Net
.Search
.Spans
21 /// <summary>Removes matches which overlap with another SpanQuery. </summary>
23 public class SpanNotQuery
:SpanQuery
25 private class AnonymousClassSpans
: Spans
27 public AnonymousClassSpans(Lucene
.Net
.Index
.IndexReader reader
, SpanNotQuery enclosingInstance
)
29 InitBlock(reader
, enclosingInstance
);
31 private void InitBlock(Lucene
.Net
.Index
.IndexReader reader
, SpanNotQuery enclosingInstance
)
34 this.enclosingInstance
= enclosingInstance
;
35 includeSpans
= Enclosing_Instance
.include
.GetSpans(reader
);
36 excludeSpans
= Enclosing_Instance
.exclude
.GetSpans(reader
);
38 private Lucene
.Net
.Index
.IndexReader reader
;
39 private SpanNotQuery enclosingInstance
;
40 public SpanNotQuery Enclosing_Instance
44 return enclosingInstance
;
48 private Spans includeSpans
;
49 private bool moreInclude
= true;
51 private Spans excludeSpans
;
52 private bool moreExclude
= true;
54 public virtual bool Next()
57 // move to next include
58 moreInclude
= includeSpans
.Next();
60 while (moreInclude
&& moreExclude
)
63 if (includeSpans
.Doc() > excludeSpans
.Doc())
65 moreExclude
= excludeSpans
.SkipTo(includeSpans
.Doc());
67 while (moreExclude
&& includeSpans
.Doc() == excludeSpans
.Doc() && excludeSpans
.End() <= includeSpans
.Start())
69 moreExclude
= excludeSpans
.Next(); // increment exclude
72 if (!moreExclude
|| includeSpans
.Doc() != excludeSpans
.Doc() || includeSpans
.End() <= excludeSpans
.Start())
73 break; // we found a match
75 moreInclude
= includeSpans
.Next(); // intersected: keep scanning
80 public virtual bool SkipTo(int target
)
84 moreInclude
= includeSpans
.SkipTo(target
);
89 if (moreExclude
&& includeSpans
.Doc() > excludeSpans
.Doc())
90 moreExclude
= excludeSpans
.SkipTo(includeSpans
.Doc());
92 while (moreExclude
&& includeSpans
.Doc() == excludeSpans
.Doc() && excludeSpans
.End() <= includeSpans
.Start())
94 moreExclude
= excludeSpans
.Next(); // increment exclude
97 if (!moreExclude
|| includeSpans
.Doc() != excludeSpans
.Doc() || includeSpans
.End() <= excludeSpans
.Start())
98 return true; // we found a match
100 return Next(); // scan to next match
103 public virtual int Doc()
105 return includeSpans
.Doc();
107 public virtual int Start()
109 return includeSpans
.Start();
111 public virtual int End()
113 return includeSpans
.End();
116 public override System
.String
ToString()
118 return "spans(" + Enclosing_Instance
.ToString() + ")";
121 private SpanQuery include
;
122 private SpanQuery exclude
;
124 /// <summary>Construct a SpanNotQuery matching spans from <code>include</code> which
125 /// have no overlap with spans from <code>exclude</code>.
127 public SpanNotQuery(SpanQuery include
, SpanQuery exclude
)
129 this.include
= include
;
130 this.exclude
= exclude
;
132 if (!include
.GetField().Equals(exclude
.GetField()))
133 throw new System
.ArgumentException("Clauses must have same Field.");
136 /// <summary>Return the SpanQuery whose matches are filtered. </summary>
137 public virtual SpanQuery
GetInclude()
142 /// <summary>Return the SpanQuery whose matches must not overlap those returned. </summary>
143 public virtual SpanQuery
GetExclude()
148 public override System
.String
GetField()
150 return include
.GetField();
153 public override System
.Collections
.ICollection
GetTerms()
155 return include
.GetTerms();
158 public override System
.String
ToString(System
.String field
)
160 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
161 buffer
.Append("spanNot(");
162 buffer
.Append(include
.ToString(field
));
164 buffer
.Append(exclude
.ToString(field
));
166 return buffer
.ToString();
170 public override Spans
GetSpans(IndexReader reader
)
172 return new AnonymousClassSpans(reader
, this);