SaaS CMS has officially launched! Learn more now.

Praful Jangid
Apr 12, 2019
  9897
(12 votes)

Indexing Block's Content to make it searchable

By default, the content of a block (that is added to ContentArea on a page) is not indexed and therefore you can’t search for the content of that block instance in your site.

So, the content of a page (including block's content) is indexed as normal text under "SearchText$$string". To check if the content from Block is being indexed or not. Go to Episerver CMS, Find -> Overview -> Explore, look for the page you want to confirm. Expand to see all its indexed properties. You can see here that, by default the content of the block is not being indexed.

Let's talk about how to enable it. Here we go....

There are several ways to do that, use any of them as per your requirement.

  1. To allow content of all the instances of a block type to be indexed. Just add an attribute [IndexInContentArea] on your block.
    [IndexInContentAreas]
    public class CopyBlock : SectionBlock

    Tip: If you still not see the content is being indexed. Perform some changes to your page and you will see it worked. It's only to reindex the page.

  2. To exclude content of certain instances of the block, add a bool type property with exact name IndexInContentAreas on your block type.

    public virtual bool IndexInContentAreas { get; set; }

    And set its value to True, so that content of all instances will be indexed by default. To exclude content of any instance, uncheck the checkbox for property IndexInContentAreas (for that instance only).

  3. Using search convention, changing the default behaviour of IContentIndexerConventions.ShouldIndexInContentAreaConvention.

    To index a particular block type, create a class and inherit it with interface IShouldIndexInContentAreaConvention

    public class ShouldIndexInContentAreaConvention : IShouldIndexInContentAreaConvention
    {
           public bool? ShouldIndexInContentArea(IContent content)
           {
           	return content is CopyBlock;
           }
    }
    

     Now create an Initializable module

    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Find.Cms.Module.IndexingModule))]
    public class SearchConventionInitializationModule : IInitializableModule
    {
            public void Initialize(InitializationEngine context)
            {
                ContentIndexer.Instance.Conventions.ShouldIndexInContentAreaConvention = new ShouldIndexInContentAreaConvention();
            }
    
            public void Uninitialize(InitializationEngine context)
            {
            }
    }
    

    In addition, to control the depth of ContentArea to be indexed, can be controlled by MaxDepthContentAreaConverter (by default all nested ContentArea are indexed)

    public class SearchConventionInitializationModule : IInitializableModule
    {
            private const int MaxDepth = 4;
    
            public void Initialize(InitializationEngine context)
            {
                ContentIndexer.Instance.Conventions.ShouldIndexInContentAreaConvention = new ShouldIndexInContentAreaConvention();
                SearchClient.Instance.Conventions.ForInstancesOf<ContentArea>().ModifyContract(x => x.Converter = new MaxDepthContentAreaConverter(MaxDepth));
            }
    
            public void Uninitialize(InitializationEngine context)
            {
            }
    }

Document reference: https://world.episerver.com/documentation/developer-guides/find/Integration/episerver-cms-7-5-with-updates/Indexing-content-in-a-content-area/

Happy Coding :)

Apr 12, 2019

Comments

KennyG
KennyG Apr 12, 2019 08:21 PM

I see you mention all instances of a block type. Do you have to do this per each block type or can you easily enable for ALL blocktypes?

Praful Jangid
Praful Jangid Apr 13, 2019 04:30 AM

Hi Kenny,

You must be talking about the first one? If yes, all instances means where-ever (contentarea on any page) you create a block using that block type, it will be indexed for that page. You will not have control over it. You will not be able to disable indexing for any particular block instance.

KennyG
KennyG Apr 15, 2019 02:12 PM

Hey Praful, I was more asking about all blocks of any type. All blocks, all types, anywhere without having to decorate each blocktype?

Praful Jangid
Praful Jangid Apr 17, 2019 08:33 AM

Hey Kenny,

Apologies for the late reply. If you want all instances of all block to be allowed indexed by default then use the search convention and add you base block or BlockData into the class

ShouldIndexInContentAreaConvention 

(as in above example 3).

Thanks & Regards

/Praful Jangid

Kane Made It
Kane Made It Feb 10, 2020 08:50 AM

This article is helpful. We're working on that also. Thanks alot Praful.

Praful Jangid
Praful Jangid May 5, 2020 03:06 PM

Thanks Kane, glad you like it.

Avinash
Avinash May 11, 2020 06:20 AM

Hey Praful,

It is really helpful for me. I was looking for such a type of article where we can index the block.

Avinash

Sunil
Sunil Oct 12, 2022 08:10 AM

Nice article and very useful.

Thank you for sharing, keep doing great work.

Please login to comment.
Latest blogs
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024