Skip to content

Apache Lucene open-source search software

LUCENE-4703: add simple tool to print summary stats of the facet taxonomy index

Michael McCandlessa6e3cdb9b046
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index bebaf6fc37e..a5de8981f29 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -58,6 +58,9 @@ New Features
* LUCENE-4686: New specialized DGapVInt8IntEncoder for facets (now the
default). (Shai Erera)
+* LUCENE-4703: Add simple PrintTaxonomyStats tool to see summary
+ information about the facets taxonomy index. (Mike McCandless)
+
======================= Lucene 4.1.0 =======================
Changes in backwards compatibility policy
diff --git a/lucene/build.xml b/lucene/build.xml
index 75c6b788f50..7b666c6695c 100644
--- a/lucene/build.xml
+++ b/lucene/build.xml
@@ -205,6 +205,7 @@
<exclude name="queryparser/classes/java/org/apache/lucene/queryparser/classic/QueryParserTokenManager.class"/>
<exclude name="queryparser/classes/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParserTokenManager.class"/>
<exclude name="queryparser/classes/java/org/apache/lucene/queryparser/surround/parser/QueryParserTokenManager.class"/>
+ <exclude name="facet/classes/java/org/apache/lucene/facet/util/PrintTaxonomyStats.class"/>
</fileset>
</forbidden-apis>
</target>
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/util/PrintTaxonomyStats.java b/lucene/facet/src/java/org/apache/lucene/facet/util/PrintTaxonomyStats.java
new file mode 100644
index 00000000000..afd206e17f7
--- /dev/null
+++ b/lucene/facet/src/java/org/apache/lucene/facet/util/PrintTaxonomyStats.java
@@ -0,0 +1,98 @@
+package org.apache.lucene.facet.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+
+import org.apache.lucene.facet.taxonomy.CategoryPath;
+import org.apache.lucene.facet.taxonomy.TaxonomyReader;
+import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
+import org.apache.lucene.facet.taxonomy.directory.ParallelTaxonomyArrays;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+
+/** Prints how many ords are under each dimension. */
+
+public class PrintTaxonomyStats {
+
+ public static void main(String[] args) throws IOException {
+ boolean printTree = false;
+ String path = null;
+ for(int i=0;i<args.length;i++) {
+ if (args[i].equals("-printTree")) {
+ printTree = true;
+ } else {
+ path = args[i];
+ }
+ }
+ if (args.length != (printTree ? 2 : 1)) {
+ System.out.println("\nUsage: java -classpath ... org.apache.lucene.facet.util.PrintTaxonomyStats [-printTree] /path/to/taxononmy/index\n");
+ System.exit(1);
+ }
+ Directory dir = FSDirectory.open(new File(path));
+ TaxonomyReader r = new DirectoryTaxonomyReader(dir);
+ printStats(r, System.out, printTree);
+ r.close();
+ dir.close();
+ }
+
+ public static void printStats(TaxonomyReader r, PrintStream out, boolean printTree) throws IOException {
+ ParallelTaxonomyArrays arrays = r.getParallelTaxonomyArrays();
+ //int[] parents = arrays.parents();
+ int[] children = arrays.children();
+ int[] siblings = arrays.siblings();
+ out.println(r.getSize() + " total categories.");
+
+ int childOrd = children[TaxonomyReader.ROOT_ORDINAL];
+ while(childOrd != -1) {
+ CategoryPath cp = r.getPath(childOrd);
+ int childOrd2 = children[childOrd];
+ int numImmediateChildren = 0;
+ while(childOrd2 != -1) {
+ numImmediateChildren++;
+ childOrd2 = siblings[childOrd2];
+ }
+ out.println("/" + cp + ": " + numImmediateChildren + " immediate children; " + (1+countAllChildren(r, childOrd, children, siblings)) + " total categories");
+ if (printTree) {
+ printAllChildren(out, r, childOrd, children, siblings, " ", 1);
+ }
+ childOrd = siblings[childOrd];
+ }
+ }
+
+ private static int countAllChildren(TaxonomyReader r, int ord, int[] children, int[] siblings) throws IOException {
+ int childOrd = children[ord];
+ int count = 0;
+ while(childOrd != -1) {
+ count += 1+countAllChildren(r, childOrd, children, siblings);
+ childOrd = siblings[childOrd];
+ }
+ return count;
+ }
+
+ private static void printAllChildren(PrintStream out, TaxonomyReader r, int ord, int[] children, int[] siblings, String indent, int depth) throws IOException {
+ int childOrd = children[ord];
+ while(childOrd != -1) {
+ out.println(indent + "/" + r.getPath(childOrd).components[depth]);
+ printAllChildren(out, r, childOrd, children, siblings, indent + " ", depth+1);
+ childOrd = siblings[childOrd];
+ }
+ }
+}
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java
index 6df1e474242..0331c1607ee 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/search/TestDemoFacets.java
@@ -17,7 +17,9 @@ package org.apache.lucene.facet.search;
* limitations under the License.
*/
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
@@ -31,6 +33,7 @@ import org.apache.lucene.facet.taxonomy.CategoryPath;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
+import org.apache.lucene.facet.util.PrintTaxonomyStats;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.search.IndexSearcher;
@@ -116,6 +119,17 @@ public class TestDemoFacets extends LuceneTestCase {
assertEquals("Author (2)\n Lisa (1)\n Bob (1)\n",
FacetTestUtils.toSimpleString(results.get(0)));
+ // Smoke test PrintTaxonomyStats:
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ PrintTaxonomyStats.printStats(taxoReader, new PrintStream(bos, false, "UTF-8"), true);
+ String result = bos.toString("UTF-8");
+ assertTrue(result.indexOf("/Author: 4 immediate children; 5 total categories") != -1);
+ assertTrue(result.indexOf("/Publish Date: 3 immediate children; 12 total categories") != -1);
+ // Make sure at least a few nodes of the tree came out:
+ assertTrue(result.indexOf(" /1999") != -1);
+ assertTrue(result.indexOf(" /2012") != -1);
+ assertTrue(result.indexOf(" /20") != -1);
+
taxoReader.close();
searcher.getIndexReader().close();
dir.close();