Spring.net整合Lucene.net 实现全文检索(附例程)
2009-11-09 21:53 莫耶 阅读(2719) 评论(10) 编辑 收藏 举报什么是Lucene.net?
引用wikipedia上的介绍:“Lucene是一套用于全文检索和搜尋的開放源碼程式庫,由Apache软件基金会支持和提供。Lucene提供了一個簡單卻強大的應用程式介面,能夠做全文索引和搜尋...”
而Lucene.net便是Lucene在dot net平台上的移植版本。关于Lucene的一些概念,有兴趣的朋友可以参考这里。
要实现Spring.net与Lucene.net的整合,关键点在于:
1、查询时,使用Spring.net集成的nHibernate来管理Lucene.net使用的Session;
2、对数据实体的CRUD进行事件监控,以便动态更新索引
经过一番尝试,决定将原有的框架进行升级——spring.net 1.2.0 + nHibernaet 2.0.1 + Lucene.net 2.0 + nHibernate.Search。
ok,看看都需要进行哪些调整:
一、实体生成模板加上索引
在model层引用nHibernate.Search程序集,它的作用是根据实体上的元标记,选择是否为实体开启存储空间,以及索引的字段范围。
修改codeSmith中的nHibernate Template,像这样,用以将主键和字符型字段进行索引:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a C# class for use with NHibnate" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the mapping file is based on" %>
<%@ Property Name="Namespace" Type="System.String" Default="MyNamespace.Data" Category="Object" Description="The class namespace that the mapping file should use" %>
<%@ Property Name="Assembly" Type="System.String" Default="MyApp.MyAssembly" Category="Object" Description="The assembly that the class will be used in" %>
<%@ Property Name="RemoveTablePrefix" Type="System.String" Default="tbl" Category="Object" Description="The prefix to remove from table names" %>
<%@ Property Name="ForceId" Type="System.Boolean" Default="true" Category="Object" Description="Force Id for identity column" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
using System;
using System.Collections.Generic;
using NHibernate.Search.Attributes;
using NHibernate.Search;
namespace <%= Namespace %>
{
#region <%= ClassName(SourceTable) %>
/// <summary>
/// <%= ClassName(SourceTable) %> object for NHibernate mapped table '<%= ClassTable(SourceTable) %>'.
/// </summary>
[Serializable]
[TableInfo(TableName = "<%= ClassTable(SourceTable) %>", PrimaryKey = "<%= ColumnName(SourceTable.PrimaryKey.MemberColumns[0])%>" , Columns="<% for(int i=0;i<SourceTable.Columns.Count;i++) {%><%=ColumnName(SourceTable.Columns[i]) + (i==SourceTable.Columns.Count-1 ? "":",") %><%}%>")]
[Indexed(Index = "<%= ClassName(SourceTable) %>")]
public class <%= ClassName(SourceTable) %>
{
#region Member Variables
<% if (SourceTable.PrimaryKey.MemberColumns.Count == 1) {%>
protected <%= IdMemberType(SourceTable) %> <%= IdMemberName(SourceTable) %>;
<% } %>
<% foreach(ColumnSchema column in SourceTable.NonKeyColumns) { %>
protected <%= MemberType(column) %> <%= MemberName(column) %>;
<% } %>
<% foreach (TableKeySchema foreignKey in SourceTable.ForeignKeys) { %>
protected <%= ManyToOneClass(foreignKey) %> <%= ManyToOneMemberName(foreignKey) %>;
<% if ((foreignKey.ForeignKeyTable == SourceTable) && (foreignKey.PrimaryKeyTable == SourceTable)) { %>
protected <%= CollectionType(foreignKey)%> <%= CollectionMemberName(foreignKey)%>;
<% } %>
<% } %>
<% foreach(TableKeySchema primaryKey in SourceTable.PrimaryKeys) { %>
<% if (IsManyToManyTable(primaryKey.ForeignKeyTable)) { %>
protected <%= CollectionType(primaryKey)%> <%= CollectionManyToManyMemberName(primaryKey)%>;
<% } else if (IsOneToOneTable(primaryKey)) { %>
protected <%= OneToOneClass(primaryKey) %> <%= OneToOneMemberName(primaryKey) %>;
<% } else if (IsSubClassTable(primaryKey)) { %>
<% } else { %>
protected <%= CollectionType(primaryKey)%> <%= CollectionMemberName(primaryKey)%>;
<% } %>
<% } %>
#endregion
#region Constructors
public <%= ClassName(SourceTable) %>() { }
public <%= ClassName(SourceTable) %>( <%
int count = 0;
foreach(ColumnSchema column in SourceTable.NonKeyColumns)
{
if (count > 0) Response.Write(", ");
%><%= MemberType(column) %> <%= ParameterName(column) %><%
count ++;
}
foreach(TableKeySchema foreignKey in SourceTable.ForeignKeys)
{
if (count > 0) Response.Write(", ");
%><%= ManyToOneClass(foreignKey) %> <%= ManyToOneParameterName(foreignKey) %><%
count ++;
}
%> )
{
<% foreach(ColumnSchema column in SourceTable.NonKeyColumns) { %>
this.<%= MemberName(column) %> = <%= ParameterName(column) %>;
<% } %>
<% foreach(TableKeySchema foreignKey in SourceTable.ForeignKeys) { %>
this.<%= ManyToOneMemberName(foreignKey) %> = <%= ManyToOneParameterName(foreignKey) %>;
<% } %>
}
#endregion
#region Public Properties
<% if (SourceTable.PrimaryKey.MemberColumns.Count == 1) {%>
[DocumentId]
public <%= IdMemberType(SourceTable) %> <%= IdName(SourceTable) %>
{
get {return <%= IdMemberName(SourceTable) %>;}
<% if (IdMemberType(SourceTable) == "string" ) {%>
set
{
if ( value != null && value.Length > <%= ColumnLength(SourceTable.PrimaryKey.MemberColumns[0])%>)
throw new ArgumentOutOfRangeException("Invalid value for <%= IdName(SourceTable) %>", value, value.ToString());
<%= IdMemberName(SourceTable) %> = value;
}
<% } else { %>
set {<%= IdMemberName(SourceTable) %> = value;}
<% } %>
}
<% } %>
<% foreach(ColumnSchema column in SourceTable.NonKeyColumns) { %>
<%if( MemberType(column) =="string"){%>
[Field(Index.Tokenized, Store = Store.Yes)]
<%}%>
public <%= MemberType(column) %> <%= PropertyName(column) %>
{
get { return <%= MemberName(column) %>; }
<% if (MemberType(column) == "string") {%>
set
{
<% if(ColumnLength(column) != "16" ){ %>
if ( value != null && value.Length > <%= ColumnLength(column)%>)
throw new ArgumentOutOfRangeException("Invalid value for <%= PropertyName(column) %>", value, value.ToString());
<%}%>
<%= MemberName(column) %> = value;
}
<% } else { %>
set { <%= MemberName(column) %> = value; }
<% } %>
}
<% } %>
<% foreach(TableKeySchema foreignKey in SourceTable.ForeignKeys) { %>
public <%= ManyToOneClass(foreignKey) %> <%= ManyToOneName(foreignKey) %>
{
get { return <%= ManyToOneMemberName(foreignKey) %>; }
set { <%= ManyToOneMemberName(foreignKey) %> = value; }
}
<% if ((foreignKey.ForeignKeyTable == SourceTable) && (foreignKey.PrimaryKeyTable == SourceTable)) { %>
public <%= CollectionType(foreignKey) %> <%= CollectionName(foreignKey) %>
{
get { return <%= CollectionMemberName(foreignKey) %>; }
set { <%= CollectionMemberName(foreignKey) %> = value; }
}
<% } %>
<% } %>
<% foreach(TableKeySchema primaryKey in SourceTable.PrimaryKeys) { %>
<% if (IsManyToManyTable(primaryKey.ForeignKeyTable)) { %>
public <%= CollectionType(primaryKey) %> <%= CollectionManyToManyName(primaryKey) %>
{
get
{
if (<%= CollectionManyToManyMemberName(primaryKey) %>==null)
{
<%= CollectionManyToManyMemberName(primaryKey) %> = <%= NewCollectionType(primaryKey) %>;
}
return <%= CollectionManyToManyMemberName(primaryKey) %>;
}
set { <%= CollectionManyToManyMemberName(primaryKey) %> = value; }
}
<% } else if (IsOneToOneTable(primaryKey)) { %>
public <%= OneToOneClass(primaryKey) %> <%= OneToOneName(primaryKey) %>
{
get { return <%= OneToOneMemberName(primaryKey) %>; }
set { <%= OneToOneMemberName(primaryKey) %> = value; }
}
<% } else if (IsSubClassTable(primaryKey)) { %>
<% } else { %>
public <%= CollectionType(primaryKey) %> <%= CollectionName(primaryKey) %>
{
get
{
if (<%= CollectionMemberName(primaryKey) %>==null)
{
<%= CollectionMemberName(primaryKey) %> = <%= NewCollectionType(primaryKey) %>;
}
return <%= CollectionMemberName(primaryKey) %>;
}
set { <%= CollectionMemberName(primaryKey) %> = value; }
}
<% } %>
<% } %>
#endregion
<% foreach(TableKeySchema primaryKey in SourceTable.PrimaryKeys) { %>
<% if (IsManyToManyTable(primaryKey.ForeignKeyTable)) { } %>
<% else if (IsSubClassTable(primaryKey)) { %>
#region <%= JoinedSubclassName(primaryKey) %>
/// <summary>
/// <%= JoinedSubclassName(primaryKey) %> object for NHibernate mapped table '<%= JoinedSubclassTable(primaryKey) %>'.
/// </summary>
public class <%= JoinedSubclassName(primaryKey) %> : <%= ClassName(SourceTable) %>
{
#region Member Variables
<% foreach(ColumnSchema column in primaryKey.ForeignKeyTable.NonKeyColumns) { %>
protected <%= MemberType(column) %> <%= MemberName(column) %>;
<% } %>
<% foreach (TableKeySchema foreignKey in primaryKey.ForeignKeyTable.ForeignKeys) { %>
<% if (foreignKey.PrimaryKeyTable != SourceTable) { %>
protected <%= ManyToOneClass(foreignKey) %> <%= ManyToOneMemberName(foreignKey) %>;
<% } %>
<% if ((foreignKey.ForeignKeyTable == primaryKey.ForeignKeyTable) && (foreignKey.PrimaryKeyTable == primaryKey.ForeignKeyTable)) { %>
protected <%= CollectionType(foreignKey)%> <%= CollectionMemberName(foreignKey)%>;
<% } %>
<% } %>
#endregion
#region Constructors
public <%= JoinedSubclassName(primaryKey) %>() : base() { }
public <%= JoinedSubclassName(primaryKey) %>( <%
count = 0;
foreach(ColumnSchema column in SourceTable.NonKeyColumns)
{
if (count > 0) Response.Write(", ");
%><%= MemberType(column) %> <%= ParameterName(column) %><%
count ++;
}
foreach(TableKeySchema foreignKey in SourceTable.ForeignKeys)
{
if (count > 0) Response.Write(", ");
%><%= ManyToOneClass(foreignKey) %> <%= ManyToOneParameterName(foreignKey) %><%
count ++;
}
foreach(ColumnSchema column in primaryKey.ForeignKeyTable.NonKeyColumns)
{
if (count > 0) Response.Write(", ");
%><%= MemberType(column) %> <%= ParameterName(column) %><%
count ++;
}
foreach(TableKeySchema foreignKey in primaryKey.ForeignKeyTable.ForeignKeys)
{
if (foreignKey.PrimaryKeyTable != SourceTable)
{
if (count > 0) Response.Write(", ");
%><%= ManyToOneClass(foreignKey) %> <%= ManyToOneParameterName(foreignKey) %><%
count ++;
}
}
%> ) : base(<%
count = 0;
foreach(ColumnSchema column in SourceTable.NonKeyColumns)
{
if (count > 0) Response.Write(", ");
%><%= ParameterName(column) %><%
count ++;
}
foreach(TableKeySchema foreignKey in SourceTable.ForeignKeys)
{
if (count > 0) Response.Write(", ");
%><%= ManyToOneParameterName(foreignKey) %><%
count ++;
}
%>)
{
<% foreach(ColumnSchema column in primaryKey.ForeignKeyTable.NonKeyColumns) { %>
this.<%= MemberName(column) %> = <%= ParameterName(column) %>;
<% } %>
<% foreach (TableKeySchema foreignKey in primaryKey.ForeignKeyTable.ForeignKeys) { %>
<% if (foreignKey.PrimaryKeyTable != SourceTable) { %>
this.<%= ManyToOneName(foreignKey) %> = <%= ManyToOneParameterName(foreignKey) %>;
<% } %>
<% } %>
}
#endregion
#region Public Properties
<% foreach(ColumnSchema column in primaryKey.ForeignKeyTable.NonKeyColumns) { %>
public <%= MemberType(column) %> <%= PropertyName(column) %>
{
get { return <%= MemberName(column) %>; }
set { <%= MemberName(column) %> = value; }
}
<% } %>
<% foreach(TableKeySchema foreignKey in primaryKey.ForeignKeyTable.ForeignKeys) { %>
<% if (foreignKey.PrimaryKeyTable != SourceTable) { %>
public <%= ManyToOneClass(foreignKey) %> <%= ManyToOneName(foreignKey) %>
{
get { return <%= ManyToOneMemberName(foreignKey) %>; }
set { <%= ManyToOneMemberName(foreignKey) %> = value; }
}
<% } %>
<% if ((foreignKey.ForeignKeyTable == primaryKey.ForeignKeyTable) && (foreignKey.PrimaryKeyTable == primaryKey.ForeignKeyTable)) { %>
public <%= CollectionType(foreignKey) %> <%= CollectionName(foreignKey) %>
{
get { return <%= CollectionName(foreignKey) %>; }
set { <%= CollectionName(foreignKey) %> = value; }
}
<% } %>
<% } %>
#endregion
}
#endregion
<% } %>
<% } %>
}
#endregion
<!-- #include file="NHibernate.inc" -->
}
在例程包中的“其他”目录中,有完整的生成模板。
二、集成环境配置
修改web.config,进行Spring.net和nHibernate.Search的配置:
<configSections>
<sectionGroup name="spring">
<section name="typeAliases" type="Spring.Context.Support.TypeAliasesSectionHandler, Spring.Core"/>
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
<section name="nhs-configuration" type="NHibernate.Search.Cfg.ConfigurationSectionHandler, NHibernate.Search" requirePermission="false"/>
<section name="SpringOverrideProperty" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<SpringOverrideProperty>
<add key="DbProvider.ConnectionString" value="Data Source=(local);Database=LuceneDemo;User ID=sa;Password=system;Trusted_Connection=False"/>
<add key="SystemInit.IsDebug" value="true"/>
<add key="SystemInit.Level" value="4"/>
</SpringOverrideProperty>
<!--nHibernate.Search-->
<nhs-configuration xmlns="urn:nhs-configuration-1.0">
<search-factory sessionFactoryName="NHibernateSessionFactory">
<property name="hibernate.search.default.directory_provider">
NHibernate.Search.Store.FSDirectoryProvider,NHibernate.Search
</property>
<property name="hibernate.search.default.indexBase">~/Index</property>
<property name="hibernate.search.default.indexBase.create">true</property>
</search-factory>
</nhs-configuration>
<!-- Spirng.Net 配置 -->
<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>
<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>
</parsers>
<context>
<resource uri="config://spring/objects"/>
<resource uri="~/config/appDaoContext.xml"/>
<resource uri="~/config/business.xml"/>
<resource uri="~/config/controllers.xml"/>
<resource uri="~/config/pageConfig.xml"/>
<resource uri="~/config/serviceConfig.xml"/>
</context>
<objects xmlns="http://www.springframework.net"/>
</spring>
此处的"hibernate.search.default.indexBase"值"~/Index"可以更改为您希望的目录名称。这里的意义在于,当监控到数据变化时,会将索引变化写入到"~/Index/实体名/“。
在nHibernate 2.0中,加入了对数据库事件的监控,在配置文件中,给集成环境中的SessionFactory加上"EventListeners" 属性,便可轻松捕捉到诸如Insert、Update、Delete等事件,交由nHibernate.Search进行索引处理:
<!--SessionFactory对象,其中包括一些比较重要的属性 -->
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate20">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>lucenedemo.model</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect"
value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
<entry key="show_sql" value="false" />
<entry key="hibernate.current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate20"/>
<entry key="hibernate.query.factory_class" value="NHibernate.Hql.Classic.ClassicQueryTranslatorFactory" />
<entry key="cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache" />
<entry key="relativeExpiration" value="5" />
<!--2.显式启用二级缓存-->
<entry key="cache.use_second_level_cache" value="true"/>
<!--4.启动查询缓存-->
<entry key="cache.use_query_cache" value="true"/>
<entry key='hibernate.search.default.directory_provider' value='NHibernate.Search.Store.FSDirectoryProvider,NHibernate.Search' />
<entry key='hibernate.search.default.indexBase' value='~/Index'/>
<entry key='hibernate.search.default.indexBase.create' value='true' />
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
<!--events-->
<property name="EventListeners">
<dictionary>
<entry key="PostInsert">
<object type="NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search" />
</entry>
<entry key="PostUpdate">
<object type="NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search" />
</entry>
<entry key="PostDelete">
<object type="NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search" />
</entry>
</dictionary>
</property>
</object>
三、全文搜索
给数据访问模板DaoTemplate添加一个全文搜索的方法:
/// 全文检索
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="query">关键词</param>
/// <returns></returns>
IList
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:
随笔写得很粗糙,大部分细节在附件的例程包中(包括数据库备份,模板,项目文件),如果有兴趣,还是参考实例吧:
欢迎转载,但必须保留文章的署名老莫的帐本子
并保留此链接:http://moye.cnblogs.com/
如有疑问请发邮件:moyerock@gmail.com