Predefined Queries

Coveo for Sitecore 4.1 (November 2018)

Predefined queries are a feature that was added in Sitecore 7.0 Update-1, and it’s now supported in Coveo for Sitecore. This feature allows developers to decorate any C# object inheriting the SearchResultItem class with a set of filters, which will be applied upon querying the object. In the following example, a predefined query is appended to the Airport object.

[PredefinedQuery("templatename", ComparisonType.Equal, "Airport"]
public class Airport : SearchResultItem
    public string City { get; set; }

Upon querying the object, the expression templatename=="Airport" will be added to the LINQ expression automatically. As a result, obvious expressions can be eliminated just as you type the query.

// Retrieving all airports when the object is decorated with a PredefinedQueryAttribute 
var result = context.GetQueryable<Airport>().ToList();
// Retrieving all airports when the object isn't decorated with a PredefinedQueryAttribute
var result = context.GetQueryable<Airport>()
                    .Where(item => item.TemplateName == "Airport")