Avoid search queries in page Quick view
Avoid search queries in page Quick view
With the FetchPageContent
or the HTMLContentInBodyWithRequest
processors active, Coveo for Sitecore performs additional web requests when indexing Sitecore items to get the item page content, which is essentially the item as seen by a visitor.
When the layout contains search-driven components, it may send a lot of unnecessary search queries to the search index.
The purpose of this article is to explain how search queries can be disabled when the item is visited by Coveo to index the page content.
To do so, you need to add a condition in the search-driven component to detect whether the page request comes from a Coveo agent or not.
Coveo for Sitecore always uses the Coveo Sitecore Search Provider
user agent to render the Quick view of your pages, making it easy to create a condition to ignore your search-driven components in your Quick view.
It’s best to avoid rendering the associated component completely when possible.
If the component is rendered without data, messages like no data available
might be indexed with the item.
The best way to skip the search queries depends heavily on the implementation. The following sections present solutions for common scenarios.
You have a site that presents various products. For each product, there’s a small box for the related news. The Related news component is search-driven and uses the Search Provider API (via LINQ) to return its results.
Every time the search page is accessed, a search query is performed to get the news.
To solve the issue, you modify the search-driven component to detect if the page request comes from Coveo before filling the Quick view.
Prevent component rendering
The first thing to do is to detect if the request is made by Coveo so as to skip the query.
It’s important to set the SkipRendering
property so the view can also stop rendering.
SkipRendering = HttpContext.Current.Request.UserAgent.Contains("Coveo Sitecore Search Provider");
Therefore, for the previous example, the code of the model that populates the MVC view would look like this:
RelatedNews.cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Mvc.Presentation;
namespace Tutorial.Models
{
public class RelatedNews : IRenderingModel
{
public bool SkipRendering { get; private set; }
public RelatedNews()
{
SkipRendering = false;
}
public void Initialize(Rendering p_Rendering)
{
SkipRendering = HttpContext.Current.Request.UserAgent.Contains("Coveo Sitecore Search Provider");
}
}
}
You then need to stop the rendering process by adding a SkipRendering
property.
@if (Model.SkipRendering) {
return;
}
Therefore, for the same example, the code for the associated view would look like this:
RelatedNews.cshtml
@using Sitecore.Mvc
@using Sitecore.ContentSearch
@using Sitecore.ContentSearch.Linq
@using Sitecore.ContentSearch.SearchTypes
@model Tutorial.Models.RelatedNews
@if (Model.SkipRendering) {
return;
}
<div id="relatednews">
<h2>Related news</h2>
// This is where you need to insert your component code.
</div>