zoukankan      html  css  js  c++  java
  • Lucence.Net 2.9.3 日期范围搜索

    关键步骤如下:

    1.将 DateTime 转换成 long 型。
    2.索引时要使用 NumericField 进行索引
    3.搜索时使用 NumericRangeQuery 进行查询

     事例代码如下:

      [TestMethod()]

            public void RangTest()
            {
                RAMDirectory mDirInfo 
    = new RAMDirectory();
                IndexWriter mIndexWriter 
    = new IndexWriter(mDirInfo, CAnalyzerLoader.GetAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
                mIndexWriter.AddDocument(GetDocumentByModel(
    1"姑姑,我是态儿", CDateTime.UNIX_TIMESTAMP(System.DateTime.Now.AddDays(-1))));
                mIndexWriter.AddDocument(GetDocumentByModel(
    2"B儿,我是态儿", CDateTime.UNIX_TIMESTAMP(System.DateTime.Now.AddDays(-2))));
                mIndexWriter.AddDocument(GetDocumentByModel(
    3"肝儿,我是态儿", CDateTime.UNIX_TIMESTAMP(System.DateTime.Now.AddDays(-3))));
                mIndexWriter.Close();
                
    long upper = CDateTime.UNIX_TIMESTAMP(DateTime.Now.AddDays(-1));
                
    long lower = CDateTime.UNIX_TIMESTAMP(DateTime.Now.AddDays(-3).AddHours(-1));
                NumericRangeQuery rangeQuery 
    = NumericRangeQuery.NewLongRange("UpdateOn", lower, upper, truetrue);

                IndexSearcher mSearcher 
    = new IndexSearcher(mDirInfo, true);
                TopDocs topDocs 
    = mSearcher.Search(rangeQuery, 100);
                Assert.AreEqual(
    2, topDocs.totalHits);
            }

            
    private Document GetDocumentByModel(int tid,string title,long time)
            {
                Document doc 
    = new Document();
                doc.AddFiled(
    "Id", tid.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)
                    .AddFiled(
    "Title", title.Trim(), Field.Store.YES, Field.Index.ANALYZED)
                    .AddLongFiled(
    "UpdateOn", time, Field.Store.NO, true);
                
    return doc;
            }
            
    public static Document AddFiled(this Document self, string name, string value, Field.Store fStore, Field.Index fIndex)
            {
                Field filed 
    = new Field(name, value, fStore, fIndex);
                self.Add(filed);
                
    return self;
            }
            
    public static Document AddLongFiled(this Document self, string name, long value, Field.Store fStore, bool bIndex)
            {
                NumericField filed 
    = new NumericField(name, NumericUtils.PRECISION_STEP_DEFAULT, fStore, bIndex).SetLongValue(value);
                self.Add(filed);
                
    return self;
            }
  • 相关阅读:
    NOIP2017 时间复杂度 大模拟
    【Python】CV2的一些基本操作
    【Python】类、对象、self
    【Ubuntu18.04】清空回收站
    小飞机可以解决git clone没有返回的问题吗?
    sqlserver2005 远程服务器数据 完全拷贝 到本地数据库
    Microsoft Visual Studio 2005 多线程时 解决不是该线程创建的来访问
    MS SqL2000 数据库置疑状态的解决方法[转]
    vue 函数配置项watch以及函数 $watch 源码分享
    Vue生命周期之beforeCreate vue 生命周期详解
  • 原文地址:https://www.cnblogs.com/nasdaqhe/p/2042331.html
Copyright © 2011-2022 走看看