zoukankan      html  css  js  c++  java
  • Lucene.Net中按时间范围查询,结果没有查到数据

          最近运用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;
      }
  • 相关阅读:
    linux 系统账户 和 普通账户 的区别
    supervisor 相关命令
    linux 动态链接库查找方法;查找动态链接库位置; LIBRARY_PATH 和 LD_LIBRARY_PATH 的区别;LD_LIBRARY_PATH and LD_RUN_PATH的区别;MACOS 下查看动态链接库方法
    Ubuntu 一键伪装成Win 10,Kali Linux 2019 kali-undercover软件嫁接;Ubuntu 1804 使用 kali-undercover;
    C 实战练习题目30 – 回文数
    C 实战练习题目29
    C 实战练习题目28
    C 实战练习题目27
    C 实战练习题目26 -递归法求阶乘
    C 实战练习题目25
  • 原文地址:https://www.cnblogs.com/huazai8204/p/793157.html
Copyright © 2011-2022 走看看