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

推荐订阅源

S
Schneier on Security
雷峰网
雷峰网
S
Securelist
V
Vulnerabilities – Threatpost
S
SegmentFault 最新的问题
T
The Exploit Database - CXSecurity.com
A
About on SuperTechFans
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
L
Lohrmann on Cybersecurity
S
Security Affairs
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
H
Heimdal Security Blog
L
LINUX DO - 热门话题
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
D
DataBreaches.Net
N
Netflix TechBlog - Medium
The Hacker News
The Hacker News
N
News and Events Feed by Topic
C
Check Point Blog
博客园_首页
Scott Helme
Scott Helme
T
Troy Hunt's Blog
U
Unit 42

博客园 - 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;
        }