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.
18 using Document
= Lucene
.Net
.Documents
.Document
;
19 using Term
= Lucene
.Net
.Index
.Term
;
21 namespace Lucene
.Net
.Search
24 /// <summary>An abstract base class for search implementations.
25 /// Implements the main search methods.
27 /// <p>Note that you can only access Hits from a Searcher as long as it is
28 /// not yet closed, otherwise an IOException will be thrown.
30 public abstract class Searcher
: Lucene
.Net
.Search
.Searchable
36 private void InitBlock()
38 similarity
= Similarity
.GetDefault();
41 /// <summary>Returns the documents matching <code>query</code>. </summary>
42 /// <throws> BooleanQuery.TooManyClauses </throws>
43 public Hits
Search(Query query
)
45 return Search(query
, (Filter
) null);
48 /// <summary>Returns the documents matching <code>query</code> and
49 /// <code>filter</code>.
51 /// <throws> BooleanQuery.TooManyClauses </throws>
52 public virtual Hits
Search(Query query
, Filter filter
)
54 return new Hits(this, query
, filter
);
57 /// <summary>Returns documents matching <code>query</code> sorted by
58 /// <code>sort</code>.
60 /// <throws> BooleanQuery.TooManyClauses </throws>
61 public virtual Hits
Search(Query query
, Sort sort
)
63 return new Hits(this, query
, null, sort
);
66 /// <summary>Returns documents matching <code>query</code> and <code>filter</code>,
67 /// sorted by <code>sort</code>.
69 /// <throws> BooleanQuery.TooManyClauses </throws>
70 public virtual Hits
Search(Query query
, Filter filter
, Sort sort
)
72 return new Hits(this, query
, filter
, sort
);
75 /// <summary>Expert: Low-level search implementation with arbitrary sorting. Finds
76 /// the top <code>n</code> hits for <code>query</code>, applying
77 /// <code>filter</code> if non-null, and sorting the hits by the criteria in
78 /// <code>sort</code>.
80 /// <p>Applications should usually call {@link
81 /// Searcher#Search(Query,Filter,Sort)} instead.
83 /// <throws> BooleanQuery.TooManyClauses </throws>
84 public virtual TopFieldDocs
Search(Query query
, Filter filter
, int n
, Sort sort
)
86 return Search(CreateWeight(query
), filter
, n
, sort
);
89 /// <summary>Lower-level search API.
91 /// <p>{@link HitCollector#Collect(int,float)} is called for every non-zero
94 /// <p>Applications should only use this if they need <i>all</i> of the
95 /// matching documents. The high-level search API ({@link
96 /// Searcher#Search(Query)}) is usually more efficient, as it skips
97 /// non-high-scoring hits.
98 /// <p>Note: The <code>score</code> passed to this method is a raw score.
99 /// In other words, the score will not necessarily be a float whose value is
102 /// <throws> BooleanQuery.TooManyClauses </throws>
103 public virtual void Search(Query query
, HitCollector results
)
105 Search(query
, (Filter
) null, results
);
108 /// <summary>Lower-level search API.
110 /// <p>{@link HitCollector#Collect(int,float)} is called for every non-zero
111 /// scoring document.
112 /// <br>HitCollector-based access to remote indexes is discouraged.
114 /// <p>Applications should only use this if they need <i>all</i> of the
115 /// matching documents. The high-level search API ({@link
116 /// Searcher#Search(Query)}) is usually more efficient, as it skips
117 /// non-high-scoring hits.
120 /// <param name="query">to match documents
122 /// <param name="filter">if non-null, a bitset used to eliminate some documents
124 /// <param name="results">to receive hits
126 /// <throws> BooleanQuery.TooManyClauses </throws>
127 public virtual void Search(Query query
, Filter filter
, HitCollector results
)
129 Search(CreateWeight(query
), filter
, results
);
132 /// <summary>Expert: Low-level search implementation. Finds the top <code>n</code>
133 /// hits for <code>query</code>, applying <code>filter</code> if non-null.
135 /// <p>Called by {@link Hits}.
137 /// <p>Applications should usually call {@link Searcher#Search(Query)} or
138 /// {@link Searcher#Search(Query,Filter)} instead.
140 /// <throws> BooleanQuery.TooManyClauses </throws>
141 public virtual TopDocs
Search(Query query
, Filter filter
, int n
)
143 return Search(CreateWeight(query
), filter
, n
);
146 /// <summary>Returns an Explanation that describes how <code>doc</code> scored against
147 /// <code>query</code>.
149 /// <p>This is intended to be used in developing Similarity implementations,
150 /// and, for good performance, should not be displayed with every hit.
151 /// Computing an explanation is as expensive as executing the query over the
154 public virtual Explanation
Explain(Query query
, int doc
)
156 return Explain(CreateWeight(query
), doc
);
159 /// <summary>The Similarity implementation used by this searcher. </summary>
160 private Similarity similarity
;
162 /// <summary>Expert: Set the Similarity implementation used by this Searcher.
165 /// <seealso cref="Similarity.SetDefault(Similarity)">
167 public virtual void SetSimilarity(Similarity similarity
)
169 this.similarity
= similarity
;
172 /// <summary>Expert: Return the Similarity implementation used by this Searcher.
174 /// <p>This defaults to the current value of {@link Similarity#GetDefault()}.
176 public virtual Similarity
GetSimilarity()
178 return this.similarity
;
181 /// <summary> creates a weight for <code>query</code></summary>
182 /// <returns> new weight
184 protected internal virtual Weight
CreateWeight(Query query
)
186 return query
.Weight(this);
190 public virtual int[] DocFreqs(Term
[] terms
)
192 int[] result
= new int[terms
.Length
];
193 for (int i
= 0; i
< terms
.Length
; i
++)
195 result
[i
] = DocFreq(terms
[i
]);
200 /* The following abstract methods were added as a workaround for GCJ bug #15411.
201 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15411
203 abstract public void Search(Weight weight
, Filter filter
, HitCollector results
);
204 abstract public void Close();
205 abstract public int DocFreq(Term term
);
206 abstract public int MaxDoc();
207 abstract public TopDocs
Search(Weight weight
, Filter filter
, int n
);
208 abstract public Document
Doc(int i
);
209 abstract public Query
Rewrite(Query query
);
210 abstract public Explanation
Explain(Weight weight
, int doc
);
211 abstract public TopFieldDocs
Search(Weight weight
, Filter filter
, int n
, Sort sort
);
212 /* End patch for GCJ bug #15411. */