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.
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.
25 /// <code>MultiTermQuery</code> is not designed to be used by itself.
27 /// The reason being that it is not intialized with a {@link FilteredTermEnum}
28 /// enumeration. A {@link FilteredTermEnum} enumeration needs to be provided.
30 /// For example, {@link WildcardQuery} and {@link FuzzyQuery} extend
31 /// <code>MultiTermQuery</code> to provide {@link WildcardTermEnum} and
32 /// {@link FuzzyTermEnum}, respectively.
35 public abstract class MultiTermQuery
:Query
39 /// <summary>Constructs a query for terms matching <code>term</code>. </summary>
40 public MultiTermQuery(Term term
)
45 /// <summary>Returns the pattern term. </summary>
46 public virtual Term
GetTerm()
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);
62 Term t
= enumerator
.Term();
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());
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());
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;
101 buffer
.Append(GetBoost().ToString("N", nfi
));
103 return buffer
.ToString();
106 public override bool Equals(System
.Object o
)
110 if (!(o
is MultiTermQuery
))
113 MultiTermQuery multiTermQuery
= (MultiTermQuery
) o
;
115 if (!term
.Equals(multiTermQuery
.term
))
121 public override int GetHashCode()
123 return term
.GetHashCode();