Moving Away From LINQ

Support for LINQ queries was dropped in Coveo for Sitecore 5 (see Release Notes for November 2018 - 5.0.74.10). This article explains the reasons for this decision and provides guidance on how to move from LINQ to Coveo queries.

Why Coveo for Sitecore No Longer Supports LINQ Queries

Coveo’s decision to discontinue support for LINQ is simply one of business focus. One needs only to compare the characteristics of LINQ and real Coveo queries to realize the extent to which LINQ was out-of-line with what Coveo stands for.

  LINQ Queries Coveo for Sitecore Hive Queries
Data Retrieval Method The Coveo index is queried through the Coveo Search API using the Sitecore Content Search API. The Coveo Hive framework queries the Coveo Search API which retrieves the data from the index.
Query Creation Hard-coded server-side. Queries are generated by the Coveo Hive Search Interface, but can be customized using several business tools or client-side using the Coveo JavaScript Framework.
Identity Management Hard-coded with a custom user authentication logic. The Coveo Hive Search Interface automatically identifies the user and sends the proper role to the Coveo index for security trimming.
Analytics Tracking The connection with the Coveo Analytics API needs to be built manually. Every Coveo Hive component sends usage analytics data to Coveo.
Machine Learning Only active if the entire analytics tracking loop is completed. Activated by default based on data sent by the components.
Focus of the solution Data retrieval. Relevance at every interaction.

In a nutshell, using LINQ queries is a completely valid way to retrieve index content. However, Coveo aims to provide users with tailored results, based on analytics and artificial intelligence models. Coveo is a relevance engine.

Other Sitecore search providers can be used to surface index data just fine, in parallel with Coveo. Using LINQ queries on a Coveo index amounts to missing out entirely on the whole value of Coveo.

Coveo for Sitecore Hive Explained

At the core of Coveo is the JavaScript Search UI Framework, a set of components typically associated with search interfaces (e.g., search box components, facet components, result list components). The component underlying logic ensures all components inserted in a web page work together. Only a few components are required to form a fully functional (and relevance-ready) search interface, right out-of-the-box.

Coveo packages the Coveo JavaScript Search UI Framework as Sitecore items and associated files under the Coveo for Sitecore Hive Framework designation. Therefore, you can seamlessly leverage the Coveo JavaScript Search UI Framework in Sitecore by inserting Coveo for Sitecore Hive renderings in your web pages, the same way you would with other renderings in Sitecore ( see Coveo for Sitecore Hive UI Framework).

Using Coveo for Sitecore Hive for Listing Pages

You get the most out of Coveo when you draw from usage analytics, and much of that data comes from user actions. Hence, using a complete search interface, including a search box and facets will generate more analytics and better relevance than simply having Coveo for Sitecore display a result list on web page loads. But even for that specific use case, Coveo for Sitecore Hive is more flexible and powerful than using LINQ.

You can add a simple Coveo for Sitecore results list in your Sitecore web page(s) to surface related items. The required Coveo results list components automatically generate and send a query to the Coveo Cloud Search API when the page finishes loading. This query contains user/visitor and context information which is leveraged by Coveo Cloud before it sends a response (search results) back to the page. These results are immediately rendered.

Result List Example

The Creating a Simple Result List page contains step-by-step instructions on adding a simple Coveo for Sitecore result list and setting up result filtering and boosting using both recommended methods.

Calling the Coveo Search API Using Server-Side Code

Although the Coveo for Sitecore Hive Framework is highly flexible, you might need to retrieve server-side data for precise use cases.

Using server-side code to fetch Coveo index data is not impossible. Coveo provides information on how to do this outside of Sitecore (see Search API). However, Coveo for Sitecore assemblies contain a client that simplifies the process of connecting to the Search API. This client allows you to generate and send a query to the Search API and then bind the query results to a model.

Be aware that, just like LINQ, using the Coveo Search API directly will not log any analytics event used to feed the Coveo Machine Learning models.

You work for the Regional Corn Growers Association and you have been tasked to add a side widget on the association web site pages displaying the upcoming corn roast events. You have set up event items in Sitecore with a field called EventType.

The following code sample generates a Search API query that contains an advanced query (aq) (see Query Parameters and Coveo Cloud Query Syntax Reference). This advanced query specifies that only items whose EventType value is set to Corn Roast must be returned.

The code samples contain fictitious values for field and security provider names. The associated comments provide instructions on how to set the appropriate variable values for your implementation.

using Coveo.Framework.SearchService;
using Coveo.SearchServiceProvider.Rest;
using Coveo.Framework.Configuration;
using Coveo.Framework.Connection;
using Coveo.SearchProvider.ContentSearch;
using Sitecore.ContentSearch;
using System.Collections.Generic;
namespace ...
private QueryResults ExecuteQuery()
{
   IClientSessionWrapperFactory factory = new ClientSessionWrapperFactory();
   ICoveoIndexFetcher coveoFetcher = new CoveoIndexFetcher();
   ISearchIndex coveoIndex = coveoFetcher.GetCoveoSearchIndex("Coveo_web_index");
   CoveoIndexConfiguration configuration = coveoIndex.Configuration as CoveoIndexConfiguration;
   /*
    * Refer to the Coveo Cloud administration console at
    * https://platform.cloud.coveo.com/admin/#content/permissions/providers/ for the securityProviderName value
    * to use for your instance of Coveo for Sitecore.
    */
    String securityProviderName = "Expanded Sitecore Security Provider for WKS-000236";
   // This example assumes that non-authenticated users have read access to the fetched items.
   String currentUserName = @"extranet\Anonymous";
   QueryParams qParams = new QueryParams();
   qParams.SortByCriteria = new []
   {
      /*
       * Use the Translated Name value of the Sitecore fields as the first parameter of the FieldSortBy constructor.
       * The Translated Name value may be found in the Indexing Manager at
       * http://[Your Sitecore Instance]/coveo/command-center/index.html#fields/.
       * Ensure the field to sort by is marked as "Sortable".
       */
       new FieldSortBy("feventdate2181", true)
   };
   /*
    * Use an advanced query (`aq`) to filter your items according to the Sitecore field of your choice. Once again,
    * use the Translated Name of the Sitecore field in your query.
    */
   qParams.AdvancedExpression = "@feventtype2181=\"Corn Roast\"";
   IClientSessionWrapper client = factory.GetClientSessionWrapper(configuration, securityProviderName, currentUserName);
   return client.ExecuteQuery(qParams);
}

You can subsequently retrieve the field values you need to display from the returned QueryResults type object using a syntax like the following:

var allEventNamesForResults = ExecuteQuery().Results.Select(result => result.Fields["feventname2181"];

Refer to the Indexing Manager for the translated names of your Sitecore fields. The Indexing Manager is located at http://[Your Sitecore Instance]/coveo/command-center/index.html#fields/.