2013-02-13 2 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(); 
    } 

그러나 세 번째 레벨에 도달하면 null이됩니다. 무슨 일 이니?

감사합니다.

답변

1

또한 설정해야합니다 :

facetRequest.setDepth(MAX_DEPTH_TREE); 
+0

더 재미있는 정보 : http://shaierera.blogspot.com/2012/12/lucene-facets-under-hood.html – synack