Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
46  [Serializable]
47  public abstract class Query : System.ICloneable
48  {
49  private float boost = 1.0f; // query boost factor
50 
55  public virtual float Boost
56  {
57  get { return boost; }
58  set { boost = value; }
59  }
60 
75  public abstract System.String ToString(System.String field);
76 
78  public override System.String ToString()
79  {
80  return ToString("");
81  }
82 
88  public virtual Weight CreateWeight(Searcher searcher)
89  {
90  throw new System.NotSupportedException();
91  }
92 
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 
111  public virtual Query Rewrite(IndexReader reader)
112  {
113  return this;
114  }
115 
116 
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 
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 
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 
217  public virtual Similarity GetSimilarity(Searcher searcher)
218  {
219  return searcher.Similarity;
220  }
221 
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 }