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.
18 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
19 using Term
= Lucene
.Net
.Index
.Term
;
20 using TermPositions
= Lucene
.Net
.Index
.TermPositions
;
21 using ToStringUtils
= Lucene
.Net
.Util
.ToStringUtils
;
23 namespace Lucene
.Net
.Search
.Spans
26 /// <summary>Matches spans containing a term. </summary>
28 public class SpanTermQuery
:SpanQuery
30 private class AnonymousClassSpans
: Spans
32 public AnonymousClassSpans(Lucene
.Net
.Index
.IndexReader reader
, SpanTermQuery enclosingInstance
)
34 InitBlock(reader
, enclosingInstance
);
36 private void InitBlock(Lucene
.Net
.Index
.IndexReader reader
, SpanTermQuery enclosingInstance
)
39 this.enclosingInstance
= enclosingInstance
;
40 positions
= reader
.TermPositions(Enclosing_Instance
.term
);
42 private Lucene
.Net
.Index
.IndexReader reader
;
43 private SpanTermQuery enclosingInstance
;
44 public SpanTermQuery Enclosing_Instance
48 return enclosingInstance
;
52 private TermPositions positions
;
54 private int doc
= - 1;
59 public virtual bool Next()
63 if (!positions
.Next())
65 doc
= System
.Int32
.MaxValue
;
68 doc
= positions
.Doc();
69 freq
= positions
.Freq();
72 position
= positions
.NextPosition();
77 public virtual bool SkipTo(int target
)
79 // are we already at the correct position?
85 if (!positions
.SkipTo(target
))
87 doc
= System
.Int32
.MaxValue
;
91 doc
= positions
.Doc();
92 freq
= positions
.Freq();
95 position
= positions
.NextPosition();
101 public virtual int Doc()
105 public virtual int Start()
109 public virtual int End()
114 public override System
.String
ToString()
116 return "spans(" + Enclosing_Instance
.ToString() + ")@" + (doc
== - 1?"START":((doc
== System
.Int32
.MaxValue
)?"END":doc
+ "-" + position
));
121 /// <summary>Construct a SpanTermQuery matching the named term's spans. </summary>
122 public SpanTermQuery(Term term
)
127 /// <summary>Return the term whose spans are matched. </summary>
128 public virtual Term
GetTerm()
133 public override System
.String
GetField()
138 public override System
.Collections
.ICollection
GetTerms()
140 System
.Collections
.ArrayList terms
= new System
.Collections
.ArrayList();
145 public override System
.String
ToString(System
.String field
)
147 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
148 if (term
.Field().Equals(field
))
149 buffer
.Append(term
.Text());
152 buffer
.Append(term
.ToString());
154 buffer
.Append(ToStringUtils
.Boost(GetBoost()));
155 return buffer
.ToString();
158 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
159 public override bool Equals(System
.Object o
)
161 if (!(o
is SpanTermQuery
))
163 SpanTermQuery other
= (SpanTermQuery
) o
;
164 return (this.GetBoost() == other
.GetBoost()) && this.term
.Equals(other
.term
);
167 /// <summary>Returns a hash code value for this object.</summary>
168 public override int GetHashCode()
170 return GetBoost().ToString().GetHashCode() ^ term
.GetHashCode(); // {{Aroush-1.9}} Is this OK?
173 public override Spans
GetSpans(IndexReader reader
)
175 return new AnonymousClassSpans(reader
, this);