// For computing range facet counts:
doc.add(new NumericDocValuesField("field", l));
// For drill down by numeric range:
doc.add(new LongField("field", l, Field.Store.NO));
CategoryPath cp;
if ((l&3) == 0) {
cp = new CategoryPath("dim", "a");
} else {
cp = new CategoryPath("dim", "b");
}
ff.addFields(doc, Collections.singletonList(cp));
w.addDocument(doc);
}
final IndexReader r = w.getReader();
w.close();
final TaxonomyReader tr = new DirectoryTaxonomyReader(tw);
tw.close();
IndexSearcher s = newSearcher(r);
final CountFacetRequest countRequest = new CountFacetRequest(new CategoryPath("dim"), 2);
final RangeFacetRequest<LongRange> rangeRequest = new RangeFacetRequest<LongRange>("field",
new LongRange("less than 10", 0L, true, 10L, false),
new LongRange("less than or equal to 10", 0L, true, 10L, true),
new LongRange("over 90", 90L, false, 100L, false),
new LongRange("90 or above", 90L, true, 100L, false),
new LongRange("over 1000", 1000L, false, Long.MAX_VALUE, false));
FacetSearchParams fsp = new FacetSearchParams(countRequest, rangeRequest);
final Set<String> dimSeen = new HashSet<String>();
DrillSideways ds = new DrillSideways(s, tr) {
@Override
protected FacetsAccumulator getDrillDownAccumulator(FacetSearchParams fsp) {
checkSeen(fsp);
return FacetsAccumulator.create(fsp, r, tr, null);
}
@Override
protected FacetsAccumulator getDrillSidewaysAccumulator(String dim, FacetSearchParams fsp) {
checkSeen(fsp);
return FacetsAccumulator.create(fsp, r, tr, null);
}
private void checkSeen(FacetSearchParams fsp) {
// Each dim should up only once, across
// both drillDown and drillSideways requests:
for(FacetRequest fr : fsp.facetRequests) {
String dim = fr.categoryPath.components[0];
assertFalse("dim " + dim + " already seen", dimSeen.contains(dim));
dimSeen.add(dim);
}
}
@Override
protected boolean scoreSubDocsAtOnce() {
return random().nextBoolean();
}
};
// First search, no drill downs:
DrillDownQuery ddq = new DrillDownQuery(FacetIndexingParams.DEFAULT, new MatchAllDocsQuery());
DrillSidewaysResult dsr = ds.search(null, ddq, 10, fsp);
assertEquals(100, dsr.hits.totalHits);
assertEquals(2, dsr.facetResults.size());
assertEquals("dim (0)\n b (75)\n a (25)\n", FacetTestUtils.toSimpleString(dsr.facetResults.get(0)));
assertEquals("field (0)\n less than 10 (10)\n less than or equal to 10 (11)\n over 90 (9)\n 90 or above (10)\n over 1000 (0)\n", FacetTestUtils.toSimpleString(dsr.facetResults.get(1)));
// Second search, drill down on dim=b:
ddq = new DrillDownQuery(FacetIndexingParams.DEFAULT, new MatchAllDocsQuery());
ddq.add(new CategoryPath("dim", "b"));
dimSeen.clear();
dsr = ds.search(null, ddq, 10, fsp);
assertEquals(75, dsr.hits.totalHits);
assertEquals(2, dsr.facetResults.size());