There is a requirement to show the number of page views count for every each article in Coveo Search results page. We have created a Computed field to get the Page views count. This value is coming up for every Coveo index and we thought that this is working as expected.

After some days we found that the values are not updating even though there are number of page views are happening by end users. We came to know that the computed field is getting the updated values whenever there is a Sitecore item having updates with publishing.

What about other Sitecore items having no updates in the item? Here we started to do some research on how to achieve it.

First we looked at any Meta data having page views count for every search results from Coveo. Later we found that there is no such Meta data available. So we started to think for other options and we finally found that there is an option to refresh the Coveo Index programmatically.

As we need to get the latest page views counts near real time data, we thought it to create a schedule task which runs for every 4 hours to trigger the Coveo index.

So the below steps we have followed

  • Create a Sitecore command which triggers Coveo index for some Sitecore items only
  • Schedule that command in Sitecore task schedule

How to create a Sitecore Command and Schedule

The below one is the sample code.

using Coveo.SearchProvider;

using Sitecore.ContentSearch;

using Sitecore.Data;

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using System;

using System.Linq;

namespace Coveo.Index

{

public class SampleCoveoIndexRefresh

{

public void Execute(Item[] items, Sitecore.Tasks.CommandItem command, Sitecore.Tasks.ScheduleItem schedule)

{

Sitecore.Diagnostics.Log.Info(“SampleCoveoIndexRefresh started”, this);

ISearchIndex index = ContentSearchManager.GetIndex(“Coveo_web_index”);

IProviderIndex providerIndex = index as IProviderIndex;

string databaseName = providerIndex.Database;

Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase(databaseName);

if (items != null && items.Any())

{

using (new DatabaseSwitcher(Sitecore.Data.Database.GetDatabase(databaseName)))                {

foreach (var rootFolder in items)

{

try

{

Item item = database.GetItem(rootFolder.ID);

// Another option to sync refresh here.

// Making Async refresh here.

 

index.RefreshAsync(new SitecoreIndexableItem(item), IndexingOptions.Default, new System.Threading.CancellationToken(false));

}

catch (Exception ex)

{

Log.Error(“Error Occured while doing SampleCoveoIndexRefresh.” + ex.Message, this);

}

}

}

}

else

Log.Error(“Root folder paths are missed in SampleCoveoIndexRefresh Job. Please add them.”, this);

}

}

}

The next step is create a Sitecore Command and add the type information

Next step is create a Sitecore schedule by using the command. I have added 2 folder name paths separated by symbol.

I wanted to trigger Coveo index only for 2 paths and not all Sitecore items. This reduces the Coveo index refresh time.

That”s all. It triggers for every 4 hours to get the latest page views counts for every page.

 


Leave a Reply

Your email address will not be published. Required fields are marked *