Request rq = (Request)srq;
PostFilter[] filters = getPostFilters(rq);
final int filters_length = filters.length;
//the results to filter
ResultSet results = rq.getResultSet();
//the size of the results - this could be more than what we need to display
int ResultsSize = results.getResultSize();
//load in the lower and upper bounds of the resultset
String tmp = rq.getControl("start");/* 0 based */
if (tmp.length() == 0)
tmp = "0";
int Start = Integer.parseInt(tmp);
tmp = rq.getControl("end");
if (tmp.length() == 0)
tmp = "0";
int End = Integer.parseInt(tmp);/* 0 based */
if (End == 0)
{
End = ResultsSize -1;
}
int length = End-Start+1;
if (length > ResultsSize)
length = ResultsSize-Start;
//we've got no filters set, so just give the results ASAP
if (filters_length == 0)
{
rq.setResultSet( results.getResultSet(Start, length) );
if (logger.isDebugEnabled()) {
logger.debug("No filters, just Crop: "+Start+", length"+length);
logger.debug("Resultset is now "+results.getScores().length + " long");
}
return;
}
//tell all the postfilters that they are processing another query
for(int i=0;i<filters_length; i++)
{
filters[i].new_query(this, srq, results);
}
int doccount = -1;//doccount is zero-based, so 0 means 1 document
TIntArrayList docatnumbers = new TIntArrayList();//list of resultset index numbers to keep
byte docstatus; int thisDocId;
int[] docids = results.getDocids();
//int elitesetsize = results.getExactResultSize();
//the exact result size is the total number of
//documents that would be retrieved if we
//didn't do any cropping
int elitesetsize = results.getResultSize();
for(int thisdoc = 0; thisdoc < elitesetsize; thisdoc++)
{
//run this document through all the filters
docstatus = PostFilter.FILTER_OK;
thisDocId = docids[thisdoc];
//run this doc through the filters
for(int i=0;i<filters_length; i++)
{
if ( ( docstatus = filters[i].filter(this, srq, results, thisdoc, thisDocId) )
== PostFilter.FILTER_REMOVE
)
break;
//break if the document has to be removed
}
//if it's not being removed, then
if (docstatus != PostFilter.FILTER_REMOVE) //TODO this should always be true
{
//success, another real document
doccount++;
//check if it's in our results "WINDOW"
if (doccount >= Start)
{
if (doccount <= End)
{ //add to the list of documents to keep
docatnumbers.add(thisdoc);
//System.out.println("Keeping @"+thisdoc);
}
else
{
//we've now got enough results, break
break;
}
}
}
else
{
//System.out.println("Removed");
}
}
//since doccount is zero-based, we add one so that it
//corresponds to the real number of documents.
doccount++;
rq.setNumberOfDocumentsAfterFiltering(doccount);
if (docatnumbers.size() < docids.length)
{
//result set is definently shorter, replace with new one
rq.setResultSet( results.getResultSet(docatnumbers.toNativeArray()));
rq.getResultSet().setExactResultSize(results.getExactResultSize());
}
}
catch(Exception e){}
}