追寻人生

学习之园地,创意之源泉

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

      最近运用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;
  }
posted on 2007-06-22 15:00  追梦华仔  阅读(2333)  评论(1编辑  收藏  举报