使用 Umbraco 从索引中获取类型化文档

Get typed document from an index using Umbraco

我会尝试什么

内容树中有很多人,我将为此创建一个新索引。这是为了提高 Web 应用程序在搜索特定人员时的性能。


创建索引

我在 Umbraco 7.7 的检查管理器中创建了一个名为 PersonIndexer 的新索引来索引所有人员。这仅包括一个人的节点类型。

为此,我编写了以下代码:

ExamineSettings.config

文件内 ExamineSettings.configExamineExamineIndexProvidersproviders 标签:

<add name="PersonsIndexer" 
     type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine" 
     supportUnpublished="false"
     supportProtected="true" 
     indexSet="Persons"
     analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>

在同一个文件中我也添加了这个但是在 ExamineExamineSearchProvidersproviders 标签中:

<add name="PersonsSearcher" 
     type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" 
     supportUnpublished="false"
     supportProtected="true" 
     indexSet="Persons"
     analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>

ExamineIndex.config

ExamineLuceneIndexSets 标签内的文件 ExamineIndex.config 内:

<IndexSet SetName="Persons" IndexPath="~/App_Data/TEMP/ExamineIndexes/Persons/" >
    <IndexAttributeFields>
        <add Name="knowledge" />
        <add Name="photo" />
        <add Name="name"/>
        <add Name="firstName"/>
        <add Name="lastName"/>
    </IndexAttributeFields>

    <IncludeNodeTypes>
        <add Name="person" />
    </IncludeNodeTypes>
</IndexSet>

获取文件

当我建立这个索引时,索引中有 7 个文档。

如何在我的视图中获取所有这些文档。我试过这段代码:

var indexer = ExamineManager.Instance.IndexProviderCollection["PersonsIndexer"];

这给了我该索引的所有属性。


问题

这不是我需要的。所以我的问题是:如何从该索引中获取类型化的文档?


编辑

@Marks 回答后,我尝试了他的代码,但是当我观看 searchResults 时,我得到了这个:

当我对 * 进行 Lucene 搜索时,我得到了 7 个结果。

当我在一个空字符串上进行文本搜索 xor I text xor lucene 搜索时,我一无所获。

您需要使用搜索器来检索文档。

这是我用来从索引中检索文档的一些代码示例,希望对您有所帮助。

var searcher = ExamineManager.Instance.SearchProviderCollection["PersonsSearcher"];
var searchCriteria = searcher.CreateSearchCriteria();
var query = searchCriteria.Field("nodeTypeAlias", "person").Compile();
var searchResults = searcher.Search(query);

编辑

我已经查看了我的 ExamineIndex.config,您必须在 XML.

中分别定义默认的 Umbraco 属性和用户定义的属性

我的一个小例子CustomIndexSet

<IndexSet SetName="CustomIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/Custom/">
    <IndexAttributeFields>
      <add Name="id" />
      <add Name="nodeName"/>
      <add Name="updateDate" />
      <add Name="nodeTypeAlias" />
    </IndexAttributeFields>
    <IndexUserFields>
      <add Name="category" />
      <add Name="title" />
      <add Name="description" />
      <add Name="status" />
      <add Name="keywords" />
    </IndexUserFields>
    <IncludeNodeTypes>
      <add Name="item"/>
      <add Name="category"/>
    </IncludeNodeTypes>
</IndexSet>