在这个很棒的网站上发表第一篇文章!
我的目标是通过 Lucene 使用分层面来搜索索引。但是,我的构面需要由“/”以外的字符分隔(在本例中为“~”)。示例:
类别 分类~Category1 类别~Category2
我创建了一个实现 FacetIndexingParams 接口(interface)的类(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 工具查看了索引,它似乎在索引中由“~”字符分隔了各个方面。
执行此操作的最佳途径是什么?非常感谢任何帮助!
最佳答案
我已经解决了这个问题。搜索和索引正在按预期工作。这就是我如何获得侧面结果的方式。我正在使用:
res = facetsCollector.getFacetResults()
res.each{ result ->
result.getFacetResultNode().getLabel().toString()
}
我需要使用的是:
res = facetsCollector.getFacetResults()
res.each{ result ->
result.getFacetResultNode().getLabel().toString((char)'~')
}
区别在于发送到 toString 函数的参数!
容易被忽视,很难找到。
希望这对其他人有帮助。
关于java - 卢塞恩 : Changing the default facet delimiter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853821/