Lucene.NET - 按int排序

Jud*_*ngo 9 sorting lucene lucene.net

在最新版本的Lucene(或Lucene.NET)中,以排序顺序恢复搜索结果的正确方法是什么?

我有一个这样的文件:

var document = new Lucene.Document();
document.AddField("Text", "foobar");
document.AddField("CreationDate", DateTime.Now.Ticks.ToString()); // store the date as an int

indexWriter.AddDocument(document);
Run Code Online (Sandbox Code Playgroud)

现在我想进行搜索并按照最近的顺序返回我的结果.

如何通过CreationDate进行搜索结果搜索?我看到的所有文档都是针对使用现已弃用的旧版本的Lucene版本.

Jud*_*ngo 11

在做了一些研究并探讨了API之后,我终于找到了一些不推荐使用的API(从v2.9和v3.0开始),它们允许你按日期订购:

// Find all docs whose .Text contains "hello", ordered by .CreationDate.
var query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Text", new StandardAnalyzer()).Parse("hello");
var indexDirectory = FSDirectory.Open(new DirectoryInfo("c:\\foo"));
var searcher = new IndexSearcher(indexDirectory, true);
try
{
   var sort = new Sort(new SortField("CreationDate", SortField.LONG));
   var filter =  new QueryWrapperFilter(query);
   var results = searcher.Search(query, , 1000, sort);
   foreach (var hit in results.scoreDocs)
   {
       Document document = searcher.Doc(hit.doc);
       Console.WriteLine("\tFound match: {0}", document.Get("Text"));
   }
}
finally
{
   searcher.Close();
}
Run Code Online (Sandbox Code Playgroud)

注意我正在使用LONG比较对创建日期进行排序.那是因为我将创建日期存储为DateTime.Now.Ticks,它是一个System.Int64,或者是C#中的long.

  • 我发现Lucene排序的第一个可理解的解决方案 (3认同)