Random Sorting Results in Lucene.Net 2.4

心不动则不痛 提交于 2019-12-23 06:08:32

问题


How do I sort my results in a random order. my code looks something like this at the moment:

Dim searcher As IndexSearcher = New IndexSearcher(dir, True)
Dim collector As TopScoreDocCollector = TopScoreDocCollector.create(100, True)
searcher.Search(query, collector)
Dim hits() As ScoreDoc = collector.TopDocs.scoreDocs

For Each sDoc As ScoreDoc In hits
    'get doc and return
Next

回答1:


Since this is an IEnumerable, you can use standard linq to randomize it. You can find an example here:

public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
{
   Random rnd = new Random();
   return source.OrderBy<T, int>((item) => rnd.Next());
}

If you want to do this inside of Lucene itself, you can make your own sorter (although note that you will no longer be randomizing the top 100 results, but rather randomizing all results).



来源:https://stackoverflow.com/questions/3990760/random-sorting-results-in-lucene-net-2-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!