惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
量子位
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
B
Blog
U
Unit 42
C
Check Point Blog
I
InfoQ
aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
爱范儿
爱范儿

博客园 - nasdaqhe

Frida 使用 Android https 抓包 云效流水线部署ack Android 解包重签名打包 centos7 docker 安装及配置 CentOS 6 升级 curl Mac 下编译安装 php-5.6 ubuntu+php5-fpm 下安装 memcached PHP扩展 cmd下使用telnet连接到memcached服务器操作 SQL语句优化一例 row_number not in or flickr head中用到的标签 - nasdaqhe - 博客园 判断中文是否UTF8编码 MSSQL备忘 新浪微博产品图 (Vincent.H手笔) MindManage HTML5学习资料整理 ubuntu备忘 const 与 readonly ubuntu 10.04 安装 oracle11g VS2010 .NET 4学习资料整理
Lucence.Net 2.9.3 日期范围搜索
nasdaqhe · 2011-05-10 · via 博客园 - nasdaqhe

Posted on 2011-05-10 17:38  nasdaqhe  阅读(465)  评论()    收藏  举报

关键步骤如下:

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;
        }