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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 using StringHelper = Mono.Lucene.Net.Util.StringHelper;
22 namespace Mono.Lucene.Net.Index
25 /// <summary>A Term represents a word from text. This is the unit of search. It is
26 /// composed of two elements, the text of the word, as a string, and the name of
27 /// the field that the text occured in, an interned string.
28 /// Note that terms may represent more than words from text fields, but also
29 /// things like dates, email addresses, urls, etc.
33 public sealed class Term : System.IComparable
35 internal System.String field;
36 internal System.String text;
38 /// <summary>Constructs a Term with the given field and text.
39 /// <p/>Note that a null field or null text value results in undefined
40 /// behavior for most Lucene APIs that accept a Term parameter.
42 public Term(System.String fld, System.String txt)
44 field = StringHelper.Intern(fld);
48 /// <summary>Constructs a Term with the given field and empty text.
49 /// This serves two purposes: 1) reuse of a Term with the same field.
50 /// 2) pattern for a query.
53 /// <param name="fld">
55 public Term(System.String fld):this(fld, "", true)
59 internal Term(System.String fld, System.String txt, bool intern)
61 field = intern?StringHelper.Intern(fld):fld; // field names are interned
62 text = txt; // unless already known to be
65 /// <summary>Returns the field of this term, an interned string. The field indicates
66 /// the part of a document which this term came from.
68 public System.String Field()
73 /// <summary>Returns the text of this term. In the case of words, this is simply the
74 /// text of the word. In the case of dates and other types, this is an
75 /// encoding of the object as a string.
77 public System.String Text()
82 /// <summary> Optimized construction of new Terms by reusing same field as this Term
83 /// - avoids field.intern() overhead
85 /// <param name="text">The text of the new term (field is implicitly same as this Term instance)
87 /// <returns> A new Term
89 public Term CreateTerm(System.String text)
91 return new Term(field, text, false);
95 public override bool Equals(System.Object obj)
101 if (GetType() != obj.GetType())
103 Term other = (Term) obj;
106 if (other.field != null)
109 else if (!field.Equals(other.field))
113 if (other.text != null)
116 else if (!text.Equals(other.text))
122 public override int GetHashCode()
126 result = prime * result + ((field == null)?0:field.GetHashCode());
127 result = prime * result + ((text == null)?0:text.GetHashCode());
131 public int CompareTo(System.Object other)
133 return CompareTo((Term) other);
136 /// <summary>Compares two terms, returning a negative integer if this
137 /// term belongs before the argument, zero if this term is equal to the
138 /// argument, and a positive integer if this term belongs after the argument.
139 /// The ordering of terms is first by field, then by text.
141 public int CompareTo(Term other)
143 if ((System.Object) field == (System.Object) other.field)
144 // fields are interned
145 return String.CompareOrdinal(text, other.text);
147 return String.CompareOrdinal(field, other.field);
150 /// <summary>Resets the field and text of a Term. </summary>
151 internal void Set(System.String fld, System.String txt)
157 public override System.String ToString()
159 return field + ":" + text;
162 // private void ReadObject(System.IO.BinaryReader in_Renamed)
164 // in_Renamed.defaultReadObject();
165 // field = StringHelper.Intern(field);
168 [System.Runtime.Serialization.OnDeserialized]
169 internal void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
171 field = StringHelper.Intern(field);
174 public System.String text_ForNUnit