重新一步一步学习Lucene.NET 一个简单的程序开始(1)

首先,我们先把我们要开始分解的程序的代码贴出来先。

以下的代码是yurow birdshover 这位网友的。
代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  Lucene.Net.Index;
using  Lucene.Net.Store;
using  Lucene.Net.Analysis;
using  Lucene.Net.Analysis.Standard;
using  Lucene.Net.Documents;
using  Lucene.Net.Search;
using  Lucene.Net.QueryParsers;

namespace  LuceneText
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Analyzer analyzer 
=   new  StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
            
            
///  建立索引
            IndexWriter writer  =   new  IndexWriter( " IndexDirectory " , analyzer,  true );
            AddDocument(writer, 
" SQL Server 2008的发布 " " SQL Server 2008的新特征 " );
            AddDocument(writer, 
" ASP.Net MVC框架配置与分析 " " 而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions " );
            writer.Optimize();
            writer.Close();


            
/// 搜索索引
             // IndexSearcher searcher = new IndexSearcher("IndexDirectory");
            
// MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "title", "content" }, analyzer);
            
// Query query = parser.Parse("sql");
            
// Hits hits = searcher.Search(query);
            
// for (int i = 0; i < hits.Length(); i++)
            
// {
            
//     Document doc = hits.Doc(i);
            
//     Console.WriteLine(string.Format("title;{0} content:{1}", doc.Get("title"), doc.Get("content")));
            
// }
            
// searcher.Close();

            
// Console.ReadKey();
        }

        
static   void  AddDocument(IndexWriter writer,  string  title,  string  content)
        {
            Document document 
=   new  Document();
            document.Add(
new  Field( " title " , title, Field.Store.YES, Field.Index.TOKENIZED));
            document.Add(
new  Field( " content " , content, Field.Store.YES, Field.Index.TOKENIZED));
            writer.AddDocument(document);
        }
    }
}

那我们现在一步一步的来看。

首先我们Analyzer实例化开始,它的参数是当前的版本。因为大家都知道lucene已经出了很多版本,为了兼容,所以他会根据版本做出相应功能的增减标示。

首先我们看一下,实例化后的analyzer的信息。

 

我们可以看到有一个静态变量slots,我们来看看他的声明

 在父类Analyzer里面有个私有的属性 private CloseableThreadLocal tokenStreams = new CloseableThreadLocal();

然后再来看看CloseableThreadLocal的定义,在CloseableThreadLocal有一个定义了[ThreadStatic]属性的静态变量。

[ThreadStatic]
static SupportClass.WeakHashTable slots; 

 ThreadStatic顾名思义,他是一个线程的静态变量。我们大家都知道。静态变量在一个进程里面是唯一的。那ThreadStatic就是在一个线程里面是唯一。别的线程不可能访问得到各自线程的静态变量。因为每个线程都会在内存块里面给自己的变量分配一块内存。

 

领导来了,继续工作先。后续。。

 

 


 

你可能感兴趣的:(Lucene)