add example of DrillSidways with range facets

This commit is contained in:
Mike McCandless 2017-02-18 05:56:22 -05:00
parent 19c8ec2bf1
commit 2b4a1646ee
2 changed files with 52 additions and 5 deletions

View File

@ -16,11 +16,15 @@
*/
package org.apache.lucene.demo.facet;
import java.io.Closeable;
import java.io.IOException;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.facet.DrillDownQuery;
import org.apache.lucene.facet.DrillSideways;
import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.facet.FacetsCollector;
@ -29,17 +33,14 @@ import org.apache.lucene.facet.range.LongRange;
import org.apache.lucene.facet.range.LongRangeFacetCounts;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import java.io.Closeable;
import java.io.IOException;
/** Shows simple usage of dynamic range faceting. */
public class RangeFacetsExample implements Closeable {
@ -109,6 +110,29 @@ public class RangeFacetsExample implements Closeable {
return searcher.search(q, 10);
}
public DrillSideways.DrillSidewaysResult drillSideways(LongRange range) throws IOException {
// Passing no baseQuery means we drill down on all
// documents ("browse only"):
DrillDownQuery q = new DrillDownQuery(getConfig());
q.add("timestamp", LongPoint.newRangeQuery("timestamp", range.min, range.max));
// DrillSideways only handles taxonomy and sorted set drill facets by default; to do range facets we must subclass and override the
// buildFacetsResult method.
DrillSideways.DrillSidewaysResult result = new DrillSideways(searcher, getConfig(), null, null) {
@Override
protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways, String[] drillSidewaysDims) throws IOException {
// If we had other dims we would also compute their drill-down or drill-sideways facets here:
assert drillSidewaysDims[0].equals("timestamp");
return new LongRangeFacetCounts("timestamp", drillSideways[0],
PAST_HOUR,
PAST_SIX_HOURS,
PAST_DAY);
}
}.search(q, 10);
return result;
}
@Override
public void close() throws IOException {
searcher.getIndexReader().close();
@ -130,6 +154,13 @@ public class RangeFacetsExample implements Closeable {
TopDocs hits = example.drillDown(example.PAST_SIX_HOURS);
System.out.println(hits.totalHits + " totalHits");
System.out.println("\n");
System.out.println("Facet drill-sideways example (timestamp/Past six hours):");
System.out.println("---------------------------------------------");
DrillSideways.DrillSidewaysResult sideways = example.drillSideways(example.PAST_SIX_HOURS);
System.out.println(sideways.hits.totalHits + " totalHits");
System.out.println(sideways.facets.getTopChildren(10, "timestamp"));
example.close();
}
}

View File

@ -80,4 +80,20 @@ abstract class RangeFacetCounts extends Facets {
public List<FacetResult> getAllDims(int topN) throws IOException {
return Collections.singletonList(getTopChildren(topN, null));
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("RangeFacetCounts totCount=");
b.append(totCount);
b.append(":\n");
for(int i=0;i<ranges.length;i++) {
b.append(" ");
b.append(ranges[i].label);
b.append(" -> count=");
b.append(counts[i]);
b.append('\n');
}
return b.toString();
}
}