use of org.apache.lucene.facet.taxonomy.TaxonomyReader.ChildrenIterator in project lucene-solr by apache.
the class PrintTaxonomyStats method printAllChildren.
private static void printAllChildren(PrintStream out, TaxonomyReader r, int ord, String indent, int depth) throws IOException {
ChildrenIterator it = r.getChildren(ord);
int child;
while ((child = it.next()) != TaxonomyReader.INVALID_ORDINAL) {
out.println(indent + "/" + r.getPath(child).components[depth]);
printAllChildren(out, r, child, indent + " ", depth + 1);
}
}
use of org.apache.lucene.facet.taxonomy.TaxonomyReader.ChildrenIterator in project lucene-solr by apache.
the class PrintTaxonomyStats method printStats.
/** Recursively prints stats for all ordinals. */
public static void printStats(TaxonomyReader r, PrintStream out, boolean printTree) throws IOException {
out.println(r.getSize() + " total categories.");
ChildrenIterator it = r.getChildren(TaxonomyReader.ROOT_ORDINAL);
int child;
while ((child = it.next()) != TaxonomyReader.INVALID_ORDINAL) {
ChildrenIterator chilrenIt = r.getChildren(child);
int numImmediateChildren = 0;
while (chilrenIt.next() != TaxonomyReader.INVALID_ORDINAL) {
numImmediateChildren++;
}
FacetLabel cp = r.getPath(child);
out.println("/" + cp.components[0] + ": " + numImmediateChildren + " immediate children; " + (1 + countAllChildren(r, child)) + " total categories");
if (printTree) {
printAllChildren(out, r, child, " ", 1);
}
}
}
use of org.apache.lucene.facet.taxonomy.TaxonomyReader.ChildrenIterator in project lucene-solr by apache.
the class TestDirectoryTaxonomyReader method testGetChildren.
@Test
public void testGetChildren() throws Exception {
Directory dir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
int numCategories = atLeast(10);
int numA = 0, numB = 0;
Random random = random();
// add the two categories for which we'll also add children (so asserts are simpler)
taxoWriter.addCategory(new FacetLabel("a"));
taxoWriter.addCategory(new FacetLabel("b"));
for (int i = 0; i < numCategories; i++) {
if (random.nextBoolean()) {
taxoWriter.addCategory(new FacetLabel("a", Integer.toString(i)));
++numA;
} else {
taxoWriter.addCategory(new FacetLabel("b", Integer.toString(i)));
++numB;
}
}
// add category with no children
taxoWriter.addCategory(new FacetLabel("c"));
taxoWriter.close();
DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);
// non existing category
ChildrenIterator it = taxoReader.getChildren(taxoReader.getOrdinal(new FacetLabel("invalid")));
assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
// a category with no children
it = taxoReader.getChildren(taxoReader.getOrdinal(new FacetLabel("c")));
assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
// arbitrary negative ordinal
it = taxoReader.getChildren(-2);
assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
// root's children
Set<String> roots = new HashSet<>(Arrays.asList("a", "b", "c"));
it = taxoReader.getChildren(TaxonomyReader.ROOT_ORDINAL);
while (!roots.isEmpty()) {
FacetLabel root = taxoReader.getPath(it.next());
assertEquals(1, root.length);
assertTrue(roots.remove(root.components[0]));
}
assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
for (int i = 0; i < 2; i++) {
FacetLabel cp = i == 0 ? new FacetLabel("a") : new FacetLabel("b");
int ordinal = taxoReader.getOrdinal(cp);
it = taxoReader.getChildren(ordinal);
int numChildren = 0;
int child;
while ((child = it.next()) != TaxonomyReader.INVALID_ORDINAL) {
FacetLabel path = taxoReader.getPath(child);
assertEquals(2, path.length);
assertEquals(path.components[0], i == 0 ? "a" : "b");
++numChildren;
}
int expected = i == 0 ? numA : numB;
assertEquals("invalid num children", expected, numChildren);
}
taxoReader.close();
dir.close();
}
use of org.apache.lucene.facet.taxonomy.TaxonomyReader.ChildrenIterator in project lucene-solr by apache.
the class PrintTaxonomyStats method countAllChildren.
private static int countAllChildren(TaxonomyReader r, int ord) throws IOException {
int count = 0;
ChildrenIterator it = r.getChildren(ord);
int child;
while ((child = it.next()) != TaxonomyReader.INVALID_ORDINAL) {
count += 1 + countAllChildren(r, child);
}
return count;
}
Aggregations