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
Searcher.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 Lucene.Net.Documents;
20 using Document = Lucene.Net.Documents.Document;
21 using CorruptIndexException = Lucene.Net.Index.CorruptIndexException;
22 using Term = Lucene.Net.Index.Term;
23 
24 namespace Lucene.Net.Search
25 {
26 
27  /// <summary> An abstract base class for search implementations. Implements the main search
28  /// methods.
29  ///
30  /// <p/>
31  /// Note that you can only access hits from a Searcher as long as it is not yet
32  /// closed, otherwise an IOException will be thrown.
33  /// </summary>
34  public abstract class Searcher : System.MarshalByRefObject, Searchable, System.IDisposable
35  {
36  protected Searcher()
37  {
38  InitBlock();
39  }
40  private void InitBlock()
41  {
42  similarity = Net.Search.Similarity.Default;
43  }
44 
45  /// <summary>Search implementation with arbitrary sorting. Finds
46  /// the top <c>n</c> hits for <c>query</c>, applying
47  /// <c>filter</c> if non-null, and sorting the hits by the criteria in
48  /// <c>sort</c>.
49  ///
50  /// <p/>NOTE: this does not compute scores by default; use
51  /// <see cref="IndexSearcher.SetDefaultFieldSortScoring(bool,bool)" /> to enable scoring.
52  ///
53  /// </summary>
54  /// <throws> BooleanQuery.TooManyClauses </throws>
55  public virtual TopFieldDocs Search(Query query, Filter filter, int n, Sort sort)
56  {
57  return Search(CreateWeight(query), filter, n, sort);
58  }
59 
60  /// <summary>Lower-level search API.
61  ///
62  /// <p/><see cref="Collector.Collect(int)" /> is called for every matching document.
63  ///
64  /// <p/>Applications should only use this if they need <i>all</i> of the matching
65  /// documents. The high-level search API (<see cref="Searcher.Search(Query, int)" />
66  /// ) is usually more efficient, as it skips non-high-scoring hits.
67  /// <p/>Note: The <c>score</c> passed to this method is a raw score.
68  /// In other words, the score will not necessarily be a float whose value is
69  /// between 0 and 1.
70  /// </summary>
71  /// <throws> BooleanQuery.TooManyClauses </throws>
72  public virtual void Search(Query query, Collector results)
73  {
74  Search(CreateWeight(query), null, results);
75  }
76 
77  /// <summary>Lower-level search API.
78  ///
79  /// <p/><see cref="Collector.Collect(int)" /> is called for every matching
80  /// document.
81  /// <br/>Collector-based access to remote indexes is discouraged.
82  ///
83  /// <p/>Applications should only use this if they need <i>all</i> of the
84  /// matching documents. The high-level search API (<see cref="Searcher.Search(Query, Filter, int)" />)
85  /// is usually more efficient, as it skips
86  /// non-high-scoring hits.
87  ///
88  /// </summary>
89  /// <param name="query">to match documents
90  /// </param>
91  /// <param name="filter">if non-null, used to permit documents to be collected.
92  /// </param>
93  /// <param name="results">to receive hits
94  /// </param>
95  /// <throws> BooleanQuery.TooManyClauses </throws>
96  public virtual void Search(Query query, Filter filter, Collector results)
97  {
98  Search(CreateWeight(query), filter, results);
99  }
100 
101  /// <summary>Finds the top <c>n</c>
102  /// hits for <c>query</c>, applying <c>filter</c> if non-null.
103  ///
104  /// </summary>
105  /// <throws> BooleanQuery.TooManyClauses </throws>
106  public virtual TopDocs Search(Query query, Filter filter, int n)
107  {
108  return Search(CreateWeight(query), filter, n);
109  }
110 
111  /// <summary>Finds the top <c>n</c>
112  /// hits for <c>query</c>.
113  ///
114  /// </summary>
115  /// <throws> BooleanQuery.TooManyClauses </throws>
116  public virtual TopDocs Search(Query query, int n)
117  {
118  return Search(query, null, n);
119  }
120 
121  /// <summary>Returns an Explanation that describes how <c>doc</c> scored against
122  /// <c>query</c>.
123  ///
124  /// <p/>This is intended to be used in developing Similarity implementations,
125  /// and, for good performance, should not be displayed with every hit.
126  /// Computing an explanation is as expensive as executing the query over the
127  /// entire index.
128  /// </summary>
129  public virtual Explanation Explain(Query query, int doc)
130  {
131  return Explain(CreateWeight(query), doc);
132  }
133 
134  /// <summary>The Similarity implementation used by this searcher. </summary>
135  private Similarity similarity;
136 
137  /// <summary>Expert: Gets or Sets the Similarity implementation used by this Searcher.
138  ///
139  /// </summary>
140  /// <seealso cref="Lucene.Net.Search.Similarity.Default">
141  /// </seealso>
142  public virtual Similarity Similarity
143  {
144  get { return this.similarity; }
145  set { this.similarity = value; }
146  }
147 
148  /// <summary> creates a weight for <c>query</c></summary>
149  /// <returns> new weight
150  /// </returns>
151  public /*protected internal*/ virtual Weight CreateWeight(Query query)
152  {
153  return query.Weight(this);
154  }
155 
156  // inherit javadoc
157  public virtual int[] DocFreqs(Term[] terms)
158  {
159  int[] result = new int[terms.Length];
160  for (int i = 0; i < terms.Length; i++)
161  {
162  result[i] = DocFreq(terms[i]);
163  }
164  return result;
165  }
166 
167  public abstract void Search(Weight weight, Filter filter, Collector results);
168 
169  [Obsolete("Use Dispose() instead")]
170  public void Close()
171  {
172  Dispose();
173  }
174 
175  public void Dispose()
176  {
177  Dispose(true);
178  }
179 
180  protected abstract void Dispose(bool disposing);
181 
182  public abstract int DocFreq(Term term);
183  public abstract int MaxDoc { get; }
184  public abstract TopDocs Search(Weight weight, Filter filter, int n);
185  public abstract Document Doc(int i);
186  public abstract Document Doc(int docid, FieldSelector fieldSelector);
187  public abstract Query Rewrite(Query query);
188  public abstract Explanation Explain(Weight weight, int doc);
189  public abstract TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort);
190  /* End patch for GCJ bug #15411. */
191  }
192 }