Lucene’s docFreq Got You Down? Replace It With a Custom Collector
A Counting Collector
When you do searches in Lucene you can give the searcher a Collector which feels a bit like the visitor pattern as the search calls your collector once for each document that matches your query.
Take Away
Doing searches with custom collectors is quite easy. You just give an instance of one to the search method and interrogate it afterwards for the information you require.
A Counting Collector
When you do searches in Lucene you can give the searcher a Collector which feels a bit like the visitor pattern as the search calls your collector once for each document that matches your query.
public
class
CounterCollector : Collector
{
public
int
Count {
get
;
private
set
; }
public
void
Reset()
{
Count = 0;
}
public
override
void
Collect(
int
docID)
{
Count = Count + 1;
}
public
override
void
SetScorer(Scorer scorer) { }
public
override
void
SetNextReader(IndexReader reader,
int
docBase) { }
public
override
bool
AcceptsDocsOutOfOrder()
{
return
true
;
}
}
How Do I Use One Of Those?
public
int
GetNumberOfDocumentsForTerm(Term term)
{
return
searchIndex(searcher =>
{
//replacing this
//return searcher.DocFreq(term);
//with this
var counterCollector =
new
CounterCollector();
searcher.Search(
new
TermQuery(term), counterCollector);
return
counterCollector.Count;
});
}
Doing searches with custom collectors is quite easy. You just give an instance of one to the search method and interrogate it afterwards for the information you require.
Please read full article from Lucene’s docFreq Got You Down? Replace It With a Custom Collector
No comments:
Post a Comment