Random Sorting Results in Lucene.Net 2.4

2019-06-10 21:40发布

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条回答
We Are One
2楼-- · 2019-06-10 22:20

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).

查看更多
登录 后发表回答