简体   繁体   English

如何使用 Lucene 的 DistinctValuesCollector?

[英]How to use Lucene's DistinctValuesCollector?

My objective is to collect distinct values of select fields to provided them as filter options for the frontend.我的目标是收集 select 字段的不同值,以将它们作为前端的过滤器选项提供。 DistinctValuesCollector seems to be the tool for this, however since I haven't found code sample and documentation except for the Javadocs I can't currently correctly construct this collector. DistinctValuesCollector似乎是用于此的工具,但是由于除了 Javadoc 之外我还没有找到代码示例和文档,所以我目前无法正确构造此收集器。 Can anyone provide an example?谁能举个例子?

This is my attempt which doesn't deliver the desired distinct values of the field PROJEKTSTATUS.name .这是我的尝试,它没有提供字段PROJEKTSTATUS.name所需的不同值。

val groupSelector = TermGroupSelector(PROJEKTSTATUS.name)
val searchGroup = SearchGroup<BytesRef>()
val valueSelector = TermGroupSelector(PROJEKTSTATUS.name)
val groups = mutableListOf(searchGroup)
val distinctValuesCollector = DistinctValuesCollector(groupSelector, groups, valueSelector)

That field is indexed as follows:该字段的索引如下:

document.add(TextField(PROJEKTSTATUS.name, aggregat.projektstatus, YES))
document.add(SortedDocValuesField(PROJEKTSTATUS.name, BytesRef(aggregat.projektstatus)))

Thanks to @andrewJames's hint to a test class I could figure it out:感谢@andrewJames 对测试 class 的提示,我可以弄清楚:

fun IndexSearcher.collectFilterOptions(query: Query, field: String, topNGroups: Int = 128, mapper: Function<String?, String?> = Function { it }): Set<String?> {
    val firstPassGroupingCollector = FirstPassGroupingCollector(TermGroupSelector(field), Sort(), topNGroups)
    search(query, firstPassGroupingCollector)
    val topGroups = firstPassGroupingCollector.getTopGroups(0)
    val groupSelector = firstPassGroupingCollector.groupSelector
    val distinctValuesCollector = DistinctValuesCollector(groupSelector, topGroups, groupSelector)
    search(query, distinctValuesCollector)
    return distinctValuesCollector.groups.map { mapper.apply(it.groupValue.utf8ToString()) }.toSet()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM