【问题标题】:Lucene.NET getting the most recently indexed documentLucene.NET 获取最近索引的文档
【发布时间】:2016-08-03 23:01:44
【问题描述】:

我的问题是找到获取最后(时间戳)索引的 Lucene 文档的最快方法。

文档中的字段如下所示:

        // Index file contents
        Field contentField = new Field(
            FieldContent, 
            message.content,
            Field.Store.YES, 
            Field.Index.ANALYZED, 
            Field.TermVector.YES);

        // The id of the document
        Field messageIdField = new Field(
            FieldMessageId,
            message.serverMessageId,
            Field.Store.YES,
            Field.Index.NOT_ANALYZED);

        // The dateTime that the document was created
        Field timeStampField = new Field(
            FieldTimeStamp,
            message.creationDate.ToString(),
            Field.Store.YES,
            Field.Index.NOT_ANALYZED);

目前我认为一种可行的解决方案是按文档的 timestampField 对索引中的所有文档进行排序,然后选择最上面的文档。有没有我可以制作的更适合此目的的搜索查询?

【问题讨论】:

  • 为什么要这样做?
  • 好吧,每当我索引一条消息时,我都想将消息的 TF-IDF 与前一个进行比较。为此,我需要将上一条消息的文档 ID 插入到索引中。因此,我认为有必要进行搜索,因为无法保证 Lucene 文档 ID。

标签: c# .net lucene lucene.net


【解决方案1】:

有几个选项。

首先,确保您的“时间戳”字段是可排序的。 ToString() 对文化敏感,因此不能保证可订购。 ToString("o") 可以,但是...

我更喜欢使用数字字段并将 DateTime.UtcNow().Ticks 放入其中。 (使用 .SetLongValue(ticks) )

那么,

  • 为该字段创建降序排序。类似的东西

    var sort = new Sort(new SortField(TimestampFieldName, SortField.LONG, true))
    
  • 使用 Searcher 获取收集器(注意 .Create 行中的“1”= 最多只返回一个结果)

    var collector = TopFieldCollector.Create(sort, 1, false, trackScores, trackScores, trackScores);
    searcher.Search(query, filter, collector);
    var topdocs = collector.TopDocs();
    
  • 获取文档

    var topdoc = topDocs.ScoreDocs[0];
    var doc = container.Document(topdoc.Doc);
    
  • 利润!!!

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多