Why do we need Computed Indexed Field?
Computed fields are a great way of storing calculated information while Sitecore indexes your items as opposed to using more resource to calculate it on the fly for use in your sublayouts/layouts.
Coding:-
- Create a class which implements AbstractComputedIndexField
- Use the method Sitecore.ContentSearch.ComputedFields namespace, then override the ComputeFieldValue method.
- Create a config-include file which adds the computed index field to the index configuration.Here I will using the same example used in “Uli” Blogs, but I have made the changes code and config file so that it can work in Sitecore 8.
- Author Template
- Publication Template
Below code will create the IndexField name “AuthorPublications”. And this field will contain all the Publication ID related to the Author.
public class AuthorPublications : AbstractComputedIndexField
{
public override object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
if (item == null)
return null;
if (item.TemplateName != “Author”)
return null;
return GetPublicationIDs(item);
}private IEnumerable<Guid> GetPublicationIDs(Item authorItem)
{
return (from link in Globals.LinkDatabase.GetItemReferrers(authorItem, false)
let sourceItem = link.GetSourceItem()
where sourceItem != null
where sourceItem.TemplateName == “Publication”
select sourceItem.ID.Guid).ToArray();
}public string FieldName
{
get;
set;
}
public string ReturnType
{
get;
set;
}
}Create a new Config file “ComputedIndexFields.config” and add the namespace & DLL name in the field
<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”>
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration type=”Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider”>
<fields hint=”raw:AddComputedIndexField”>
<field fieldName=”AuthorPublications” storageType=”YES” indexType=”TOKENIZED”>Namespace.AuthorPublications, DLL Name </field>
</fields>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>After adding the file in App_config/Include folder, Kindly check in Sitecore\admin\Showconfig.aspx
Debugging using Visual Studio.
1) Open the Visual Studio -> Attached the debugger.
2) In Desktop-> Control Panel-> Indexing Manager-> Rebuild the Search Index.
Also in Luke you can find the field
-
-
References
https://reasoncodeexample.com/2014/04/01/computed-index-fields-sitecore-7-content-search/