Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / Query.cs
blob7dea7edf57f0a34b8bb4c807eb330d1937144711
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 namespace Lucene.Net.Search
21 /// <summary>The abstract base class for queries.
22 /// <p>Instantiable subclasses are:
23 /// <ul>
24 /// <li> {@link TermQuery}
25 /// <li> {@link MultiTermQuery}
26 /// <li> {@link BooleanQuery}
27 /// <li> {@link WildcardQuery}
28 /// <li> {@link PhraseQuery}
29 /// <li> {@link PrefixQuery}
30 /// <li> {@link PhrasePrefixQuery}
31 /// <li> {@link FuzzyQuery}
32 /// <li> {@link RangeQuery}
33 /// <li> {@link Lucene.Net.Search.Spans.SpanQuery}
34 /// </ul>
35 /// <p>A parser for queries is contained in:
36 /// <ul>
37 /// <li>{@link Lucene.Net.QueryParser.QueryParser QueryParser}
38 /// </ul>
39 /// </summary>
40 [Serializable]
41 public abstract class Query : System.ICloneable
43 private float boost = 1.0f; // query boost factor
45 /// <summary>Sets the boost for this query clause to <code>b</code>. Documents
46 /// matching this clause will (in addition to the normal weightings) have
47 /// their score multiplied by <code>b</code>.
48 /// </summary>
49 public virtual void SetBoost(float b)
51 boost = b;
54 /// <summary>Gets the boost for this clause. Documents matching
55 /// this clause will (in addition to the normal weightings) have their score
56 /// multiplied by <code>b</code>. The boost is 1.0 by default.
57 /// </summary>
58 public virtual float GetBoost()
60 return boost;
63 /// <summary>Prints a query to a string, with <code>Field</code> as the default Field
64 /// for terms. <p>The representation used is one that is readable by
65 /// {@link Lucene.Net.QueryParser.QueryParser QueryParser}
66 /// (although, if the query was created by the parser, the printed
67 /// representation may not be exactly what was parsed).
68 /// </summary>
69 public abstract System.String ToString(System.String field);
71 /// <summary>Prints a query to a string. </summary>
72 public override System.String ToString()
74 return ToString("");
77 /// <summary>Expert: Constructs an appropriate Weight implementation for this query.
78 ///
79 /// <p>Only implemented by primitive queries, which re-write to themselves.
80 /// </summary>
81 protected internal virtual Weight CreateWeight(Searcher searcher)
83 throw new System.NotSupportedException();
86 /// <summary>Expert: Constructs an initializes a Weight for a top-level query. </summary>
87 public virtual Weight Weight(Searcher searcher)
89 Query query = searcher.Rewrite(this);
90 Weight weight = query.CreateWeight(searcher);
91 float sum = weight.SumOfSquaredWeights();
92 float norm = GetSimilarity(searcher).QueryNorm(sum);
93 weight.Normalize(norm);
94 return weight;
97 /// <summary>Expert: called to re-write queries into primitive queries. </summary>
98 public virtual Query Rewrite(IndexReader reader)
100 return this;
103 /// <summary>Expert: called when re-writing queries under MultiSearcher.
104 ///
105 /// <p>Only implemented by derived queries, with no
106 /// {@link #CreateWeight(Searcher)} implementatation.
107 /// </summary>
108 public virtual Query Combine(Query[] queries)
110 throw new System.NotSupportedException();
114 /// <summary>Expert: merges the clauses of a set of BooleanQuery's into a single
115 /// BooleanQuery.
116 ///
117 /// <p>A utility for use by {@link #Combine(Query[])} implementations.
118 /// </summary>
119 public static Query MergeBooleanQueries(Query[] queries)
121 System.Collections.Hashtable allClauses = new System.Collections.Hashtable();
122 for (int i = 0; i < queries.Length; i++)
124 BooleanClause[] clauses = ((BooleanQuery) queries[i]).GetClauses();
125 for (int j = 0; j < clauses.Length; j++)
127 allClauses.Add(clauses[j], clauses[j]);
131 BooleanQuery result = new BooleanQuery();
132 foreach (BooleanClause booleanClause in allClauses.Keys)
134 result.Add(booleanClause);
136 return result;
139 /// <summary>Expert: Returns the Similarity implementation to be used for this query.
140 /// Subclasses may override this method to specify their own Similarity
141 /// implementation, perhaps one that delegates through that of the Searcher.
142 /// By default the Searcher's Similarity implementation is returned.
143 /// </summary>
144 public virtual Similarity GetSimilarity(Searcher searcher)
146 return searcher.GetSimilarity();
149 /// <summary>Returns a clone of this query. </summary>
150 public virtual System.Object Clone()
154 return (Query) this.MemberwiseClone();
156 catch (System.Exception e)
158 throw new System.SystemException("Clone not supported: " + e.Message);