首先,我們先把我們要開始分解的程式的代碼貼出來先。
以下的代碼是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就是在一個線程裡面是唯一。別的線程不可能訪問得到各自線程的靜態變數。因為每個線程都會在記憶體塊裡面給自己的變數分配一塊記憶體。
領導來了,繼續工作先。後續。。