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

推荐订阅源

Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园_首页
U
Unit 42
T
Tailwind CSS Blog
G
GRAHAM CLULEY
F
Full Disclosure
V
Vulnerabilities – Threatpost
T
Tenable Blog
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
K
Kaspersky official blog
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LINUX DO - 最新话题
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
罗磊的独立博客
S
Security Affairs
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
美团技术团队
Simon Willison's Weblog
Simon Willison's Weblog
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
V
Visual Studio Blog

博客园 - 追梦华仔

Openfire服务器MySQL优化 放开Linux内核对用户进程可打开文件数和TCP连接的限制 SSL 握手协议 非对称密码学(Asymmetric Cryptography)- 转载 Visual Studio 2008下载 Visual Studio 2008 提示“无法识别工具版本4.0” 改造MUC实现Openfire群 Openfire S2S 经验分享 注册Jmail组件的三种方法 读取excel文件的时候 出错提示:外部表不是预期的格式 - 追梦华仔 - 博客园 System.Runtime.InteropServices.COMException: 服务器出现意外情况 Excel表格导入出错,提示“拒绝访问”的错误,ExcelObj = new Excel.Application(); 在 ASP.NET 中执行 URL 重写 XP下如何解决“ASP.NET 未被授权访问所请求的资源”的问题 - 追梦华仔 - 博客园 无法在web服务器上启动调试 您不具备调试此应用程序的权限 - 追梦华仔 - 博客园 Server Application Error ---Http 500错误解决方法 用Lucene实现在检索结果中再检索 [转载]Ruby on Rails:开源技术将深入企业 Lucene.Net.Search.Highlight.FragmentQueue 中的派生方法 LessThan 不能减少访问
Lucene.Net中按时间范围查询,结果没有查到数据
追梦华仔 · 2007-06-22 · via 博客园 - 追梦华仔

      最近运用Lucene.Net做了个全文检索。开始时用了Lucene.Net 1.9版本,按照关键字查询没有问题,加入按时间范围查询,结果没有查到数据。取出Lucene语句直接在Luke工具里查询,也没有记录出来。
      我昨天发邮件请教了 雨痕 RainTrail (http://www.rainsts.net/),他回复我了,给了我一小段代码,一点思想,就开窍了。在此表示万分感谢!
      用Lucene.Net 1.9我目前没有找到解决办法,还是无法实现按时间范围查询,我改用 Lucene.Net 2.0 尝试,方案一:Lucene复合语句 + 运用RangeFilter类 实现了按时间范围查询。方案二:只用Lucene复合语句,可以查到数据,但关键显示内容为null了(不知道什么原因?)。关键代码如下供大家分享:

方案一:Lucene复合语句 + 运用RangeFilter类
public ResultSet Search(int pageIndex,int PageSize,int PostTime,.....)
  {
  
    ChineseAnalyzer analyse = new ChineseAnalyzer();
    StopWatch sw = new StopWatch();

//定义一个StopWatch  对象
    string strlucene = ""; //lucene语句未加入时间查询语句
    .... //如 strlucene = +( +Content:手机 +Content:数码相机 -Content:销售 )   
   QueryParser parser = new QueryParser("Content", analyse);
   Query query = parser.Parse(strlucene);   

    if(PostTime!=0)
   {

//运用RangeFilter类
        string lower = DateTime.Now.AddDays(PostTime*(-1)).ToString("yyyyMMdd");
        string upper = DateTime.Now.ToString("yyyyMMdd");
        RangeFilter filter = new RangeFilter("PostDateTime", lower, upper, true, true);

        Hits hits = searcher.Search(query, filter);
        long executionTime = sw.Peek();
        ResultSet results = GetResults(hits,pageIndex,query,PageSize); //见最下面,自己构建的方法,支持分页,返回自己需要的数据集    

       results.ExecutionTime = executionTime;
        return results; 
   }
   else
   {
        Hits hits = searcher.Search(query);
        long executionTime = sw.Peek();
        ResultSet results = GetResults(hits,pageIndex,query,PageSize); //同上 

        results.ExecutionTime = executionTime;
        return results; 
   }

}

方案二:只用Lucene复合语句

public ResultSet Search(int pageIndex,int PageSize,int PostTime,.....)
  {
  

     ChineseAnalyzer analyse = new ChineseAnalyzer();
     StopWatch sw = new StopWatch();
//定义一个StopWatch  对象

   

string strlucene = ""; //lucene语句加入了时间查询语句

    if(PostTime!=0)
   {
       strposttime = " +PostDateTime:["+ DateTime.Now.AddDays(PostTime*(-1)).ToString("yyyyMMdd") + " TO " + DateTime.Now.ToString("yyyyMMdd") +"] ";   

    }   

     .... //如 strlucene = +( +Content:手机 +Content:数码相机 -Content:销售 )   
   QueryParser parser = new QueryParser("Content", analyse);
   Query query = parser.Parse(strlucene); 
   

   

Hits hits = searcher.Search(query);
    long executionTime = sw.Peek();
    ResultSet results = GetResults(hits,pageIndex,query,PageSize); //同上  

    results.ExecutionTime = executionTime;
    return results;
 
}

private ResultSet GetResults(Hits hits,int pageIndex,Query query,int PageSize)
  {
       int startPosition = (pageIndex - 1) * PageSize;
       int endPosition = startPosition + PageSize;
       if(hits.Length() < endPosition)
       {
            endPosition = hits.Length();
       }
       return GetResults(hits,startPosition,endPosition,query);
  
}
  

private ResultSet GetResults(Hits hits, int startPosition, int endPosition, Query query)
  {
       ResultSet results = new ResultSet();
       try
       {
           ChineseAnalyzer analyzer = new ChineseAnalyzer();
            results.Count = hits.Length();

            Highlighter highlighter = null;
            ArrayList al = new ArrayList();
            Result result = null;
            Document doc = null;
            for(int i = startPosition; i<endPosition; i++)
            {
                result = new Result();
                 doc = hits.Doc(i);
                result.PostDateTime = doc.GetField("PostDateTime").StringValue();       
                result.UserName = doc.GetField("UserName").StringValue();
                 result.PostUrl = doc.GetField("PostUrl").StringValue();    
                 result.Title = doc.GetField("Title").StringValue();   
                 if(FragementSize > 0)
                 {
                      string text = doc.GetField("Content").StringValue();
                         // 高亮显示设置
                    SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                      highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));      
                     highlighter.SetTextFragmenter(new SimpleFragmenter(FragementSize));      
                      TokenStream tokenStream = analyzer.TokenStream("Content", new StringReader(text));
                     result.Content = highlighter.GetBestFragment(tokenStream,text); //加入时间查询语句时,问题出于这里,被赋了null值

                 }
                 else
                 {
                      result.Content = doc.GetField("Content").StringValue();
                 }        
                 result.Score = hits.Score(i);

                al.Add(result);
            }
            results.Results = (Result[])al.ToArray(typeof(Result));
       }
       catch(Exception e)
       {
            LogError.Write("GetResult Fail:"+e.Message);
       }
       return results;