Tree search with Lucene

2019-07-17 21:45发布

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);

    facetRequest.setResultMode(ResultMode.PER_NODE_IN_TREE);

    facetSearchParams.addFacetRequest(facetRequest);                    

    // To collect the number of hits per facet
    FacetsCollector facetsCollector = 
            new FacetsCollector(facetSearchParams, documentReader, taxonomyReader);
    try {
        // Collect the number of hits per facet
        documentSearcher
                .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
        e.printStackTrace();
    }

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

Thanks.

1条回答
Summer. ? 凉城
2楼-- · 2019-07-17 21:59

You have to set also:

facetRequest.setDepth(MAX_DEPTH_TREE);
查看更多
登录 后发表回答