Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SpanFirstQuery.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using Lucene.Net.Index;
21 using Lucene.Net.Support;
22 using IndexReader = Lucene.Net.Index.IndexReader;
23 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
24 using Query = Lucene.Net.Search.Query;
25 
26 namespace Lucene.Net.Search.Spans
27 {
28 
29  /// <summary>Matches spans near the beginning of a field. </summary>
30  [Serializable]
31  public class SpanFirstQuery : SpanQuery, System.ICloneable
32  {
33  private class AnonymousClassSpans : Spans
34  {
35  public AnonymousClassSpans(Lucene.Net.Index.IndexReader reader, SpanFirstQuery enclosingInstance)
36  {
37  InitBlock(reader, enclosingInstance);
38  }
39  private void InitBlock(Lucene.Net.Index.IndexReader reader, SpanFirstQuery enclosingInstance)
40  {
41  this.reader = reader;
42  this.enclosingInstance = enclosingInstance;
43  spans = Enclosing_Instance.match.GetSpans(reader);
44  }
45  private Lucene.Net.Index.IndexReader reader;
46  private SpanFirstQuery enclosingInstance;
47  public SpanFirstQuery Enclosing_Instance
48  {
49  get
50  {
51  return enclosingInstance;
52  }
53 
54  }
55  private Spans spans;
56 
57  public override bool Next()
58  {
59  while (spans.Next())
60  {
61  // scan to next match
62  if (End() <= Enclosing_Instance.end)
63  return true;
64  }
65  return false;
66  }
67 
68  public override bool SkipTo(int target)
69  {
70  if (!spans.SkipTo(target))
71  return false;
72 
73  return spans.End() <= Enclosing_Instance.end || Next();
74  }
75 
76  public override int Doc()
77  {
78  return spans.Doc();
79  }
80  public override int Start()
81  {
82  return spans.Start();
83  }
84  public override int End()
85  {
86  return spans.End();
87  }
88 
89  // TODO: Remove warning after API has been finalized
90 
91  public override ICollection<byte[]> GetPayload()
92  {
93  System.Collections.Generic.ICollection<byte[]> result = null;
94  if (spans.IsPayloadAvailable())
95  {
96  result = spans.GetPayload();
97  }
98  return result; //TODO: any way to avoid the new construction?
99  }
100 
101  // TODO: Remove warning after API has been finalized
102 
103  public override bool IsPayloadAvailable()
104  {
105  return spans.IsPayloadAvailable();
106  }
107 
108  public override System.String ToString()
109  {
110  return "spans(" + Enclosing_Instance.ToString() + ")";
111  }
112  }
113  private SpanQuery match;
114  private int end;
115 
116  /// <summary>Construct a SpanFirstQuery matching spans in <c>match</c> whose end
117  /// position is less than or equal to <c>end</c>.
118  /// </summary>
119  public SpanFirstQuery(SpanQuery match, int end)
120  {
121  this.match = match;
122  this.end = end;
123  }
124 
125  /// <summary>Return the SpanQuery whose matches are filtered. </summary>
126  public virtual SpanQuery Match
127  {
128  get { return match; }
129  }
130 
131  /// <summary>Return the maximum end position permitted in a match. </summary>
132  public virtual int End
133  {
134  get { return end; }
135  }
136 
137  public override string Field
138  {
139  get { return match.Field; }
140  }
141 
142  public override System.String ToString(System.String field)
143  {
144  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
145  buffer.Append("spanFirst(");
146  buffer.Append(match.ToString(field));
147  buffer.Append(", ");
148  buffer.Append(end);
149  buffer.Append(")");
150  buffer.Append(ToStringUtils.Boost(Boost));
151  return buffer.ToString();
152  }
153 
154  public override System.Object Clone()
155  {
156  SpanFirstQuery spanFirstQuery = new SpanFirstQuery((SpanQuery) match.Clone(), end);
157  spanFirstQuery.Boost = Boost;
158  return spanFirstQuery;
159  }
160 
161  public override void ExtractTerms(System.Collections.Generic.ISet<Term> terms)
162  {
163  match.ExtractTerms(terms);
164  }
165 
166  public override Spans GetSpans(IndexReader reader)
167  {
168  return new AnonymousClassSpans(reader, this);
169  }
170 
171  public override Query Rewrite(IndexReader reader)
172  {
173  SpanFirstQuery clone = null;
174 
175  SpanQuery rewritten = (SpanQuery) match.Rewrite(reader);
176  if (rewritten != match)
177  {
178  clone = (SpanFirstQuery) this.Clone();
179  clone.match = rewritten;
180  }
181 
182  if (clone != null)
183  {
184  return clone; // some clauses rewrote
185  }
186  else
187  {
188  return this; // no clauses rewrote
189  }
190  }
191 
192  public override bool Equals(System.Object o)
193  {
194  if (this == o)
195  return true;
196  if (!(o is SpanFirstQuery))
197  return false;
198 
199  SpanFirstQuery other = (SpanFirstQuery) o;
200  return this.end == other.end && this.match.Equals(other.match) && this.Boost == other.Boost;
201  }
202 
203  public override int GetHashCode()
204  {
205  int h = match.GetHashCode();
206  h ^= ((h << 8) | (Number.URShift(h, 25))); // reversible
207  h ^= System.Convert.ToInt32(Boost) ^ end;
208  return h;
209  }
210  }
211 }