用Lucene进行树搜索 [英] Tree search with Lucene
问题描述
我有一个描述树结构的分类索引.执行查询时,我想获取多个类别的命中数(不一定在树的同一级别).例如,给定以下路径列表:
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.
我一直在寻找解决方案,我知道可以提出树请求,然后通过调用.getSubResults()
获得结果(如
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?
谢谢.
推荐答案
您还必须设置:
facetRequest.setDepth(MAX_DEPTH_TREE);
这篇关于用Lucene进行树搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!