19 using System.Collections.Generic;
21 using Lucene.Net.Index;
22 using Lucene.Net.Support;
23 using Lucene.Net.Util;
25 using Single = Lucene.Net.Support.Single;
29 namespace Lucene.Net.Search
44 public const float defaultMinSimilarity = 0.5f;
45 public const int defaultPrefixLength = 0;
47 private float minimumSimilarity;
48 private int prefixLength;
49 private bool termLongEnough =
false;
52 public Term Term {
get;
protected internal set; }
77 if (minimumSimilarity >= 1.0f)
78 throw new System.ArgumentException(
"minimumSimilarity >= 1");
79 else if (minimumSimilarity < 0.0f)
80 throw new System.ArgumentException(
"minimumSimilarity < 0");
82 throw new System.ArgumentException(
"prefixLength < 0");
84 if (term.
Text.Length > 1.0f / (1.0f - minimumSimilarity))
86 this.termLongEnough =
true;
89 this.minimumSimilarity = minimumSimilarity;
90 this.prefixLength = prefixLength;
91 internalRewriteMethod = SCORING_BOOLEAN_QUERY_REWRITE;
95 public FuzzyQuery(
Term term,
float minimumSimilarity):this(term, minimumSimilarity, defaultPrefixLength)
100 public FuzzyQuery(
Term term):this(term, defaultMinSimilarity, defaultPrefixLength)
106 public virtual float MinSimilarity
108 get {
return minimumSimilarity; }
115 public virtual int PrefixLength
117 get {
return prefixLength; }
125 public override RewriteMethod RewriteMethod
127 set {
throw new System.NotSupportedException(
"FuzzyQuery cannot change rewrite method"); }
143 SortedList<ScoreTerm, ScoreTerm> stQueue =
new SortedList<ScoreTerm, ScoreTerm>();
148 ScoreTerm st =
new ScoreTerm();
152 if (t == null)
break;
153 float score = enumerator.Difference();
155 if (stQueue.Count >= maxSize && score <= stQueue.Keys.First().score)
162 if (stQueue.Count > maxSize)
164 st = stQueue.Keys.First();
169 st =
new ScoreTerm();
172 while (enumerator.Next());
180 foreach(ScoreTerm st
in stQueue.Keys)
183 tq.
Boost = Boost * st.score;
184 query.Add(tq,
Occur.SHOULD);
190 public override System.String ToString(System.String field)
192 System.Text.StringBuilder buffer =
new System.Text.StringBuilder();
202 return buffer.ToString();
205 protected internal class ScoreTerm : IComparable<ScoreTerm>
210 public int CompareTo(ScoreTerm other)
212 if (Comparer<float>.Default.Compare(
this.score, other.score) == 0)
214 return other.term.CompareTo(this.term);
218 return Comparer<float>.Default.Compare(this.score, other.score);
223 public override int GetHashCode()
226 int result = base.GetHashCode();
227 result = prime * result + BitConverter.ToInt32(BitConverter.GetBytes(minimumSimilarity), 0);
228 result = prime * result + prefixLength;
229 result = prime * result + ((
Term == null)?0:Term.GetHashCode());
233 public override bool Equals(System.Object obj)
237 if (!base.Equals(obj))
239 if (GetType() != obj.GetType())
242 if (BitConverter.ToInt32(BitConverter.GetBytes(minimumSimilarity), 0) != BitConverter.ToInt32(BitConverter.GetBytes(other.minimumSimilarity), 0))
244 if (prefixLength != other.prefixLength)
248 if (other.
Term != null)