umbraco 4.0之后的版本都继承了基于Examine和Lucene.Net的搜索功能,称之为umbraco Examine,在了解umbraco Examine之前,我们将首先对Examine做一个简单的介绍。
Examine的目的是提供一个简单的,基于provider的框架,改框架可以用来生成.net数据的索引器(Indexers)和搜索器(Searchers),Examine将索引器和搜索器分离开来,允许它们有不同的数据存储和获取。
作为一个基于provider的框架,Examine允许同时配置多个搜索器和索引器,并且保证它们之间是相互独立的。
Examine分别通过实现基类BaseIndexProvider和BaseSearchProvider来实现索引和搜索的功能。对于这两个基类我们就不作介绍了,有兴趣的读者可以去 http://examine.codeplex.com/documentation?referringTitle=Home 做进一步的了解。
Examine提供了一个单例类ExamineManager来操作注册在配置文件中的Index和Search Providers,改单例类可以返回默认的索引和搜索的providers,也可以得到所有providers的集合,同时也提供给了我们所有的关于索引和搜索的方法调用。
Examinemanager提供给我们了一个链式的搜索函数,将复杂的搜索过程简单化,例子如下所示:
以上我们简单的介绍了一下Examine的概念,接下来我们就将关注点回到UmbracoExamine上,UmbracoExamine扩展自Examine,用来搜索umbraco的数据,并且使用Lucene.Net作为相关的索引器。使用UmbracoExamine首先要配置相关的providers和LunceneIndexSets,umbraco默认的providers配置如下:<Examine> <ExamineIndexProviders> <providers> <add name="InternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine" supportUnpublished="true" supportProtected="true" interval="10" analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/> <add name="InternalMemberIndexer" type="UmbracoExamine.UmbracoMemberIndexer, UmbracoExamine" supportUnpublished="true" supportProtected="true" interval="10" analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"/> <!-- default external indexer, which excludes protected and published pages--> <add name="ExternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine" supportUnpublished="false" supportProtected="false" interval="10" analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/> </providers> </ExamineIndexProviders> <ExamineSearchProviders defaultProvider="ExternalSearcher"> <providers> <add name="InternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/> <add name="ExternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net" enableLeadingWildcards="true"/> <add name="InternalMemberSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" enableLeadingWildcards="true"/> </providers> </ExamineSearchProviders> </Examine>
我们还可以扩展自己的索引和搜索的provider,扩展自定义的索引和搜索的第一步就是定义自己的IndexSet,umbraco默认的IndexSet配置如下:
<ExamineLuceneIndexSets> <!-- The internal index set used by Umbraco back-office - DO NOT REMOVE --> <IndexSet SetName="InternalIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/Internal/" /> <!-- The internal index set used by Umbraco back-office for indexing members - DO NOT REMOVE --> <IndexSet SetName="InternalMemberIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/InternalMember/"> <IndexAttributeFields> <add Name="id" /> <add Name="nodeName"/> <add Name="updateDate" /> <add Name="writerName" /> <add Name="loginName" /> <add Name="email" /> <add Name="nodeTypeAlias" /> </IndexAttributeFields> </IndexSet> <!-- Default Indexset for external searches, this indexes all fields on all types of nodes--> <IndexSet SetName="ExternalIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/External/" /> </ExamineLuceneIndexSets>
IndexSet主要包含三个子节点,其中IndexAttributeFields包含的是节点的内置属性,IndexUserFields包含的是用户在umbraco中定义的属性,IncludeNodeTypes包含所有用于此索引的节点,还有一个节点叫做ExcludeNode,包含不能使用此索引的节点,一般设为空。如下是一个自定义的索引集:
<IndexSet SetName="CWSIndexSet" IndexPath="~/App_Data/CWSIndexSetTest" IndexParentId="-1"> <IndexAttributeFields> <add Name="id" EnableSorting="true" Type="Number" /> <add Name="nodeName" EnableSorting="true" /> <add Name="updateDate" EnableSorting="true" Type="DateTime" /> <add Name="writerName" /> <add Name="path" /> <add Name="nodeTypeAlias" /> <add Name="parentID" /> </IndexAttributeFields> <IndexUserFields> <add Name="headerText" /> <add Name="bodyText" /> <add Name="metaDescription" /> <add Name="metaKeywords" /> <add Name="bodyTextColOne" /> <add Name="bodyTextColTwo" /> <add Name="xmlStorageTest" /> </IndexUserFields> <IncludeNodeTypes> <add Name="CWS_Home" /> <add Name="CWS_Textpage" /> <add Name="CWS_TextpageTwoCol" /> <add Name="CWS_NewsEventsList" /> <add Name="CWS_NewsItem" /> <add Name="CWS_Gallery" /> <add Name="CWS_EventItem" /> <add Name="Image" /> </IncludeNodeTypes> <ExcludeNodeTypes /> </IndexSet>
设置好索引集后,就可以定义相应的Index Provider和Search Provider了,如下所示:
接下来就是调用ExamineManager里面的函数返回搜索结果了,调用的过程如下:
var Searcher = ExamineManager.Instance.SearchProviderCollection[
"CWSSearcher"
];
var searchCriteria = Searcher.CreateSearchCriteria();
var query = searchCriteria.Field(
"nodeName"
,
"hello"
).Or().Field(
"bodyText"
,
"hello"
).Compile();
关于umbraco内置的search功能就先介绍到这里了!var searchResults = Searcher.Search(query);