如何解决选择性地从Sitecore的Lucene搜索索引中排除项目-使用IndexViewer进行重建时有效,但使用Sitecore的内置工具时则无效
| 在由Sitecore 6.2驱动的网站上,我需要为用户提供从搜索结果中选择性排除项目的功能。 为此,我添加了一个名为“包含在搜索结果中”的复选框字段,并创建了一个自定义数据库搜寻器来检查该字段的值: 〜\\ App_Config \\ Include \\ Search Indexes \\ Website.config:<search>
<configuration type=\"Sitecore.Search.SearchConfiguration,Sitecore.Kernel\" singleInstance=\"true\">
<indexes hint=\"list:AddIndex\">
<index id=\"website\" singleInstance=\"true\" type=\"Sitecore.Search.Index,Sitecore.Kernel\">
...
<locations hint=\"list:AddCrawler\">
<master type=\"MyProject.Lib.Search.Indexing.CustomCrawler,MyProject\">
...
</master>
<!-- Similar entry for web database. -->
</locations>
</index>
</indexes>
</configuration>
</search>
〜\\ Lib \\ Search \\ Indexing \\ CustomCrawler.cs:
using Lucene.Net.Documents;
using Sitecore.Search.Crawlers;
using Sitecore.Data.Items;
namespace MyProject.Lib.Search.Indexing
{
public class CustomCrawler : DatabaseCrawler
{
/// <summary>
/// Determines if the item should be included in the index.
/// </summary>
/// <param name=\"item\"></param>
/// <returns></returns>
protected override bool IsMatch(Item item)
{
if (item[\"include in search results\"] != \"1\")
{
return false;
}
return base.IsMatch(item);
}
}
}
有趣的是,如果我使用Index Viewer应用程序重建索引,则一切正常。未选中“包括在搜索结果中”复选框的项目将不包括在搜索索引中。
但是,当我在Sitecore控制面板应用程序中使用搜索索引重建器时,或者当IndexingManager自动更新搜索索引时,所有项目都会被包括在内,无论其“包括在搜索结果中”复选框的状态如何。
我还在自定义搜寻器类中设置了多个断点,当我使用内置索引器重建搜索索引时,应用程序从不命中任何一个。当我使用Index Viewer时,它确实会达到我设置的所有断点。
如何获得Sitecore的内置索引编制过程来尊重我的“包含在搜索结果中”复选框?
解决方法
昨天我与Alex Shyba进行了交谈,我们能够弄清楚发生了什么。我的配置存在一些问题,导致一切无法正常工作:
正如Seth所指出的,Sitecore中有两个不同的搜索API。我的配置文件同时使用了它们。要使用较新的API,只需设置
sitecore/search/configuration
部分(除了我在OP中发布的内容外,我还在sitecore/indexes
和ѭ4adding中添加了索引,这是不正确的)。
我应该覆盖AddItem()
,而不是覆盖IsMatch()
。由于Lucene的工作方式,您无法就地更新文档。相反,您必须先删除它,然后添加更新的版本。
当Sitecore.Search.Crawlers.DatabaseCrawler.UpdateItem()
运行时,它将检查IsMatch()
以查看是否应删除并重新添加该项目。如果IsMatch()
返回false,则即使该项目本来不应该从索引中删除也不会被删除。
通过覆盖ѭ6,我可以指示搜寻器是否应在删除现有文档后将其添加到索引中。这是更新后的类的样子:
〜\\ Lib \\ Search \\ Indexing \\ CustomCrawler.cs:
using Sitecore.Data.Items;
using Sitecore.Search;
using Sitecore.Search.Crawlers;
namespace MyProject.Lib.Search.Indexing
{
public class CustomCrawler : DatabaseCrawler
{
protected override void AddItem(Item item,IndexUpdateContext context)
{
if (item[\"include in search results\"] == \"1\")
{
base.AddItem(item,context);
}
}
}
}
Alex还指出,我的某些可伸缩性设置不正确。特别:
InstanceName
设置为空,这可能在临时(云)实例上引起问题,在这些实例上计算机名称可能在两次执行之间更改。我们在每个实例上将此设置更改为具有恒定且不同的值(例如CMS
和CD
)。
Indexing.ServerSpecificProperties
设置必须为true
,以便每个实例保留其上一次更新其搜索索引的时间的记录。
EnableEventQueues
设置必须为true
,以防止搜索索引编制和高速缓存刷新过程之间的争用情况。
开发时,应将Indexing.UpdateInterval
设置为相对较小的值(例如00:00:15
)。这不适用于生产环境,但是可以减少对搜索索引问题进行故障排除时的等待时间。
确保为每个Web数据库(包括远程发布目标)打开了历史记录引擎:
<database id=\"production\">
<Engines.HistoryEngine.Storage>
<obj type=\"Sitecore.Data.$(database).$(database)HistoryStorage,Sitecore.Kernel\">
<param connectionStringName=\"$(id)\" />
<EntryLifeTime>30.00:00:00</EntryLifeTime>
</obj>
</Engines.HistoryEngine.Storage>
<Engines.HistoryEngine.SaveDotNetCallStack>false</Engines.HistoryEngine.SaveDotNetCallStack>
</database>
要手动重建CD实例上的搜索索引,由于无法访问Sitecore后端,我还安装了RebuildDatabaseCrawlers.aspx(来自本文)。
,我想我已经找到了解决方案。
这是来自ѭ22的有趣片段,由控制面板应用程序中的搜索索引重建器调用:
for (int i = 0; i < database.Indexes.Count; i++)
{
database.Indexes[i].Rebuild(database);
...
}
database.Indexes
包含一组of25ѭ,它们不使用数据库搜寻器来重建索引!
换句话说,内置的搜索索引器在重建完全忽略ѭ26中的搜索配置设置的搜索索引时会使用完全不同的类。
要解决此问题,我更改了以下文件:
〜\\ App_Config \\ Include \\ Search Indexes \\ Website.config:
<indexes>
<index id=\"website\" ... type=\"MyProject.Lib.Search.Indexing.CustomIndex,MyProject\">
...
</index>
...
</indexes>
〜\\ Lib \\ Search \\ Indexing \\ CustomIndex.cs:
using Sitecore.Data;
using Sitecore.Data.Indexing;
using Sitecore.Diagnostics;
namespace MyProject.Lib.Search.Indexing
{
public class CustomIndex : Index
{
public CustomIndex(string name)
: base(name)
{
}
public override void Rebuild(Database database)
{
Sitecore.Search.Index index = Sitecore.Search.SearchManager.GetIndex(Name);
if (index != null)
{
index.Rebuild();
}
}
}
}
此方法的唯一警告是它将为每个数据库重建索引,而不仅仅是为选定数据库重建索引(我想这就是为什么Sitecore有两种完全独立的重建索引方法)的原因。
, Sitecore 6.2使用旧的和较新的搜索API,因此我相信索引的构建方式有所不同。 CMS 6.5(即将发布)仅使用较新的api-例如Sitecore.Search
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。