初识Lucene.net
生活随笔
收集整理的這篇文章主要介紹了
初识Lucene.net
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近想提高下自己的能力,也是由于自己的項目中需要用到Lucene,所以開始接觸這門富有挑戰又充滿新奇的技術。。 剛剛開始,只是寫了個小小的demo,用了用lucene,確實很好 創建索引 DataTable dt = DB.SqlHelper.ExecuteDataset(connectionString, CommandType.Text, "select top 1000 id,title,productsummary from dbo.products").Tables[0]; Lucene.Net.Store.FSDirectory fs = Lucene.Net.Store.FSDirectory.GetDirectory(basePath); if (Lucene.Net.Index.IndexReader.IsLocked(fs)) Lucene.Net.Index.IndexReader.Unlock(fs); Lucene.Net.Index.IndexWriter iw = new Lucene.Net.Index.IndexWriter(basePath, new Lucene.Net.Analysis.Standard.StandardAnalyzer()); foreach (DataRow dr in dt.Rows) { Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); doc.Add(new Lucene.Net.Documents.Field("id", dr["id"].ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO)); doc.Add(new Lucene.Net.Documents.Field("title", dr["title"].ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); doc.Add(new Lucene.Net.Documents.Field("productsummary", dr["productsummary"].ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); iw.AddDocument(doc); } iw.Optimize(); iw.Close(); 搜索結果 System.Text.StringBuilder sb = new System.Text.StringBuilder(); Lucene.Net.Store.FSDirectory dir = Lucene.Net.Store.FSDirectory.GetDirectory(basePath); Lucene.Net.Analysis.PanGu.PanGuAnalyzer pgAnalyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer(); Lucene.Net.Search.IndexSearcher search = new Lucene.Net.Search.IndexSearcher(dir); Lucene.Net.QueryParsers.QueryParser qp = new Lucene.Net.QueryParsers.QueryParser("title", pgAnalyzer); Lucene.Net.Search.Query query = qp.Parse(this.txtKeywords.Text); ?//第一種結果,使用Hits集合
? ? ? ? ? ? Lucene.Net.Search.Hits hits = search.Search(query); ? ? ? ? ? ? for (int i = 0; i < hits.Length(); i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Lucene.Net.Documents.Document doc = hits.Doc(i); ? ? ? ? ? ? ? ? sb.Append(doc.GetField("title").StringValue()+"<br />"); ? ? ? ? ? ? } ?//第二種結果,使用TopDocs(最精確的結果) ?????????? Lucene.Net.Search.TopDocs topDocs = search.Search(query, 100);//100為返回的結果數目,必須是大于0的數字,分頁的時候會用到
??????????? Lucene.Net.Search.ScoreDoc[] result = topDocs.scoreDocs;
??????????? for (int i = 0; i < result.Length; i++)
??????????? {
??????????????? Lucene.Net.Documents.Document doc = search.Doc(result[i].doc);
????????????????sb.Append((i+1).ToString() + "\t" + highter.GetBestFragment(this.txtKeywords.Text, doc.Get("title")) + "<br />");
??????????? } Response.Write(sb.ToString()); dir.Close(); ? 結果 ?自己寫的第一個小例子,當出現結果的那一剎那,瞬間充滿了驚喜,因為自己調試了很久才出現結果,真的對自己也是一種鼓勵,堅持下去,以后還會繼續深入研究lucene。。。。
轉載于:https://www.cnblogs.com/xiaoshouzi/p/3195004.html
總結
以上是生活随笔為你收集整理的初识Lucene.net的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 具体的压栈指令,例子
- 下一篇: python time timeit_p