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

推荐订阅源

W
WeLiveSecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cloudbric
Cloudbric
V
Visual Studio Blog
L
LangChain Blog
A
About on SuperTechFans
B
Blog
T
Tenable Blog
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Palo Alto Networks Blog
U
Unit 42
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
C
Check Point Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence
Application and Cybersecurity Blog
Application and Cybersecurity Blog
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
S
Securelist
Security Archives - TechRepublic
Security Archives - TechRepublic
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Latest news
Latest news
GbyAI
GbyAI
T
Troy Hunt's Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
V2EX - 技术
V2EX - 技术
小众软件
小众软件
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
Netflix TechBlog - Medium
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed

博客园 - 追梦华仔

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;