Take the 2-minute tour ×

I have a taxonomy index that describes a tree structure. When performing a query I want to get the number of hits for multiple categories (not necessarily in the same level of the tree). For example, given the following list of paths:

[Root/Cat1, Root/Cat1/Cat12, Root/Cat3]

I want to obtain the number of hits for each of these three categories.

I've been looking for a solution and I know that is possible to make a tree request and then get the results by calling .getSubResults() (as it is explained in the API). However I haven't found any example and I don't really know how to implement it. So far I've got to the following:

    // Build query
    Query query = extendQuery(queryGenerator.generateQuery(resource));

    // Set the number of top results        
    TopScoreDocCollector tdc = TopScoreDocCollector.create(numTopDocuments, true);

    // Set a faceted search
    FacetSearchParams facetSearchParams = new FacetSearchParams();

    // Search at level of the category in interests     
    CountFacetRequest facetRequest = new CountFacetRequest(new CategoryPath("Top", '/'), numTopCategories);



    // To collect the number of hits per facet
    FacetsCollector facetsCollector = 
            new FacetsCollector(facetSearchParams, documentReader, taxonomyReader);
    try {
        // Collect the number of hits per facet
                .search(query, MultiCollector.wrap(tdc, facetsCollector));                          
        for (FacetResult res : facetsCollector.getFacetResults()){
            //this is the top lvl facet
              FacetResultNode toplvl = res.getFacetResultNode();
              System.out.println(toplvl.getLabel() + " (" + toplvl.getValue() + ")");
              for (FacetResultNode secondlvl : toplvl.getSubResults()) {
                  //second lvl facet categories
                  System.out.println("  " + secondlvl.getLabel().getComponent(1) 
                                + " (" + secondlvl.getValue() + ")");
                  for (FacetResultNode thirdlvl : secondlvl.getSubResults()) {
                      //second lvl facet categories
                      System.out.println("  " + thirdlvl.getLabel().getComponent(2) 
                                    + " (" + thirdlvl.getValue() + ")");
    } catch (IOException e) {
        // TODO Auto-generated catch block

However, when I get to the third level I get null. What is going on?


share|improve this question

1 Answer 1

You have to set also:

share|improve this answer
more interesting info: shaierera.blogspot.com/2012/12/lucene-facets-under-hood.html –  Kits89 Feb 13 '13 at 15:00

Your Answer


