【轉載】使用Lucene.NET實現資料檢索功能

來源:互聯網
上載者:User

標籤:Lucene   style   blog   http   color   使用   os   io   

1、索引的管理

//指定索引庫檔案存放檔案位置FSDirectory directory = FSDirectory.Open(new DirectoryInfo(this.IndexDataDir), new NativeFSLockFactory());//判斷索引檔案目錄是否存在bool isExist = IndexReader.IndexExists(directory);if (isExist){    if (IndexWriter.IsLocked(directory))    {        IndexWriter.Unlock(directory);    }}//Apsara Distributed File System分詞器PanGuAnalyzer analyzer = new PanGuAnalyzer();//索引寫入類IndexWriter writer = new IndexWriter(directory, analyzer, !isExist, IndexWriter.MaxFieldLength.UNLIMITED);//迴圈隊列執行操作while (IndexDataQueue.Count > 0){    Document document = new Document();//這是我為索引資料自訂的模型類,主要內容是檔案的路徑、名稱、內容和索引管理的操作類型(新增、更新、刪除)    BaseDataMode mode = IndexDataQueue.Dequeue();    switch (mode.Type)    {        case OperationType.Insert:            {                foreach (KeyValuePair<string, string> kv in mode.Content)                {                    //這裡kv.Key是設定索引內欄位的名稱,kv.Value是這個欄位記憶體儲的內容。                    document.Add(new Field(kv.Key, kv.Value, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));                }                writer.AddDocument(document);            }; break;        case OperationType.Update:        {            //設定刪除條件            MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "id" }, analyzer);            Query query = parser.Parse(mode.Content["id"]);            writer.DeleteDocuments(query);            foreach (KeyValuePair<string, string> kv in mode.Content)            {                document.Add(new Field(kv.Key, kv.Value, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));            }            writer.AddDocument(document);        }; break;        case OperationType.Delete:        {            MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "id" }, analyzer);            Query query = parser.Parse(mode.Content["id"]);            writer.DeleteDocuments(query);        }; break;        default: { }; break;    }}//提交操作writer.Commit();//最佳化writer.Optimize();//關閉串連writer.Close();directory.Close();

2、檢索

//指定索引庫檔案存放檔案位置FSDirectory directory = FSDirectory.Open(new DirectoryInfo(this.IndexDir), new NativeFSLockFactory());IndexReader reader = IndexReader.Open(directory, true);IndexSearcher searcher = new IndexSearcher(reader);//設定關鍵詞在條件中為OR關係BooleanQuery queryOr = new BooleanQuery();foreach (string word in SplitContent.SplitByPanGu(keyword)){    foreach (KeyValuePair<string, string> kv in Mode.Content)    {        TermQuery query = new TermQuery(new Term(kv.Key, word));        //這裡設定條件為Or關係        queryOr.Add(query, BooleanClause.Occur.SHOULD);    }}//擷取搜尋結果       //1000為搜尋檔案的下標限制,設定這個可以控制檢索的範圍,也可以用於分頁顯示TopDocs tds = searcher.Search(queryOr, null, 1000);ScoreDoc[] docs = tds.scoreDocs;for (int i = 0; i < docs.Length; i++){    int docId = docs[i].doc;    Document doc = searcher.Doc(docId);string content = doc.Get("索引內欄位的名稱");}

轉自:http://www.cnblogs.com/liusuqi/p/3671161.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.