三、全文搜索
给数据访问模板DaoTemplate添加一个全文搜索的方法:

Code
[http://www.xueit.com]
/**//// <summary>
/// 全文检索
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="query">关键词</param>
/// <returns></returns>
IList FullTextSearch(string query)#region IList<T> FullTextSearch<T>(string query)
public IList<T> FullTextSearch<T>(string query)
{
//生成字段列表
object objTarget = Assembly.GetAssembly(typeof(T)).CreateInstance(typeof(T).ToString());
PropertyInfo[] pps = objTarget.GetType().GetProperties();
string fs = "";
foreach(PropertyInfo p in pps)
{
var fieldAttr = p.GetCustomAttributes
(typeof(FieldAttribute),false);
if (fieldAttr!=null && fieldAttr.Length>0)
{
fs = p.Name ",";
}
}
string[] fields = fs.TrimEnd(',').Split(',');
//中文拆词
ChineseAnalyzer cnAnalyzer = new ChineseAnalyzer();
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, cnAnalyzer);
Query queryObj;
try
{
queryObj = parser.Parse(query);
}
catch (ParseException)
{
return null;
}
//使用当前Session
IFullTextSession fullTextSession = NHibernate.Search.Search.CreateFullTextSession(Session);
IQuery nhQuery = fullTextSession.CreateFullTextQuery(queryObj, typeof(T));
//结果
IList<T> results = nhQuery.List<T>();
return results;
}
#endregion
四、运行一下
Insert对象:

Search:

随笔写得很粗糙,大部分细节在附件的例程包中(包括数据库备份,模板,项目文件),如果有兴趣,还是参考实例吧:
lucenedemo.rar
精彩图集