Hi,
Can anyone spot where I'm going wrong here. I've got a custom module that has two parts:
- A product admin page for adding product details that is accessed using an link in the administration page
- A product list control that links to a details page, which I've used the internal url manager to create search engine friendly urls to
In order to get the products searchable I've created an IndexBuilderProvider which, in essence, seems to work fine. Here's the code for the RebuildIndex routine:
public override void RebuildIndex(PageSettings pageSettings, string indexPath)
{
if (WebConfigSettings.DisableSearchIndex) { return; }
if ((pageSettings == null) || (indexPath == null))
{
return;
}
//don't index pending/unpublished pages
if (pageSettings.IsPending) { return; }
Guid featureGuid = new Guid("9164051a-7335-447a-90e1-37f73bd858e7");
ModuleDefinition productListFeature = new ModuleDefinition(featureGuid);
// get the list of modules on the site using the feature, although in all likelyhood there'll only be one
List<int> moduleIds = new List<int>();
using (IDataReader rdr = Module.GetModulesForSite(pageSettings.SiteId, featureGuid))
{
while (rdr.Read())
{
moduleIds.Add(Convert.ToInt32(rdr["ModuleId"]));
}
}
foreach (int modId in moduleIds)
{
Module mod = new Module(modId);
// get the pageModules using the Module
List<PageModule> pageModules = PageModule.GetPageModulesByPage(pageSettings.PageId);
foreach (PageModule pageModule in pageModules)
{
// cehck if the pageModule matches our custom module id
if (pageModule.ModuleId == modId)
{
log.Info("CSafeProductsIndexBuilderProvider indexing page - " + pageSettings.PageName);
// get the list of active products showing on the list page
List<Product> allProds = ProductManager.GetHomeProducts();
// for each product create an item index for the detail page
foreach (Product product in allProds)
{
IndexItem indexItem = new IndexItem()
{
SiteId = pageSettings.SiteId,
PageId = pageSettings.PageId,
PageName = pageSettings.PageName,
ViewRoles = pageSettings.AuthorizedRoles,
ViewPage = product.Url,
UseQueryStringParams = false,
FeatureName = productListFeature.FeatureName,
FeatureResourceFile = productListFeature.ResourceFile,
ItemId = product.Id,
ModuleId = modId,
ModuleTitle = mod.ModuleTitle,
Title = product.Name,
Content = product.IntroText + " " + product.DetailText,
PublishBeginDate = pageModule.PublishBeginDate,
PublishEndDate = pageModule.PublishEndDate,
FeatureId = featureGuid.ToString()
};
if (product.Deleted)
IndexHelper.RemoveIndexItem(pageSettings.PageId, modId, product.Id);
else
IndexHelper.RebuildIndex(indexItem);
log.Info(String.Format("CSafeProductsIndexBuilderProvider indexing pageId {0}, moduleId {1}, productid {2}", pageSettings.PageId, modId, product.Id));
}
// Create a single indexItem for the Product List page (use zero for the product id to differentiate it)
string searchContent = string.Empty;
allProds.FindAll(x => !x.Deleted).ForEach(x => searchContent += String.Format("{0} {1} ", x.Name, x.ShortDescription));
IndexItem homeItem = new IndexItem()
{
SiteId = pageSettings.SiteId,
PageId = pageSettings.PageId,
PageName = pageSettings.PageName,
ViewRoles = pageSettings.AuthorizedRoles,
ViewPage = pageSettings.Url.Replace("~/", string.Empty),
UseQueryStringParams = false,
FeatureName = productListFeature.FeatureName,
FeatureResourceFile = productListFeature.ResourceFile,
ItemId = 0,
ModuleId = modId,
ModuleTitle = mod.ModuleTitle,
Title = mod.ModuleTitle,
Content = searchContent,
PublishBeginDate = pageModule.PublishBeginDate,
PublishEndDate = pageModule.PublishEndDate,
FeatureId = featureGuid.ToString()
};
IndexHelper.RebuildIndex(homeItem);
log.Info(String.Format("CSafeProductsIndexBuilderProvider indexing pageId {0}, moduleId {1}, productid {2}", pageSettings.PageId, modId, 0));
}
}
}
}
All seems to work fine and when I trigger the index rebuild I get the following log entries that I expect:
2013-07-11 14:19:39,446 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing page - Products
2013-07-11 14:19:39,495 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 1
2013-07-11 14:19:39,512 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 2
2013-07-11 14:19:39,529 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 4
2013-07-11 14:19:39,546 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 6
2013-07-11 14:19:39,561 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 7
2013-07-11 14:19:39,577 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 5
2013-07-11 14:19:39,594 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 3
2013-07-11 14:19:39,611 INFO (null) - (null) - (null) - CSafeProducts.BusinessLogic.CSafeProductsIndexBuilderProvider - CSafeProductsIndexBuilderProvider indexing pageId 8, moduleId 18, productid 0
These log entries only appear once, so I figure I'm only adding one index per product, but when I run a search on the product name then I get duplicate entries for both the detail page and the list page, as you can see here.
This is my first time playing with an IndexBuilderProvider so if anyone can point me in the right direction I'd really appreciate it.
Cheers, Martin.