LINQ To Lucene簡單介紹
本文向大家介紹LINQ To Lucene,可能好多人還不了解LINQ To Lucene,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。
lucene是在JAVA中比較有名的開源項(xiàng)目,也有.NET移植版lucene.net,不過在apache的官方網(wǎng)站上還是一個(gè)孵化器項(xiàng)目,而且好像2007年就不更新了,現(xiàn)在codeplex上推出了LINQ To Lucene,真是一個(gè)好消息。
- 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)
- {
- IIndex<Book> bookIndex = new Index<Book>();
- 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();
- }
- }
- }
不過有個(gè)bug,如果寫成from Book book in bookIndex 的話,就會報(bào)異常。
【編輯推薦】