如何使用排序功能搜索 lucene.net?

发布于 2024-10-28 18:18:10 字数 37 浏览 1 评论 0原文

我不确定如何将它用于排序。我想按日期时间降序对结果进行排序。

I am unsure that how I can use it with sorting. I want to sort the result by datetime descending.

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

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

发布评论

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

评论(1

殤城〤 2024-11-04 18:18:10

更新 (2013.04.22):较新版本的 Lucene.NET 允许更直接地按 DateTime 排序,例如:

var sort = new Sort( new SortField( Constants.LuceneCreationTime, SortField.LONG, true ) );
var filter = new QueryWrapperFilter( query );
var docs = searcher.Search( query, filter, 100, sort )

将 CreationTime 添加为 DateTime.Ticks 以允许通过 SortField.LONG 搜索它。

我已经用3.0版本测试过了。


是的,这个问题之前已经部分回答过。但是,我想在日期时间部分添加注释。

在常规 Lucene 社区中,通常建议拆分 DateTime,以使它们足够高性能和/或足够精确(Int32 只有 10 个字符)。半年前,我对多达 500,000 个文档进行了基准测试,我记得,这确实是我能想到的唯一高效方法。

无论如何,你可以尝试这样的事情:

索引

var indexWriter = OpenWritableIndex();
Document doc = new Document();

DateTime lastEdited = DateTime.Now;
int year = lastEdited.Year;
int month = lastEdited.Month;
int day = lastEdited.Day;
int hour = lastEdited.Hour;
int minute = lastEdited.Minute;

doc.Add(new Field("LastEditedYear", year.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedMonth", month.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedDay", day.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedHour", hour.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedMinute", minute.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));

indexWriter.AddDocument(doc);   

搜索

var readableIndex = OpenReadableIndex();
var searcher = new IndexSearcher(readableIndex.Directory);
var multiParser = new MultiFieldQueryParser(termsToSearchIn, readableIndex.Analyzer);

var query = multiParser.Parse(terms);
Hits hits = null;
Sort sort = new Sort(new SortField[]
{
    new SortField("LastEditedYear", true),
    new SortField("LastEditedMonth", true),
    new SortField("LastEditedDay", true),
    new SortField("LastEditedHour", true),
    new SortField("LastEditedMinute", true)
}); 
if(sort != null)
{
    try
    {
        hits = searcher.Search(query, sort);
    }
    catch(SystemException) // Lucene throws a SystemException when trying to sort an empty response.
    {
        return new List<string>();
    }
}

UPDATE (2013.04.22): Newer versions of Lucene.NET allow sorting by DateTime more directly, for example:

var sort = new Sort( new SortField( Constants.LuceneCreationTime, SortField.LONG, true ) );
var filter = new QueryWrapperFilter( query );
var docs = searcher.Search( query, filter, 100, sort )

Add the CreationTime as DateTime.Ticks to allow searching for it via SortField.LONG.

I have tested this with version 3.0.


yes, this question has partly been answered before. However, I would like to add a note on the DateTime part.

In regular Lucene communities it is offen recommended to split up DateTime's in order to make them performant and/or precise enough (Int32 is just 10 characters). I've benchmarked with up to 500.000 documents half a year ago, and as I remember, this really was the only performant way I could come up with.

Anyway, you can try out something like this:

Indexing

var indexWriter = OpenWritableIndex();
Document doc = new Document();

DateTime lastEdited = DateTime.Now;
int year = lastEdited.Year;
int month = lastEdited.Month;
int day = lastEdited.Day;
int hour = lastEdited.Hour;
int minute = lastEdited.Minute;

doc.Add(new Field("LastEditedYear", year.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedMonth", month.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedDay", day.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedHour", hour.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("LastEditedMinute", minute.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));

indexWriter.AddDocument(doc);   

Searching

var readableIndex = OpenReadableIndex();
var searcher = new IndexSearcher(readableIndex.Directory);
var multiParser = new MultiFieldQueryParser(termsToSearchIn, readableIndex.Analyzer);

var query = multiParser.Parse(terms);
Hits hits = null;
Sort sort = new Sort(new SortField[]
{
    new SortField("LastEditedYear", true),
    new SortField("LastEditedMonth", true),
    new SortField("LastEditedDay", true),
    new SortField("LastEditedHour", true),
    new SortField("LastEditedMinute", true)
}); 
if(sort != null)
{
    try
    {
        hits = searcher.Search(query, sort);
    }
    catch(SystemException) // Lucene throws a SystemException when trying to sort an empty response.
    {
        return new List<string>();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文