用Lucene搜索树

时间:2013-02-13 11:55:37

标签: lucene tree faceted-search drilldown

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

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

然而,当我到达第三级时,我得到null。发生了什么事?

感谢。

1 个答案:

答案 0 :(得分:1)

您还必须设置:

facetRequest.setDepth(MAX_DEPTH_TREE);