重新一步一步學習Lucene.NET 一個簡單的程式開始(1)

來源:互聯網
上載者:User

首先,我們先把我們要開始分解的程式的代碼貼出來先。

以下的代碼是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就是在一個線程裡面是唯一。別的線程不可能訪問得到各自線程的靜態變數。因為每個線程都會在記憶體塊裡面給自己的變數分配一塊記憶體。

 

領導來了,繼續工作先。後續。。

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.