Lucene.NET - 按 int 排序

发布于 2024-08-29 13:24:15 字数 431 浏览 2 评论 0原文

在最新版本的 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);

现在我想要进行搜索并按最新的顺序获取结果。

如何执行按 CreationDate 对结果进行排序的搜索? 我看到的所有文档均适用于使用现已弃用的 API 的旧 Lucene 版本。

In the latest version of Lucene (or Lucene.NET), what is the proper way to get the search results back in sorted order?

I have a document like this:

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

Now I want do a search and get my results back in order of most recent.

How can I do a search that orders results by CreationDate? All the documentation I see is for old Lucene versions that use now-deprecated APIs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-09-05 13:24:15

在做了一些研究并摸索 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();
}

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

After doing some research and poking around with the API, I've finally found some non-deprecated APIs (as of v2.9 and v3.0) that will allow you to order by date:

// 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();
}

Note I'm sorting the creation date with the LONG comparison. That's because I store the creation date as DateTime.Now.Ticks, which is a System.Int64, or long in C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文