让孩子们使用Lucene Taxonomy Reader

时间:2012-10-09 08:53:03

标签: java lucene

Lucene中,我使用TaxonomyReader来读取存储在磁盘中的分类法索引。对于给定的类别,我需要查找其子类的所有类别。但是,在Lucene's API中,我可以找到一种方法来检索父级,但不能找到子级。有一个名为getChildrenArrays()的方法可以返回ChildrenArrays个对象。如您所见,此类只有两种方法:

  • getYoungestChildArray
  • getOlderSiblingArray

我想使用这两种方法实现Enumerator。有人知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

我得到了以下内容:

private class EnumChildren implements Enumeration<Integer> {
    int category, current;
    int[] next, first;

    public EnumChildren(int category) {
        ChildrenArrays childrenArrays = tr.getChildrenArrays();
        first = childrenArrays.getYoungestChildArray(); 
        next = childrenArrays.getOlderSiblingArray();
        current = first[category];
    }

    public boolean hasChildren() {
        return (current != TaxonomyReader.INVALID_ORDINAL);
    }

    @Override
    public boolean hasMoreElements() {  
        current = next[current];
        return (current != TaxonomyReader.INVALID_ORDINAL);
    }

    @Override
    public Integer nextElement() {
        return current;
    }
}

Ans用作:

ordinal = tr.getOrdinal(new CategoryPath(nodeCategory.getPath(), '/')); 
        EnumChildren childrenEnumeration = new EnumChildren(ordinal);
        if (childrenEnumeration.hasChildren()) {
            do {
                int current = childrenEnumeration.nextElement();
                Category child = new Category(tr.getPath(current).toString());
                addChildren(child);
                nodeCategory.children.add(child);
            } while (childrenEnumeration.hasMoreElements());
        }