问题
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