Coveo for Sitecore 5 is now available!

Using QueryResults to Retrieve Query Metadata

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

You can use the Coveo LINQ GetCoveoQueryResults extension to retrieve an object containing results and query metadata.

This can be used to create a LINQ-based search page instead of querying the server many times to get different results.

How to Use

The following example shows one way to retrieve the QueryResults:

GetCoveoQueryResults example

private QueryResults<SearchResultItem> ExecuteQuery(string p_Query) {
    QueryResults<SearchResultItem> results;
    ISearchIndex index = ContentSearchManager.GetIndex("Coveo_web_index");
    using (var context = index.CreateSearchContext()) {
          IQueryable<SearchResultItem> query = context.GetQueryable<SearchResultItem>()
                                                      .CoveoOr(p_Query)
                                                      .Take(20);
 
          results = query.GetCoveoQueryResults();
    }
    return results;
}

Custom Result

You can specify your own result type by creating a simple class matching the field name you want to retrieve. The following example only returns the Date field.

GetCoveoQueryResults example

public class SimplePOCO {
    public string Date { get; set; }
}
 
private QueryResults<SimplePOCO> ExecuteQuery(string p_Query) {
    QueryResults<SimplePOCO> results;
    ISearchIndex index = ContentSearchManager.GetIndex("Coveo_web_index");
    using (var context = index.CreateSearchContext()) {
          IQueryable<SimplePOCO> query = context.GetQueryable<SimplePOCO>()
                                                .Take(20);
                                                .GetCoveoQueryResults();
    }
    return results;
}

For more details, refer to LINQ QueryResults Example - Introduction

Recommended Articles