cvsimport
[beagle.git] / beagled / Lucene.Net / Search / MultiTermQuery.cs
blob2e0b46905de879e880a855fdd0a66cb5455ab613
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.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Term = Lucene.Net.Index.Term;
20 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
22 namespace Lucene.Net.Search
25 /// <summary> A {@link Query} that matches documents containing a subset of terms provided
26 /// by a {@link FilteredTermEnum} enumeration.
27 /// <P>
28 /// <code>MultiTermQuery</code> is not designed to be used by itself.
29 /// <BR>
30 /// The reason being that it is not intialized with a {@link FilteredTermEnum}
31 /// enumeration. A {@link FilteredTermEnum} enumeration needs to be provided.
32 /// <P>
33 /// For example, {@link WildcardQuery} and {@link FuzzyQuery} extend
34 /// <code>MultiTermQuery</code> to provide {@link WildcardTermEnum} and
35 /// {@link FuzzyTermEnum}, respectively.
36 /// </summary>
37 [Serializable]
38 public abstract class MultiTermQuery : Query
40 private Term term;
42 /// <summary>Constructs a query for terms matching <code>term</code>. </summary>
43 public MultiTermQuery(Term term)
45 this.term = term;
48 /// <summary>Returns the pattern term. </summary>
49 public virtual Term GetTerm()
51 return term;
54 /// <summary>Construct the enumeration to be used, expanding the pattern term. </summary>
55 protected internal abstract FilteredTermEnum GetEnum(IndexReader reader);
57 public override Query Rewrite(IndexReader reader)
59 FilteredTermEnum enumerator = GetEnum(reader);
60 BooleanQuery query = new BooleanQuery(true);
61 try
63 do
65 Term t = enumerator.Term();
66 if (t != null)
68 TermQuery tq = new TermQuery(t); // found a match
69 tq.SetBoost(GetBoost() * enumerator.Difference()); // set the boost
70 query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
73 while (enumerator.Next());
75 finally
77 enumerator.Close();
79 return query;
82 /// <summary>Prints a user-readable version of this query. </summary>
83 public override System.String ToString(System.String field)
85 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
86 if (!term.Field().Equals(field))
88 buffer.Append(term.Field());
89 buffer.Append(":");
91 buffer.Append(term.Text());
92 buffer.Append(ToStringUtils.Boost(GetBoost()));
93 return buffer.ToString();
96 public override bool Equals(System.Object o)
98 if (this == o)
99 return true;
100 if (!(o is MultiTermQuery))
101 return false;
103 MultiTermQuery multiTermQuery = (MultiTermQuery) o;
105 if (!term.Equals(multiTermQuery.term))
106 return false;
108 return GetBoost() == multiTermQuery.GetBoost();
111 public override int GetHashCode()
113 return term.GetHashCode();