1

第一次在这个美妙的网站上发帖!

我的目标是使用分层构面来搜索使用 Lucene 的索引。但是,我的构面需要用除“/”以外的字符(在本例中为“~”)来分隔。例子:

分类分类~分类1分类~分类2

我创建了一个实现 FacetIndexingParams 接口的类(DefaultFacetIndexingParams 的副本,其中 DEFAULT_FACET_DELIM_CHAR 参数设置为“~”)。

释义索引代码:(对索引和分类都使用 FSDirectory)

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_34)
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, analyzer)
IndexWriter writer = new IndexWriter(indexDir, config)
TaxonomyWriter taxo = new LuceneTaxonomyWriter(taxDir, OpenMode.CREATE)

Document doc = new Document()
// Add bunch of Fields... hidden for the sake of brevity
List<CategoryPath> categories = new ArrayList<CategoryPath>()
row.tags.split('\\|').each{ tag ->
    def cp = new CategoryPath()
    tag.split('~').each{
        cp.add(it)
    }
    categories.add(cp)
}
NewFacetIndexingParams facetIndexingParams = new NewFacetIndexingParams()
DocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo, facetIndexingParams)
categoryDocBuilder.setCategoryPaths(categories).build(doc)
writer.addDocument(doc)

// Commit and close both writer and taxo.

搜索代码解释:

// Create index and taxonomoy readers to get info from index and taxonomy
IndexReader indexReader = IndexReader.open(indexDir)
TaxonomyReader taxo = new LuceneTaxonomyReader(taxDir)
Searcher searcher = new IndexSearcher(indexReader)

QueryParser parser = new QueryParser(Version.LUCENE_34, "content", new StandardAnalyzer(Version.LUCENE_34))
parser.setAllowLeadingWildcard(true)
Query q = parser.parse(query)
TopScoreDocCollector tdc = TopScoreDocCollector.create(10, true)
List<FacetResult> res = null
NewFacetIndexingParams facetIndexingParams = new NewFacetIndexingParams()
FacetSearchParams facetSearchParams = new FacetSearchParams(facetIndexingParams)
CountFacetRequest cfr = new CountFacetRequest(new CategoryPath(""), 99)
cfr.setDepth(2)
cfr.setSortBy(SortBy.VALUE)
facetSearchParams.addFacetRequest(cfr)
FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxo)

def cp = new CategoryPath("Category~Category1", (char)'~')
searcher.search(DrillDown.query(q, cp), MultiCollector.wrap(tdc, facetsCollector))

结果总是以“Category/Category1”的形式返回一个构面列表。

我已经使用 Luke 工具查看索引,并且看起来各个方面由索引中的“~”字符分隔。

做到这一点的最佳途径是什么?任何帮助是极大的赞赏!

4

1 回答 1

3

我已经弄清楚了这个问题。搜索和索引按预期工作。这就是我如何获得方面结果的问题。我正在使用:

res = facetsCollector.getFacetResults()
res.each{ result ->
    result.getFacetResultNode().getLabel().toString()
}

我需要使用的是:

res = facetsCollector.getFacetResults()
res.each{ result ->
    result.getFacetResultNode().getLabel().toString((char)'~')
}

不同之处在于发送到 toString 函数的参数!

容易被忽视,很难找到。

希望这对其他人有帮助。

于 2012-01-17T14:59:52.737 回答