【问题标题】:Get next date in lucene index获取 lucene 索引中的下一个日期
【发布时间】:2012-10-09 14:42:11
【问题描述】:

我将事件列表存储在 lucene.net 索引中,该索引具有开始日期字段。我想添加一个按钮供用户单击以查看与他们正在查看的事件相关的下一个事件(基于开始日期字段)。

我目前正在使用 ConstantScoreRangeQuery 搜索来搜索两个日期之间的事件,但不确定如何在索引中获取下一个日期。

【问题讨论】:

    标签: search lucene lucene.net


    【解决方案1】:

    这是使用TermEnum 的简单方法

            RAMDirectory ramDir = new RAMDirectory();
    
            IndexWriter iw = new IndexWriter(ramDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT));
    
            Document d = new Document();
            Field date = new Field("date", "", Field.Store.YES, Field.Index.ANALYZED);
            d.Add(date);
    
            DateTime first = DateTime.Now.Subtract(TimeSpan.FromDays(5));
            DateTime second = DateTime.Now.Subtract(TimeSpan.FromDays(4));
            DateTime third = DateTime.Now.Subtract(TimeSpan.FromDays(3));
            DateTime fourth = DateTime.Now.Subtract(TimeSpan.FromDays(2));
            DateTime fifth = DateTime.Now.Subtract(TimeSpan.FromDays(1));
    
            date.SetValue(DateTools.DateToString(first, DateTools.Resolution.MINUTE));
            iw.AddDocument(d);
            date.SetValue(DateTools.DateToString(second, DateTools.Resolution.MINUTE));
            iw.AddDocument(d);
            date.SetValue(DateTools.DateToString(third, DateTools.Resolution.MINUTE));
            iw.AddDocument(d);
            date.SetValue(DateTools.DateToString(fourth, DateTools.Resolution.MINUTE));
            iw.AddDocument(d);
            date.SetValue(DateTools.DateToString(fifth, DateTools.Resolution.MINUTE));
            iw.AddDocument(d);
    
            iw.Commit();
    
            IndexReader ir = iw.GetReader();
    
            var termEnum = ir.Terms(new Term("date", DateTools.DateToString(second, DateTools.Resolution.MINUTE)));
    
            if (termEnum.Next())
            {
                Console.WriteLine(DateTools.StringToDate(termEnum.Term().Text()));
                Console.WriteLine(DateTools.StringToDate(termEnum.Term().Text()).Day == third.Day);
            }
    
            Console.Read();
    

    【讨论】:

    • 谢谢,我会试试看,如果它适合我​​,请告诉你。
    猜你喜欢
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2012-12-11
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多