THIS IS ARCHIVED DOCUMENTATION

LINQ QueryResults Example: Debugging Helpers

Coveo for Sitecore (July 2016) Coveo for Sitecore 4.1 (November 2018)

This page contains basic information on how to show information to help you debug queries in your search page.

It uses the basic search page presented in LINQ QueryResults Example - Create a Basic Search Page.

Simple Error Handling

To handle simple errors, start by adding a new section to your page.

LinqBasedPageExample_6.aspx

<div ID="QueryErrorSection" class="queryErrorSection" Visible="False" runat="server">
    <asp:Label ID="QueryErrorLabel" runat="server" />
</div>

Add a new method called RenderException. This method handles any exception captured while the results are being queried.

It ensures the correct sections are shown and hidden when an exception has occurred.

LinqBasedPageExample_6.aspx.cs

private void RenderException(SearchModel model,
                             Exception e){
    ResultsSection.Visible = false;
    QueryErrorSection.Visible = true;
    QueryErrorLabel.Text = "Exception thrown while querying: " + e.Message.ToString();
}

You can now call this method in the Render section, and let the method handle the exception.

LinqBasedPageExample_6.aspx

private void Render(SearchModel model) {
    if(!String.IsNullOrEmpty(model.Query)) {
        try{
            ClearResults();
            QueryResults<SearchResult> results = ExecuteQuery(model);
            if(results.TotalCount != 0) {
                RenderWithResults(model, results);
            } else {
                RenderWithoutResults(model, results);
            }
            RenderCommon(model, results);
        }catch(Exception ex){
            RenderException(model, ex);
        }
    }
}

Show the Current Expression

Start by adding a new section to your page.

LinqBasedPageExample_6.aspx

<div class="expressionSection">
    <asp:Literal Text="Current expression: " runat="server" />
    <asp:Literal ID="CurrentExpressionLabel" runat="server" />
</div>

You can now bind the expression using this simple line in the RenderCommon method:

LinqBasedPageExample_6.aspx.cs

private void RenderCommon(SearchModel model,
                          QueryResults<SearchResult> results) {
    CurrentExpressionLabel.Text = results.Debug.BasicExpression;
}

You can explore the Debug object to see if any additional information is useful for your use case.

Result

When you get an exception, you should have something similar to this:

Client-Side Code

Your client-side code should now look similar to this:

LinqBasedPageExample_6.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqBasedPageExample_6.aspx.cs" Inherits="Tutorial.LinqBasedPageExample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>LINQ-based Page Example</title>
</head>
<body>
    <form id="pageForm" runat="server">
        <div class="searchSection">
            <asp:Literal Text="Query: " runat="server" />
            <asp:TextBox ID="Querybox" runat="server" />
            <asp:Button type="submit" Text="Run Query" runat="server" />
        </div>
        <div ID="ResultsSection" class="resultsSection" runat="server">
            <asp:GridView ID="LSTResults" AutoGenerateColumns="true" runat="server" />
        </div>
        <div ID="QueryErrorSection" class="queryErrorSection" Visible="False" runat="server">
            <asp:Label ID="QueryErrorLabel" runat="server" />
        </div>
        <div class="expressionSection">
            <asp:Literal Text="Current expression: " runat="server" />
            <asp:Literal ID="CurrentExpressionLabel" runat="server" />
        </div>
    </form>
</body>
</html>

Server-Side Code

Your server-side code should now look similar to this:

LinqBasedPageExample_6.aspx.cs

namespace Tutorial {
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Coveo.Framework.SearchService;
    using Coveo.SearchProvider.Linq;
    using Coveo.SearchProvider.LinqBase;
    using Sitecore.ContentSearch;
    using Sitecore.ContentSearch.Linq;
    using Sitecore.ContentSearch.SearchTypes;
    public partial class LinqBasedPageExample : Page {
        protected void Page_Load(object sender, EventArgs e) {
            SearchModel searchModel = new SearchModel();
            if (Page.IsPostBack) {
                MapControlsToModel(searchModel);
                Render(searchModel);
            }
        }
        private void Render(SearchModel model) {
            if(!String.IsNullOrEmpty(model.Query)) {
                try{
                    ClearResults();
                    throw new Exception("Forced example exception");
                    QueryResults<SearchResult> results = ExecuteQuery(model);
                    if(results.TotalCount != 0) {
                        RenderWithResults(model, results);
                    } else {
                        RenderWithoutResults(model, results);
                    }
                    RenderCommon(model, results);
                }catch(Exception ex){
                    RenderException(model, ex);
                }
            }
        }
        private void ClearResults() {
            LSTResults.DataSource = "";
            LSTResults.DataBind();
        }
        private void RenderWithResults(SearchModel model,
                                       QueryResults<SearchResult> results) {
            LSTResults.DataSource = results.Results;
            LSTResults.DataBind();
        }
        private void RenderWithoutResults(SearchModel model,
                                          QueryResults<SearchResult> results) {
            // No results handling.
        }
        private void RenderCommon(SearchModel model,
                                  QueryResults<SearchResult> results) {
            CurrentExpressionLabel.Text = results.Debug.BasicExpression;
        }
        private void RenderException(SearchModel model,
                                     Exception e){
            ResultsSection.Visible = false;
            QueryErrorSection.Visible = true;
            QueryErrorLabel.Text = "Exception thrown while querying: " + e.Message.ToString();
        }
        private QueryResults<SearchResult> ExecuteQuery(SearchModel model) {
            QueryResults<SearchResult> results;
            ISearchIndex index = ContentSearchManager.GetIndex("Coveo_web_index");
            using (var context = index.CreateSearchContext()) {
                  IQueryable<SearchResult> query = context.GetQueryable<SearchResult>()
                                                          .CoveoOr(model.Query);
                  results = query.GetCoveoQueryResults();
            }
            return results;
        }
        private void MapControlsToModel(SearchModel model) {
            model.Query = Querybox.Text;
        }
    }
    public class SearchModel {
        public string Query { get; set; }
        public SearchModel() {
            Query = "";
        }
    }
    public class SearchResult {
        public DateTime Date { get; set; }
        public string Title { get; set; }
        public string PrintableURI { get; set; }
    }
}