Umbraco 检查搜索查询空值
Umbraco Examine Search query null value
我正在尝试修复我的搜索引擎。问题是,当我搜索我现有的博客项目时,它总是显示
Did not match in any document
无论我搜索什么,它总是给我错误信息。
有什么办法可以解决这个问题,或者我的代码有问题吗?
@using Examine.LuceneEngine.SearchCriteria
@inherits Umbraco.Web.Macros.PartialViewMacroPage
string searchTerm = Request.QueryString["search"];
var searcher = ExamineManager.Instance.SearchProviderCollection["MySiteSearcher"];
var searchCriteria = searcher.CreateSearchCriteria();
var query = searchCriteria.Field("nodeName", searchTerm).Or().Field("bodyText", searchTerm).Compile();
var searchResults = searcher.Search(query);
@if (searchResults.Any()){
<ul class="search-results">
@foreach (var result in searchResults)
{
IPublishedContent node = Umbraco.Content(result.Fields["id"]);
<li>
<a href="@umbraco.library.NiceUrl(result.Id)">
@if (result.Fields.ContainsKey("nodeName")) {
@node.GetPropertyValue("blogItemTitle")
}
</a>
<p>@result.Fields["blogbodyText"]</p>
</li>
}
</ul>
} else {
<p class="error-result">
Your search
@if (!String.IsNullOrEmpty(searchTerm))
{
<strong><text>'@searchTerm'</text></strong>
}
did not match any documents.
</p>
}
这是我的设置
<?xml version="1.0"?>
<!--
Umbraco examine is an extensible indexer and search engine.
This configuration file can be extended to add your own search/index providers.
Index sets can be defined in the ExamineIndex.config if you're using the standard provider model.
More information and documentation can be found on CodePlex: http://umbracoexamine.codeplex.com
-->
<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 unpublished pages-->
<add name="ExternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"/>
<add name="MySiteIndexer" type="UmbracoExamine.UmbracoMemberIndexer, UmbracoExamine"
supportUnpublished="false"
supportProtected="true"
interval="10"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
enableDefaultEventHandler="true"/>
</providers>
</ExamineIndexProviders>
<ExamineSearchProviders defaultProvider="ExternalSearcher">
<providers>
<add name="InternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>
<add name="InternalMemberSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" enableLeadingWildcard="true"/>
<add name="ExternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" />
<add name="MySiteSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
enableLeadingWildcards="true"
indexSet="MySiteIndexSet"/>
</providers>
</ExamineSearchProviders>
</Examine>
ExamineIndex.config
<?xml version="1.0"?>
<!--
Umbraco examine is an extensible indexer and search engine.
This configuration file can be extended to create your own index sets.
Index/Search providers can be defined in the UmbracoSettings.config
More information and documentation can be found on CodePlex: http://umbracoexamine.codeplex.com
-->
<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" EnableSorting="true"/>
<add Name="name" EnableSorting="true"/>
<add Name="updateDate" />
<add Name="writerName" />
<add Name="loginName" />
<add Name="email" />
<add Name="bodyText"/>
<add Name="blogItemTitle" />
<add Name="nodeTypeAlias" />
</IndexAttributeFields>
<IndexUserFields>
<add Name="bodyText"/>
<add Name="tags"/>
<add Name="content"/>
<add Name="tags" />
<add Name="category" />
</IndexUserFields>
<IncludeNodeTypes>
<add Name="article" />
<add Name="note" />
<add Name="KnowledgebaseItem" />
</IncludeNodeTypes>
</IndexSet>
<!-- Default Indexset for external searches, this indexes all fields on all types of nodes-->
<IndexSet SetName="ExternalIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/External/" />
<IndexSet SetName="MySiteIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/MySiteSearcher/" />
</ExamineLuceneIndexSets>
需要检查几件事,首先,检查您的索引是否确实正确填充。您可以从 here 下载工具 Luke(需要 Java)。这个方便的工具可让您查看索引的内容。
如果您启动 Luke 并将其指向 /App_Data/TEMP/ExamineIndexes/MySiteSearcher 中的索引文件夹,它应该会显示索引器中有多少项目以及它们包含哪些字段。检查 a) 索引中是否有文档,以及 b) 索引是否正在索引您正在搜索的字段。
如果一切正常,可能是搜索语法问题。查看您的代码,您将以要求 nodeName 匹配搜索词的搜索结束,在 bodyText 中有一个可选的匹配(默认搜索运算符是 AND,这意味着查询中的第一项必须始终匹配).如果你想匹配其中任何一个,你需要将搜索的默认运算符设置为或,使用以下代码:
var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
我正在尝试修复我的搜索引擎。问题是,当我搜索我现有的博客项目时,它总是显示
Did not match in any document
无论我搜索什么,它总是给我错误信息。
有什么办法可以解决这个问题,或者我的代码有问题吗?
@using Examine.LuceneEngine.SearchCriteria
@inherits Umbraco.Web.Macros.PartialViewMacroPage
string searchTerm = Request.QueryString["search"];
var searcher = ExamineManager.Instance.SearchProviderCollection["MySiteSearcher"];
var searchCriteria = searcher.CreateSearchCriteria();
var query = searchCriteria.Field("nodeName", searchTerm).Or().Field("bodyText", searchTerm).Compile();
var searchResults = searcher.Search(query);
@if (searchResults.Any()){
<ul class="search-results">
@foreach (var result in searchResults)
{
IPublishedContent node = Umbraco.Content(result.Fields["id"]);
<li>
<a href="@umbraco.library.NiceUrl(result.Id)">
@if (result.Fields.ContainsKey("nodeName")) {
@node.GetPropertyValue("blogItemTitle")
}
</a>
<p>@result.Fields["blogbodyText"]</p>
</li>
}
</ul>
} else {
<p class="error-result">
Your search
@if (!String.IsNullOrEmpty(searchTerm))
{
<strong><text>'@searchTerm'</text></strong>
}
did not match any documents.
</p>
}
这是我的设置
<?xml version="1.0"?>
<!--
Umbraco examine is an extensible indexer and search engine.
This configuration file can be extended to add your own search/index providers.
Index sets can be defined in the ExamineIndex.config if you're using the standard provider model.
More information and documentation can be found on CodePlex: http://umbracoexamine.codeplex.com
-->
<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 unpublished pages-->
<add name="ExternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"/>
<add name="MySiteIndexer" type="UmbracoExamine.UmbracoMemberIndexer, UmbracoExamine"
supportUnpublished="false"
supportProtected="true"
interval="10"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
enableDefaultEventHandler="true"/>
</providers>
</ExamineIndexProviders>
<ExamineSearchProviders defaultProvider="ExternalSearcher">
<providers>
<add name="InternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>
<add name="InternalMemberSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" enableLeadingWildcard="true"/>
<add name="ExternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" />
<add name="MySiteSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
enableLeadingWildcards="true"
indexSet="MySiteIndexSet"/>
</providers>
</ExamineSearchProviders>
</Examine>
ExamineIndex.config
<?xml version="1.0"?>
<!--
Umbraco examine is an extensible indexer and search engine.
This configuration file can be extended to create your own index sets.
Index/Search providers can be defined in the UmbracoSettings.config
More information and documentation can be found on CodePlex: http://umbracoexamine.codeplex.com
-->
<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" EnableSorting="true"/>
<add Name="name" EnableSorting="true"/>
<add Name="updateDate" />
<add Name="writerName" />
<add Name="loginName" />
<add Name="email" />
<add Name="bodyText"/>
<add Name="blogItemTitle" />
<add Name="nodeTypeAlias" />
</IndexAttributeFields>
<IndexUserFields>
<add Name="bodyText"/>
<add Name="tags"/>
<add Name="content"/>
<add Name="tags" />
<add Name="category" />
</IndexUserFields>
<IncludeNodeTypes>
<add Name="article" />
<add Name="note" />
<add Name="KnowledgebaseItem" />
</IncludeNodeTypes>
</IndexSet>
<!-- Default Indexset for external searches, this indexes all fields on all types of nodes-->
<IndexSet SetName="ExternalIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/External/" />
<IndexSet SetName="MySiteIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/MySiteSearcher/" />
</ExamineLuceneIndexSets>
需要检查几件事,首先,检查您的索引是否确实正确填充。您可以从 here 下载工具 Luke(需要 Java)。这个方便的工具可让您查看索引的内容。
如果您启动 Luke 并将其指向 /App_Data/TEMP/ExamineIndexes/MySiteSearcher 中的索引文件夹,它应该会显示索引器中有多少项目以及它们包含哪些字段。检查 a) 索引中是否有文档,以及 b) 索引是否正在索引您正在搜索的字段。
如果一切正常,可能是搜索语法问题。查看您的代码,您将以要求 nodeName 匹配搜索词的搜索结束,在 bodyText 中有一个可选的匹配(默认搜索运算符是 AND,这意味着查询中的第一项必须始终匹配).如果你想匹配其中任何一个,你需要将搜索的默认运算符设置为或,使用以下代码:
var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);