2013-02-13 89 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