2013-02-13 92 views
2

我有一个描述树结构的分类索引。在执行查询时,我想获得多个类别的点击次数(不一定在树的同一级别)。例如,给定的路径以下列表:使用Lucene进行树搜索

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

我想获得的点击次数为这三个类别。

我一直在寻找一个解决方案,我知道可以做一个树请求,然后通过调用.getSubResults()(如在API中解释的)得到结果。然而,我还没有找到任何例子,我真的不知道如何实现它。到目前为止,我得到以下几点:

// 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(); 
    } 

但是,当我到达第三级时,我得到空。到底是怎么回事?

谢谢。

回答

1

你也必须设置:

facetRequest.setDepth(MAX_DEPTH_TREE); 
+0

更有趣的信息:http://shaierera.blogspot.com/2012/12/lucene-facets-under-hood.html – synack 2013-02-13 15:00:55