Apache Lucene open-source search software
LUCENE-5474: Add example for retrieving facet counts without retrieving documents
Shai Ererade9404321da3
diff --git a/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java b/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.javaindex 73575451e97..eec15d7a5e5 100644--- a/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java+++ b/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java@@ -91,7 +91,7 @@ public class SimpleFacetsExample {}/** User runs a query and counts facets. */- private List<FacetResult> search() throws IOException {+ private List<FacetResult> facetsWithSearch() throws IOException {DirectoryReader indexReader = DirectoryReader.open(indexDir);IndexSearcher searcher = new IndexSearcher(indexReader);TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);@@ -117,6 +117,34 @@ public class SimpleFacetsExample {return results;}+ /** User runs a query and counts facets only without collecting the matching documents.*/+ private List<FacetResult> facetsOnly() throws IOException {+ DirectoryReader indexReader = DirectoryReader.open(indexDir);+ IndexSearcher searcher = new IndexSearcher(indexReader);+ TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);++ FacetsCollector fc = new FacetsCollector();++ // MatchAllDocsQuery is for "browsing" (counts facets+ // for all non-deleted docs in the index); normally+ // you'd use a "normal" query:+ searcher.search(new MatchAllDocsQuery(), null /*Filter */, fc);++ // Retrieve results+ List<FacetResult> results = new ArrayList<FacetResult>();++ // Count both "Publish Date" and "Author" dimensions+ Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);++ results.add(facets.getTopChildren(10, "Author"));+ results.add(facets.getTopChildren(10, "Publish Date"));++ indexReader.close();+ taxoReader.close();++ return results;+ }+/** User drills down on 'Publish Date/2010'. */private FacetResult drillDown() throws IOException {DirectoryReader indexReader = DirectoryReader.open(indexDir);@@ -142,10 +170,16 @@ public class SimpleFacetsExample {return result;}+ /** Runs the search example. */+ public List<FacetResult> runFacetOnly() throws IOException {+ index();+ return facetsOnly();+ }+/** Runs the search example. */public List<FacetResult> runSearch() throws IOException {index();- return search();+ return facetsWithSearch();}/** Runs the drill-down example. */@@ -158,10 +192,18 @@ public class SimpleFacetsExample {public static void main(String[] args) throws Exception {System.out.println("Facet counting example:");System.out.println("-----------------------");+ SimpleFacetsExample example1 = new SimpleFacetsExample();+ List<FacetResult> results1 = example1.runFacetOnly();+ System.out.println("Author: " + results1.get(0));+ System.out.println("Publish Date: " + results1.get(1));++ System.out.println("Facet counting example (combined facets and search):");+ System.out.println("-----------------------");SimpleFacetsExample example = new SimpleFacetsExample();List<FacetResult> results = example.runSearch();System.out.println("Author: " + results.get(0));System.out.println("Publish Date: " + results.get(1));+System.out.println("\n");System.out.println("Facet drill-down example (Publish Date/2010):");@@ -169,4 +211,4 @@ public class SimpleFacetsExample {System.out.println("Author: " + example.runDrillDown());}-}+}\ No newline at end of filediff --git a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.javaindex bacbb4f4c61..71804d9a322 100644--- a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java+++ b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java@@ -26,6 +26,15 @@ import org.junit.Test;public class TestSimpleFacetsExample extends LuceneTestCase {+ @Test+ public void testFacetOnly() throws Exception {+ List<FacetResult> results = new SimpleFacetsExample().runFacetOnly();+ assertEquals(2, results.size());+ assertEquals("dim=Author path=[] value=5 childCount=4\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", results.get(0).toString());+ assertEquals("dim=Publish Date path=[] value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());+ }++@Testpublic void testSimple() throws Exception {List<FacetResult> results = new SimpleFacetsExample().runSearch();