/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Document = Lucene.Net.Documents.Document;
using CorruptIndexException = Lucene.Net.Index.CorruptIndexException;
using Term = Lucene.Net.Index.Term;
namespace Lucene.Net.Search
{
/// An abstract base class for search implementations. Implements the main search
/// methods.
///
///
/// Note that you can only access hits from a Searcher as long as it is not yet
/// closed, otherwise an IOException will be thrown.
///
public abstract class Searcher : System.MarshalByRefObject, Searchable
{
public Searcher()
{
InitBlock();
}
private void InitBlock()
{
similarity = Similarity.GetDefault();
}
/// Returns the documents matching query
.
/// BooleanQuery.TooManyClauses
/// Hits will be removed in Lucene 3.0. Use
/// {@link #Search(Query, Filter, int)} instead.
///
[Obsolete("Hits will be removed in Lucene 3.0. Use Search(Query, Filter, int) instead")]
public Hits Search(Query query)
{
return Search(query, (Filter) null);
}
/// Returns the documents matching query
and
/// filter
.
///
/// BooleanQuery.TooManyClauses
/// Hits will be removed in Lucene 3.0. Use
/// {@link #Search(Query, Filter, int)} instead.
///
[Obsolete("Hits will be removed in Lucene 3.0. Use Search(Query, Filter, int) instead")]
public virtual Hits Search(Query query, Filter filter)
{
return new Hits(this, query, filter);
}
/// Returns documents matching query
sorted by
/// sort
.
///
/// BooleanQuery.TooManyClauses
/// Hits will be removed in Lucene 3.0. Use
/// {@link #Search(Query, Filter, int, Sort)} instead.
///
[Obsolete("Hits will be removed in Lucene 3.0. Use Search(Query, Filter, int, Sort) instead")]
public virtual Hits Search(Query query, Sort sort)
{
return new Hits(this, query, null, sort);
}
/// Returns documents matching query
and filter
,
/// sorted by sort
.
///
/// BooleanQuery.TooManyClauses
/// Hits will be removed in Lucene 3.0. Use
/// {@link #Search(Query, Filter, int, Sort)} instead.
///
[Obsolete("Hits will be removed in Lucene 3.0. Use Search(Query, Filter, int, Sort) instead")]
public virtual Hits Search(Query query, Filter filter, Sort sort)
{
return new Hits(this, query, filter, sort);
}
/// Search implementation with arbitrary sorting. Finds
/// the top n
hits for query
, applying
/// filter
if non-null, and sorting the hits by the criteria in
/// sort
.
///
/// NOTE: this does not compute scores by default; use
/// {@link IndexSearcher#setDefaultFieldSortScoring} to enable scoring.
///
///
/// BooleanQuery.TooManyClauses
public virtual TopFieldDocs Search(Query query, Filter filter, int n, Sort sort)
{
return Search(CreateWeight(query), filter, n, sort);
}
/// Lower-level search API.
///
/// {@link HitCollector#Collect(int,float)} is called for every matching
/// document.
///
/// Applications should only use this if they need all of the
/// matching documents. The high-level search API ({@link
/// Searcher#Search(Query)}) is usually more efficient, as it skips
/// non-high-scoring hits.
/// Note: The score
passed to this method is a raw score.
/// In other words, the score will not necessarily be a float whose value is
/// between 0 and 1.
///
/// BooleanQuery.TooManyClauses
/// use {@link #Search(Query, Collector)} instead.
///
[Obsolete("use Search(Query, Collector) instead.")]
public virtual void Search(Query query, HitCollector results)
{
Search(CreateWeight(query), null, new HitCollectorWrapper(results));
}
/// Lower-level search API.
///
/// {@link Collector#Collect(int)} is called for every matching document.
///
/// Applications should only use this if they need all of the matching
/// documents. The high-level search API ({@link Searcher#Search(Query, int)}
/// ) is usually more efficient, as it skips non-high-scoring hits.
/// Note: The score
passed to this method is a raw score.
/// In other words, the score will not necessarily be a float whose value is
/// between 0 and 1.
///
/// BooleanQuery.TooManyClauses
public virtual void Search(Query query, Collector results)
{
Search(CreateWeight(query), null, results);
}
/// Lower-level search API.
///
/// {@link HitCollector#Collect(int,float)} is called for every matching
/// document.
///
HitCollector-based access to remote indexes is discouraged.
///
/// Applications should only use this if they need all of the
/// matching documents. The high-level search API ({@link
/// Searcher#Search(Query, Filter, int)}) is usually more efficient, as it skips
/// non-high-scoring hits.
///
///
/// to match documents
///
/// if non-null, used to permit documents to be collected.
///
/// to receive hits
///
/// BooleanQuery.TooManyClauses
/// use {@link #Search(Query, Filter, Collector)} instead.
///
[Obsolete("use Search(Query, Filter, Collector) instead.")]
public virtual void Search(Query query, Filter filter, HitCollector results)
{
Search(CreateWeight(query), filter, new HitCollectorWrapper(results));
}
/// Lower-level search API.
///
/// {@link Collector#Collect(int)} is called for every matching
/// document.
///
Collector-based access to remote indexes is discouraged.
///
/// Applications should only use this if they need all of the
/// matching documents. The high-level search API ({@link
/// Searcher#Search(Query, Filter, int)}) is usually more efficient, as it skips
/// non-high-scoring hits.
///
///
/// to match documents
///
/// if non-null, used to permit documents to be collected.
///
/// to receive hits
///
/// BooleanQuery.TooManyClauses
public virtual void Search(Query query, Filter filter, Collector results)
{
Search(CreateWeight(query), filter, results);
}
/// Finds the top n
/// hits for query
, applying filter
if non-null.
///
///
/// BooleanQuery.TooManyClauses
public virtual TopDocs Search(Query query, Filter filter, int n)
{
return Search(CreateWeight(query), filter, n);
}
/// Finds the top n
/// hits for query
.
///
///
/// BooleanQuery.TooManyClauses
public virtual TopDocs Search(Query query, int n)
{
return Search(query, null, n);
}
/// Returns an Explanation that describes how doc
scored against
/// query
.
///
/// This is intended to be used in developing Similarity implementations,
/// and, for good performance, should not be displayed with every hit.
/// Computing an explanation is as expensive as executing the query over the
/// entire index.
///
public virtual Explanation Explain(Query query, int doc)
{
return Explain(CreateWeight(query), doc);
}
/// The Similarity implementation used by this searcher.
private Similarity similarity;
/// Expert: Set the Similarity implementation used by this Searcher.
///
///
///
///
public virtual void SetSimilarity(Similarity similarity)
{
this.similarity = similarity;
}
/// Expert: Return the Similarity implementation used by this Searcher.
///
/// This defaults to the current value of {@link Similarity#GetDefault()}.
///
public virtual Similarity GetSimilarity()
{
return this.similarity;
}
/// creates a weight for query
/// new weight
///
public /*protected internal*/ virtual Weight CreateWeight(Query query)
{
return query.Weight(this);
}
// inherit javadoc
public virtual int[] DocFreqs(Term[] terms)
{
int[] result = new int[terms.Length];
for (int i = 0; i < terms.Length; i++)
{
result[i] = DocFreq(terms[i]);
}
return result;
}
/* The following abstract methods were added as a workaround for GCJ bug #15411.
* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15411
*/
/// use {@link #Search(Weight, Filter, Collector)} instead.
///
[Obsolete("use Search(Weight, Filter, Collector) instead.")]
public virtual void Search(Weight weight, Filter filter, HitCollector results)
{
Search(weight, filter, new HitCollectorWrapper(results));
}
abstract public void Search(Weight weight, Filter filter, Collector results);
abstract public void Close();
abstract public int DocFreq(Term term);
abstract public int MaxDoc();
abstract public TopDocs Search(Weight weight, Filter filter, int n);
abstract public Document Doc(int i);
abstract public Query Rewrite(Query query);
abstract public Explanation Explain(Weight weight, int doc);
abstract public TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort);
/* End patch for GCJ bug #15411. */
public abstract Lucene.Net.Documents.Document Doc(int param1, Lucene.Net.Documents.FieldSelector param2);
}
}