Lucene.NET-按int排序 [英] Lucene.NET - sorting by int
问题描述
在最新版本的Lucene(或Lucene.NET)中,什么是按排序顺序返回搜索结果的正确方法?
In the latest version of Lucene (or Lucene.NET), what is the proper way to get the search results back in sorted order?
我有一个这样的文件:
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.
如何进行按CreationDate排序结果的搜索?我看到的所有文档都是针对使用现已弃用的API的旧Lucene版本的.
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.
推荐答案
在进行了一些研究并仔细研究了API之后,我终于找到了一些不赞成使用的API(从v2.9和v3.0开始)将允许您按日期订购:
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();
}
请注意,我正在使用LONG比较对创建日期进行排序.这是因为我将创建日期存储为DateTime.Now.Ticks,它是System.Int64或C#中的long.
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#.
这篇关于Lucene.NET-按int排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!