Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Query.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Linq;
20 using Lucene.Net.Index;
21 using IndexReader = Lucene.Net.Index.IndexReader;
22 
23 namespace Lucene.Net.Search
24 {
25 
26  /// <summary>The abstract base class for queries.
27  /// <p/>Instantiable subclasses are:
28  /// <list type="bullet">
29  /// <item> <see cref="TermQuery" /> </item>
30  /// <item> <see cref="MultiTermQuery" /> </item>
31  /// <item> <see cref="BooleanQuery" /> </item>
32  /// <item> <see cref="WildcardQuery" /> </item>
33  /// <item> <see cref="PhraseQuery" /> </item>
34  /// <item> <see cref="PrefixQuery" /> </item>
35  /// <item> <see cref="MultiPhraseQuery" /> </item>
36  /// <item> <see cref="FuzzyQuery" /> </item>
37  /// <item> <see cref="TermRangeQuery" /> </item>
38  /// <item> <see cref="NumericRangeQuery{T}" /> </item>
39  /// <item> <see cref="Lucene.Net.Search.Spans.SpanQuery" /> </item>
40  /// </list>
41  /// <p/>A parser for queries is contained in:
42  /// <list type="bullet">
43  /// <item><see cref="Lucene.Net.QueryParsers.QueryParser">QueryParser</see> </item>
44  /// </list>
45  /// </summary>
46  [Serializable]
47  public abstract class Query : System.ICloneable
48  {
49  private float boost = 1.0f; // query boost factor
50 
51  /// <summary>Gets or sets the boost for this query clause to <c>b</c>. Documents
52  /// matching this clause will (in addition to the normal weightings) have
53  /// their score multiplied by <c>b</c>. The boost is 1.0 by default.
54  /// </summary>
55  public virtual float Boost
56  {
57  get { return boost; }
58  set { boost = value; }
59  }
60 
61  /// <summary>Prints a query to a string, with <c>field</c> assumed to be the
62  /// default field and omitted.
63  /// <p/>The representation used is one that is supposed to be readable
64  /// by <see cref="Lucene.Net.QueryParsers.QueryParser">QueryParser</see>. However,
65  /// there are the following limitations:
66  /// <list type="bullet">
67  /// <item>If the query was created by the parser, the printed
68  /// representation may not be exactly what was parsed. For example,
69  /// characters that need to be escaped will be represented without
70  /// the required backslash.</item>
71  /// <item>Some of the more complicated queries (e.g. span queries)
72  /// don't have a representation that can be parsed by QueryParser.</item>
73  /// </list>
74  /// </summary>
75  public abstract System.String ToString(System.String field);
76 
77  /// <summary>Prints a query to a string. </summary>
78  public override System.String ToString()
79  {
80  return ToString("");
81  }
82 
83  /// <summary> Expert: Constructs an appropriate Weight implementation for this query.
84  ///
85  /// <p/>
86  /// Only implemented by primitive queries, which re-write to themselves.
87  /// </summary>
88  public virtual Weight CreateWeight(Searcher searcher)
89  {
90  throw new System.NotSupportedException();
91  }
92 
93  /// <summary> Expert: Constructs and initializes a Weight for a top-level query.</summary>
94  public virtual Weight Weight(Searcher searcher)
95  {
96  Query query = searcher.Rewrite(this);
97  Weight weight = query.CreateWeight(searcher);
98  float sum = weight.GetSumOfSquaredWeights();
99  float norm = GetSimilarity(searcher).QueryNorm(sum);
100  if (float.IsInfinity(norm) || float.IsNaN(norm))
101  norm = 1.0f;
102  weight.Normalize(norm);
103  return weight;
104  }
105 
106 
107  /// <summary>Expert: called to re-write queries into primitive queries. For example,
108  /// a PrefixQuery will be rewritten into a BooleanQuery that consists
109  /// of TermQuerys.
110  /// </summary>
111  public virtual Query Rewrite(IndexReader reader)
112  {
113  return this;
114  }
115 
116 
117  /// <summary>Expert: called when re-writing queries under MultiSearcher.
118  ///
119  /// Create a single query suitable for use by all subsearchers (in 1-1
120  /// correspondence with queries). This is an optimization of the OR of
121  /// all queries. We handle the common optimization cases of equal
122  /// queries and overlapping clauses of boolean OR queries (as generated
123  /// by MultiTermQuery.rewrite()).
124  /// Be careful overriding this method as queries[0] determines which
125  /// method will be called and is not necessarily of the same type as
126  /// the other queries.
127  /// </summary>
128  public virtual Query Combine(Query[] queries)
129  {
130  var uniques = new System.Collections.Generic.HashSet<Query>();
131  for (int i = 0; i < queries.Length; i++)
132  {
133  Query query = queries[i];
134  BooleanClause[] clauses = null;
135  // check if we can split the query into clauses
136  bool splittable = (query is BooleanQuery);
137  if (splittable)
138  {
139  BooleanQuery bq = (BooleanQuery) query;
140  splittable = bq.IsCoordDisabled();
141  clauses = bq.GetClauses();
142  for (int j = 0; splittable && j < clauses.Length; j++)
143  {
144  splittable = (clauses[j].Occur == Occur.SHOULD);
145  }
146  }
147  if (splittable)
148  {
149  for (int j = 0; j < clauses.Length; j++)
150  {
151  uniques.Add(clauses[j].Query);
152  }
153  }
154  else
155  {
156  uniques.Add(query);
157  }
158  }
159  // optimization: if we have just one query, just return it
160  if (uniques.Count == 1)
161  {
162  return uniques.First();
163  }
164  BooleanQuery result = new BooleanQuery(true);
165  foreach (Query key in uniques)
166  {
167  result.Add(key, Occur.SHOULD);
168  }
169  return result;
170  }
171 
172 
173  /// <summary> Expert: adds all terms occuring in this query to the terms set. Only
174  /// works if this query is in its <see cref="Rewrite">rewritten</see> form.
175  ///
176  /// </summary>
177  /// <throws> UnsupportedOperationException if this query is not yet rewritten </throws>
178  public virtual void ExtractTerms(System.Collections.Generic.ISet<Term> terms)
179  {
180  // needs to be implemented by query subclasses
181  throw new System.NotSupportedException();
182  }
183 
184 
185 
186  /// <summary>Expert: merges the clauses of a set of BooleanQuery's into a single
187  /// BooleanQuery.
188  ///
189  /// <p/>A utility for use by <see cref="Combine(Query[])" /> implementations.
190  /// </summary>
191  public static Query MergeBooleanQueries(params BooleanQuery[] queries)
192  {
193  var allClauses = new System.Collections.Generic.HashSet<BooleanClause>();
194  foreach (BooleanQuery booleanQuery in queries)
195  {
196  foreach (BooleanClause clause in booleanQuery)
197  {
198  allClauses.Add(clause);
199  }
200  }
201 
202  bool coordDisabled = queries.Length == 0?false:queries[0].IsCoordDisabled();
203  BooleanQuery result = new BooleanQuery(coordDisabled);
204  foreach(BooleanClause clause in allClauses)
205  {
206  result.Add(clause);
207  }
208  return result;
209  }
210 
211 
212  /// <summary>Expert: Returns the Similarity implementation to be used for this query.
213  /// Subclasses may override this method to specify their own Similarity
214  /// implementation, perhaps one that delegates through that of the Searcher.
215  /// By default the Searcher's Similarity implementation is returned.
216  /// </summary>
217  public virtual Similarity GetSimilarity(Searcher searcher)
218  {
219  return searcher.Similarity;
220  }
221 
222  /// <summary>Returns a clone of this query. </summary>
223  public virtual System.Object Clone()
224  {
225  try
226  {
227  return base.MemberwiseClone();
228  }
229  catch (System.Exception e)
230  {
231  throw new System.SystemException("Clone not supported: " + e.Message);
232  }
233  }
234 
235  public override int GetHashCode()
236  {
237  int prime = 31;
238  int result = 1;
239  result = prime * result + BitConverter.ToInt32(BitConverter.GetBytes(boost), 0);
240  return result;
241  }
242 
243  public override bool Equals(System.Object obj)
244  {
245  if (this == obj)
246  return true;
247  if (obj == null)
248  return false;
249  if (GetType() != obj.GetType())
250  return false;
251  Query other = (Query) obj;
252  if (BitConverter.ToInt32(BitConverter.GetBytes(boost), 0) != BitConverter.ToInt32(BitConverter.GetBytes(other.boost), 0))
253  return false;
254  return true;
255  }
256  }
257 }