小編給大家分享一下LINQ To Lucene的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站是一家專業(yè)提供阿拉善盟企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、網(wǎng)站建設(shè)、H5場景定制、小程序制作等業(yè)務(wù)。10年已為阿拉善盟眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
lucene是在JAVA中比較有名的開源項(xiàng)目,也有.NET移植版lucene.net,不過在apache的官方網(wǎng)站上還是一個孵化器項(xiàng)目,而且好像2007年就不更新了,現(xiàn)在codeplex上推出了LINQ To Lucene,真是一個好消息。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Lucene.Linq.Mapping; using Lucene.Net.Analysis; using Lucene.Linq; namespace LinqToLucene1 { [Document] public class Book : IIndexable, IHit { [Field(FieldIndex.Tokenized,FieldStore.Yes, IsDefault = true)] public string Title { get; set; } [Field(FieldIndex.Tokenized, FieldStore.Yes)] public string Author { get; set; } [Field(FieldIndex.Tokenized, FieldStore.Yes)] public string PubTime { get; set; } [Field(FieldIndex.Tokenized, FieldStore.Yes)] public string Publisher { get; set; } region IHit Members public int DocumentId { get; set; } public float Relevance { get; set; } endregion } }
linq to lucene采用attribute的方式,非常簡單方便。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Lucene.Net.Documents; using Lucene.Linq.Mapping; using Lucene.Linq; using Lucene.Net.Analysis; namespace LinqToLucene1 { public class Program { static void Main(string[] args) { IIndexbookIndex = new Index (); bookIndex.Add(new Book() { Title = "誰都逃不掉的金融危機(jī)", Author = "xxx", Publisher = "東方出版社", PubTime = "2008年12月" }); bookIndex.Add(new Book() { Title = "許我向你看(“暖傷青春代言人” 辛夷塢《致我們終將逝去的青春》***續(xù)作)", Author = "辛夷塢", Publisher = "河南文藝出版社", PubTime = "2008年12月" }); bookIndex.Add(new Book() { Title = "大貓兒的TT奮斗史(都市小白領(lǐng)的爆雷囧事錄)", Author = "阿巳", Publisher = "國際文化出版公司", PubTime = "2008年12月" }); bookIndex.Add(new Book() { Title = "佳期如夢之海上繁花(匪我思存***作品上市)", Author = "匪我思存", Publisher = "新世界出版社", PubTime = "2008年12月" }); var result = from book in bookIndex where book.Author == "xxx" select book; foreach (Book book in result) { System.Console.WriteLine(book.Title); } System.Console.ReadLine(); } } }
不過有個bug,如果寫成from Book book in bookIndex 的話,就會報(bào)異常。
以上是“LINQ To Lucene的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!