#include "LCSearcher.h"
#include "LCSimilarity.h"
#include "LCHits.h"
#include "LCQuery.h"
#include "LCSort.h"
#include "LCFilter.h"
#include "LCHitCollector.h"
#include "GNUstep.h"
#include "LCWeight.h"
/** An abstract base class for search implementations.
* Implements some common utility methods.
*/
@implementation LCSearcher
- (id) init
{
self = [super init];
ASSIGN(similarity, [LCSimilarity defaultSimilarity]);
return self;
}
- (void) dealloc
{
DESTROY(similarity);
[super dealloc];
}
- (LCHits *) search: (LCQuery *) query
{
return [self search: query filter: nil];
}
- (LCHits *) search: (LCQuery *) query
filter: (LCFilter *) filter
{
return AUTORELEASE([[LCHits alloc] initWithSearcher: self query: query filter: filter]);
}
- (LCHits *) search: (LCQuery *) query sort: (LCSort *) sort
{
return AUTORELEASE([[LCHits alloc] initWithSearcher: self query: query filter: nil sort: sort]);
}
- (LCHits *) search: (LCQuery *) query
filter: (LCFilter *) filter sort: (LCSort *) sort
{
return AUTORELEASE([[LCHits alloc] initWithSearcher: self query: query filter: filter sort: sort]);
}
/** Expert: Low-level 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
.
*
*
Applications should usually call {@link * Searcher#search(Query,Filter,Sort)} instead. * @throws BooleanQuery.TooManyClauses */ - (LCTopFieldDocs *) searchQuery: (LCQuery *) query filter: (LCFilter *) filter maximum: (int) n sort: (LCSort *) sort { return [self search: [self createWeight: query] filter: filter maximum: n sort: sort]; } /** Lower-level search API. * *
{@link HitCollector#collect(int,float)} is called for every non-zero * scoring 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.
* @throws BooleanQuery.TooManyClauses
*/
- (void) search: (LCQuery *) query
hitCollector: (LCHitCollector *) results
{
[self searchQuery: query filter: nil hitCollector: results];
}
/** Lower-level search API.
*
*
{@link HitCollector#collect(int,float)} is called for every non-zero
* scoring 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)}) is usually more efficient, as it skips
* non-high-scoring hits.
*
* @param query to match documents
* @param filter if non-null, a bitset used to eliminate some documents
* @param results to receive hits
* @throws BooleanQuery.TooManyClauses
*/
- (void) searchQuery: (LCQuery *) query filter: (LCFilter *) filter hitCollector: (LCHitCollector *) results
{
[self search: [self createWeight: query] filter: filter hitCollector: results];
}
/** Expert: Low-level search implementation. Finds the top n
* hits for query
, applying filter
if non-null.
*
*
Called by {@link Hits}. * *
Applications should usually call {@link Searcher#search(Query)} or * {@link Searcher#search(Query,Filter)} instead. * @throws BooleanQuery.TooManyClauses */ - (LCTopDocs *) searchQuery: (LCQuery *) query filter: (LCFilter *) filter maximum: (int) n { return [self search: [self createWeight: query] filter: filter maximum: n]; } /** Expert: Set the Similarity implementation used by this Searcher. * * @see Similarity#setDefault(Similarity) */ - (void) setSimilarity: (LCSimilarity *) s { ASSIGN(similarity, s); } /** Expert: Return the Similarity implementation used by this Searcher. * *
This defaults to the current value of {@link Similarity#getDefault()}.
*/
- (LCSimilarity *) similarity
{
return similarity;
}
/** 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.
*/
- (LCExplanation *) explainQuery: (LCQuery *) query document: (int) doc
{
return [self explain: [self createWeight: query] document: doc];
}
- (NSArray *) documentFrequencyWithTerms: (NSArray *) terms
{
NSMutableArray *result = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < [terms count]; i++)
{
[result addObject: [NSNumber numberWithInt: [self documentFrequencyWithTerm: [terms objectAtIndex: i]]]];
}
return AUTORELEASE(result);
}
- (id