【问题标题】:Lucene : Changing the default facet delimiter?Lucene:更改默认分面分隔符?
【发布时间】:2012-01-13 16:27:53
【问题描述】:

在这个精彩网站上的第一篇文章!

我的目标是使用分层构面来搜索使用 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 工具查看索引,并且显示方面由索引中的“~”字符分隔。

执行此操作的最佳途径是什么?非常感谢任何帮助!

【问题讨论】:

    标签: java groovy lucene facet


    【解决方案1】:

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

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

    我需要使用的是:

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

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

    容易被忽视,很难找到。

    希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多