{
throw new IllegalArgumentException("both offset and count must be > 0: " + offset + "/" + count);
}
final Collector collector = senseiRequest.buildCollector(req.getQuery());
BrowseResult res = null;
BrowseHit[] hits = null;
ScoreDoc[] scoreDocs = null;
float [][] features = null;
int totalHits;
if (collector == null) {
if (senseiRequest.isSimpleRelevance())
{
browser.setSimilarity(new SimpleTFSimilarity());
}
res = browser.browse(req);
hits = res.getHits();
}
else {
Map<String, FacetAccessible> facetCollectors = new HashMap<String, FacetAccessible>();
Weight w = req.getQuery().createWeight(browser);
browser.browse(req, w, collector, facetCollectors, offset);
try {
/**
* A custom collector must implement topDocs() method and a getHitsFeatures() method.
*/
scoreDocs = (ScoreDoc[]) collector.getClass().getMethod(TOP_DOCS_METHOD).invoke(collector);
features = (float[][]) collector.getClass().getMethod(GET_HITS_FEATURES_METHOD).invoke(collector);
hits = new BrowseHit[scoreDocs.length];
Map<String, FacetHandler<?>> facetHandlerMap = browser.getFacetHandlerMap();
int i = 0;
for (ScoreDoc doc : scoreDocs) {
BrowseHit hit = new BrowseHit();
hit.setScore(doc.score);
hit.setDocid(doc.doc);
Map<String,String[]> map = new HashMap<String,String[]>();
Map<String,Object[]> rawMap = new HashMap<String,Object[]>();
for (Map.Entry<String, FacetHandler<?>> entry : facetHandlerMap.entrySet()) {
map.put(entry.getKey(), browser.getFieldVal(doc.doc, entry.getKey()));
rawMap.put(entry.getKey(), browser.getRawFieldVal(doc.doc, entry.getKey()));
}
hit.setFieldValues(map);
hit.setRawFieldValues(rawMap);
hits[i++] = hit;
}
res = new BrowseResult();
res.setHits(hits);
res.addAll(facetCollectors);
res.setTid(req.getTid());
totalHits = (Integer) collector.getClass().getMethod(GET_TOTAL_HITS_METHOD).invoke(collector);
res.setNumHits(totalHits);
res.setNumGroups(totalHits);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
if (req.getMapReduceWrapper() != null) {
result.setMapReduceResult(req.getMapReduceWrapper().getResult());
}
SenseiHit[] senseiHits = new SenseiHit[hits.length];
Set<String> selectSet = senseiRequest.getSelectSet();
for (int i = 0; i < hits.length; i++)
{
BrowseHit hit = hits[i];
SenseiHit senseiHit = new SenseiHit();
int docid = hit.getDocid();
SubReaderInfo<BoboIndexReader> readerInfo = subReaderAccessor.getSubReaderInfo(docid);
Long uid = (Long)hit.getRawField(PARAM_RESULT_HIT_UID);
if (uid == null)
uid = ((ZoieIndexReader<BoboIndexReader>) readerInfo.subreader.getInnerReader()).getUID(readerInfo.subdocid);
senseiHit.setUID(uid);
senseiHit.setDocid(docid);
senseiHit.setScore(hit.getScore());
senseiHit.setComparable(hit.getComparable());
if (selectSet != null && selectSet.size() != 0)
{
// Clear the data those are not used:
if (hit.getFieldValues() != null)
{
Iterator<String> iter = hit.getFieldValues().keySet().iterator();
while (iter.hasNext())
{
if (!selectSet.contains(iter.next()))
{
iter.remove();
}
}
}
if (hit.getRawFieldValues() != null)
{
Iterator<String> iter = hit.getRawFieldValues().keySet().iterator();
while (iter.hasNext())
{
if (!selectSet.contains(iter.next()))
{
iter.remove();
}
}
}
}
if (features != null) {
senseiHit.setFeatures(features[i]);
}
senseiHit.setFieldValues(hit.getFieldValues());
senseiHit.setRawFieldValues(hit.getRawFieldValues());
senseiHit.setStoredFields(hit.getStoredFields());
senseiHit.setExplanation(hit.getExplanation());
senseiHit.setGroupField(hit.getGroupField());
senseiHit.setGroupValue(hit.getGroupValue());
senseiHit.setRawGroupValue(hit.getRawGroupValue());
senseiHit.setGroupHitsCount(hit.getGroupHitsCount());
senseiHit.setTermFreqMap(hit.getTermFreqMap());
senseiHits[i] = senseiHit;
}
result.setHits(senseiHits);
result.setNumHits(res.getNumHits());
result.setNumGroups(res.getNumGroups());
result.setGroupAccessibles(res.getGroupAccessibles());
result.setSortCollector(res.getSortCollector());
result.setTotalDocs(browser.numDocs());
result.addAll(res.getFacetMap());
// Defer the closing of facetAccessibles till result merging time.
// Collection<FacetAccessible> facetAccessibles = facetMap.values();
// for (FacetAccessible facetAccessible : facetAccessibles){