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
SpanTermQuery.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 
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using Term = Lucene.Net.Index.Term;
22 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
23 
24 namespace Lucene.Net.Search.Spans
25 {
26 
27  /// <summary>Matches spans containing a term. </summary>
28  [Serializable]
29  public class SpanTermQuery:SpanQuery
30  {
31  protected internal Term internalTerm;
32 
33  /// <summary>Construct a SpanTermQuery matching the named term's spans. </summary>
34  public SpanTermQuery(Term term)
35  {
36  this.internalTerm = term;
37  }
38 
39  /// <summary>Return the term whose spans are matched. </summary>
40  public virtual Term Term
41  {
42  get { return internalTerm; }
43  }
44 
45  public override string Field
46  {
47  get { return internalTerm.Field; }
48  }
49 
50  public override void ExtractTerms(System.Collections.Generic.ISet<Term> terms)
51  {
52  terms.Add(internalTerm);
53  }
54 
55  public override System.String ToString(System.String field)
56  {
57  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
58  if (internalTerm.Field.Equals(field))
59  buffer.Append(internalTerm.Text);
60  else
61  {
62  buffer.Append(internalTerm.ToString());
63  }
64  buffer.Append(ToStringUtils.Boost(Boost));
65  return buffer.ToString();
66  }
67 
68  public override int GetHashCode()
69  {
70  int prime = 31;
71  int result = base.GetHashCode();
72  result = prime * result + ((internalTerm == null)?0:internalTerm.GetHashCode());
73  return result;
74  }
75 
76  public override bool Equals(System.Object obj)
77  {
78  if (this == obj)
79  return true;
80  if (!base.Equals(obj))
81  return false;
82  if (GetType() != obj.GetType())
83  return false;
84  SpanTermQuery other = (SpanTermQuery) obj;
85  if (internalTerm == null)
86  {
87  if (other.internalTerm != null)
88  return false;
89  }
90  else if (!internalTerm.Equals(other.internalTerm))
91  return false;
92  return true;
93  }
94 
95  public override Spans GetSpans(IndexReader reader)
96  {
97  return new TermSpans(reader.TermPositions(internalTerm), internalTerm);
98  }
99  }
100 }