2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
28 /// <code>MultiTermQuery</code> is not designed to be used by itself.
30 /// The reason being that it is not intialized with a {@link FilteredTermEnum}
31 /// enumeration. A {@link FilteredTermEnum} enumeration needs to be provided.
33 /// For example, {@link WildcardQuery} and {@link FuzzyQuery} extend
34 /// <code>MultiTermQuery</code> to provide {@link WildcardTermEnum} and
35 /// {@link FuzzyTermEnum}, respectively.
38 public abstract class MultiTermQuery
: Query
42 /// <summary>Constructs a query for terms matching <code>term</code>. </summary>
43 public MultiTermQuery(Term term
)
48 /// <summary>Returns the pattern term. </summary>
49 public virtual Term
GetTerm()
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);
65 Term t
= enumerator
.Term();
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());
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());
91 buffer
.Append(term
.Text());
92 buffer
.Append(ToStringUtils
.Boost(GetBoost()));
93 return buffer
.ToString();
96 public override bool Equals(System
.Object o
)
100 if (!(o
is MultiTermQuery
))
103 MultiTermQuery multiTermQuery
= (MultiTermQuery
) o
;
105 if (!term
.Equals(multiTermQuery
.term
))
108 return GetBoost() == multiTermQuery
.GetBoost();
111 public override int GetHashCode()
113 return term
.GetHashCode();