Gravatar for changping.peng@avanade.com

Question by helen, Oct 28, 2015 7:04 AM

Coveo for Sitecore - Linq Query for query and location

HI Team,

I am learning Coveo with Sitecore. May I ask how to user Linq to query based on template and location?

 IQueryable<Item> source =
     from toItem in LinqHelper.CreateQuery<SitecoreItem>(providerSearchContext, 
        SearchStringModel.ParseDatasourceString("+location:{CCC3305D-A0C3-4525-8639-ED21EED4215A};+template:{d9019e30-f954-46cc-aa70-3e928c40b5d0}"))
     where toItem.Language == languageCode
     select toItem.GetItem();

Regards, Helen

1 Reply
Gravatar for sholmesby@hhogdev.com

Answer by sholmesby, Oct 28, 2015 4:44 PM

The code you've shown is using the DataSourceString converter. Essentially this is converting the data source value from the content editor in Sitecore, to a query that can be run within a SearchContext. I've never tried this before, but if you just want some code that does the equivalent, you could use the ContentSearch Linq Provider to do it.

Some details here:- http://getfishtank.ca/blog/intro-to-coveo-for-sitecore-contentsearch-linq-api

It should be something like:-

var templateID = new ID("{d9019e30-f954-46cc-aa70-3e928c40b5d0}");
var locationID = new ID("{CCC3305D-A0C3-4525-8639-ED21EED4215A}");
var items = context.GetQueryable<SearchResultItem>()
                               .Where(x => x.TemplateID.Equals(templateID)
                                              && x.Paths.Contains(locationID));

Note: The above code has not been tested.

Gravatar for changping.peng@avanade.com

Comment by helen, Oct 29, 2015 12:25 AM

Thanks to Sholmesby.

I found the root cause of my issue - only return 10 records.

There is a default setting for Coveo search : DefaultNumberOfResults and value is 10.

Regards, Helen

Ask a question