本文向大家介绍LINQ To Lucene,可能好多人还不了解LINQ To Lucene,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

创新互联建站是一家以网站建设公司、网页设计、品牌设计、软件运维、网站推广、小程序App开发等移动开发为一体互联网公司。已累计为软装设计等众行业中小客户提供优质的互联网建站和软件开发服务。
lucene是在JAVA中比较有名的开源项目,也有.NET移植版lucene.net,不过在apache的官方网站上还是一个孵化器项目,而且好像2007年就不更新了,现在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)
 - {
 - IIndex
 bookIndex = new Index (); - bookIndex.Add(new Book()
 - {
 - Title = "谁都逃不掉的金融危机",
 - Author = "xxx",
 - Publisher = "东方出版社",
 - PubTime = "2008年12月"
 - });
 - bookIndex.Add(new Book()
 - {
 - Title = "许我向你看(“暖伤青春代言人” 辛夷坞《致我们终将逝去的青春》***续作)",
 - Author = "辛夷坞",
 - Publisher = "河南文艺出版社",
 - PubTime = "2008年12月"
 - });
 - bookIndex.Add(new Book()
 - {
 - Title = "大猫儿的TT奋斗史(都市小白领的爆雷囧事录)",
 - 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 的话,就会报异常。
Copyright © 2009-2022 www.fjjierui.cn 青羊区广皓图文设计工作室(个体工商户)达州站 版权所有 蜀ICP备19037934号