Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanTermQuery.cs
blob9e67a9e0e8fcb67654eacf6132d4ed4bc148d0ce
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.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 using Term = Lucene.Net.Index.Term;
19 using TermPositions = Lucene.Net.Index.TermPositions;
20 namespace Lucene.Net.Search.Spans
23 /// <summary>Matches spans containing a term. </summary>
24 [Serializable]
25 public class SpanTermQuery:SpanQuery
27 private class AnonymousClassSpans : Spans
29 public AnonymousClassSpans(Lucene.Net.Index.IndexReader reader, SpanTermQuery enclosingInstance)
31 InitBlock(reader, enclosingInstance);
33 private void InitBlock(Lucene.Net.Index.IndexReader reader, SpanTermQuery enclosingInstance)
35 this.reader = reader;
36 this.enclosingInstance = enclosingInstance;
37 positions = reader.TermPositions(Enclosing_Instance.term);
39 private Lucene.Net.Index.IndexReader reader;
40 private SpanTermQuery enclosingInstance;
41 public SpanTermQuery Enclosing_Instance
43 get
45 return enclosingInstance;
49 private TermPositions positions;
51 private int doc = - 1;
52 private int freq;
53 private int count;
54 private int position;
56 public virtual bool Next()
58 if (count == freq)
60 if (!positions.Next())
62 doc = System.Int32.MaxValue;
63 return false;
65 doc = positions.Doc();
66 freq = positions.Freq();
67 count = 0;
69 position = positions.NextPosition();
70 count++;
71 return true;
74 public virtual bool SkipTo(int target)
76 if (!positions.SkipTo(target))
78 doc = System.Int32.MaxValue;
79 return false;
82 doc = positions.Doc();
83 freq = positions.Freq();
84 count = 0;
86 position = positions.NextPosition();
87 count++;
89 return true;
92 public virtual int Doc()
94 return doc;
96 public virtual int Start()
98 return position;
100 public virtual int End()
102 return position + 1;
105 public override System.String ToString()
107 return "spans(" + Enclosing_Instance.ToString() + ")@" + (doc == - 1?"START":((doc == System.Int32.MaxValue)?"END":doc + "-" + position));
110 private Term term;
112 /// <summary>Construct a SpanTermQuery matching the named term's spans. </summary>
113 public SpanTermQuery(Term term)
115 this.term = term;
118 /// <summary>Return the term whose spans are matched. </summary>
119 public virtual Term GetTerm()
121 return term;
124 public override System.String GetField()
126 return term.Field();
129 public override System.Collections.ICollection GetTerms()
131 System.Collections.ArrayList terms = new System.Collections.ArrayList();
132 terms.Add(term);
133 return terms;
136 public override System.String ToString(System.String field)
138 if (term.Field().Equals(field))
139 return term.Text();
140 else
142 return term.ToString();
146 public override Spans GetSpans(IndexReader reader)
148 return new AnonymousClassSpans(reader, this);