2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Search / MultiTermQuery.cs
blobaeb31290fa6fa7518d17d6c158618abf8d00e0dc
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 namespace Lucene.Net.Search
22 /// <summary> A {@link Query} that matches documents containing a subset of terms provided
23 /// by a {@link FilteredTermEnum} enumeration.
24 /// <P>
25 /// <code>MultiTermQuery</code> is not designed to be used by itself.
26 /// <BR>
27 /// The reason being that it is not intialized with a {@link FilteredTermEnum}
28 /// enumeration. A {@link FilteredTermEnum} enumeration needs to be provided.
29 /// <P>
30 /// For example, {@link WildcardQuery} and {@link FuzzyQuery} extend
31 /// <code>MultiTermQuery</code> to provide {@link WildcardTermEnum} and
32 /// {@link FuzzyTermEnum}, respectively.
33 /// </summary>
34 [Serializable]
35 public abstract class MultiTermQuery:Query
37 private Term term;
39 /// <summary>Constructs a query for terms matching <code>term</code>. </summary>
40 public MultiTermQuery(Term term)
42 this.term = term;
45 /// <summary>Returns the pattern term. </summary>
46 public virtual Term GetTerm()
48 return term;
51 /// <summary>Construct the enumeration to be used, expanding the pattern term. </summary>
52 protected internal abstract FilteredTermEnum GetEnum(IndexReader reader);
54 public override Query Rewrite(IndexReader reader)
56 FilteredTermEnum enumerator = GetEnum(reader);
57 BooleanQuery query = new BooleanQuery(true);
58 try
60 do
62 Term t = enumerator.Term();
63 if (t != null)
65 TermQuery tq = new TermQuery(t); // found a match
66 tq.SetBoost(GetBoost() * enumerator.Difference()); // set the boost
67 query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
70 while (enumerator.Next());
72 finally
74 enumerator.Close();
76 return query;
79 public override Query Combine(Query[] queries)
81 return Query.MergeBooleanQueries(queries);
85 /// <summary>Prints a user-readable version of this query. </summary>
86 public override System.String ToString(System.String field)
88 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
89 if (!term.Field().Equals(field))
91 buffer.Append(term.Field());
92 buffer.Append(":");
94 buffer.Append(term.Text());
95 if (GetBoost() != 1.0f)
97 System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
98 nfi.NumberDecimalDigits = 1;
100 buffer.Append("^");
101 buffer.Append(GetBoost().ToString("N", nfi));
103 return buffer.ToString();
106 public override bool Equals(System.Object o)
108 if (this == o)
109 return true;
110 if (!(o is MultiTermQuery))
111 return false;
113 MultiTermQuery multiTermQuery = (MultiTermQuery) o;
115 if (!term.Equals(multiTermQuery.term))
116 return false;
118 return true;
121 public override int GetHashCode()
123 return term.GetHashCode();