Gravatar for sholmesby@hhogdev.com

Question by sholmesby, Oct 9, 2015 4:56 PM

Using an external source with Sitecore's ContentSearch 'Linq to Provider'

I've seen some questions around using Sitecore's ContentSearch with a Coveo external source. Some of these have managed to find some sort of base code for their implementation, but many parts of them are not explained. I am searching for documentation that details how Linq to Provider can be basically used once you already have an external source indexed. Is there any?

I've found some information on using LINQ (https://developers.coveo.com/display/public/SC201510/Using+LINQ+to+Customize+Queries ), but haven't found examples of a complete basic block of code.

For example:- Index Search Context https://answers.coveo.com/questions/2577/using-linq-expression-builder-with-isearchprovidercontext Refers to additional code inside the using statement to connect to external sources:-

using (var context = new ContentSearchManager.GetIndex("indexname").CreateSearchContext()) 
{
    ICoveoSearchContext coveoContext = context as ICoveoSearchContext;
    coveoContext.SearchContextOptions = new CoveoSearchContextOptions { ExternalSources = new List<string>() { "My External Source" }};
}

I've found this on some questions….but is there documentation with a detailed explanation of this? Here, I assume "My External Source" is the name of the source…. but what's confusing is it's wrapped in a using for the context of "indexname"…. So, in my case, I guess this would be "Coveowebindex", even though I don't want to query that, I only want to query the external source…..

Also:- Using SearchContext vs CoveoSearchContext https://answers.coveo.com/questions/2760/sitecore-external-sources-and-linq

The question uses the following code:-

results = context.GetQueryable<CustomSearchItem>().Where(i => i.RssTitle.Contains("Know Text")).GetAllResults().ToList();

Note, this is using the 'context' and not the 'coveoContext' that was created just above. This confused me…. How does it know to search the external source then?

Lastly:- Using POCOs and performing a query The above code uses a custom POCO 'CustomSearchItem'. I found I had the same issue as the question poster, where I wasn't retrieving any results. However, when I just used SearchResultItem, and used CoveoWhere("some query text"), I would get results.

How can I query, say, on just the '@customtitle' field? And if I had a POCO with CustomTitle as a property, what attributes do I need on it, and how could it be used in a query like the one above?

Thanks.

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Oct 14, 2015 6:02 PM

Hi Sean,

Your question contains 4 different topics:

  1. Documentation on the Coveo for Sitecore LINQ capabilities.
  2. Using Sitecore "LINQ-to-Provider" to query only external Coveo sources.
  3. Using SearchContext vs CoveoSearchContext.
  4. Using a POCO for the search result items.

Answers:

  1. Besides the documentation pages you already found, there's no other documentation or code samples.
  2. Sitecore "LINQ-to-Provider" module job is to query Sitecore indexes. Coveo for Sitecore just implemented the classes and methods of this module to be able to convert the LINQ expressions generated by the Sitecore module to Coveo search queries that the CES index is able to understand. Coveo also added a few extensions to the basic Sitecore methods to support custom query syntax and the ability to query additional CES sources in addition to the Sitecore index you create your SearchContext for. Coveo do not have a standalone LINQ module to query any CES source.
  3. In that code, the context and coveoContext variables are references to the same object in memory, just not of the sate type since the context variable is casted in the coveoContext variable. This is the trick to add the external sources in the search query.
  4. I linked 2 additional resources about the Sitecore "LINQ-to-Provider" feature that explains the attributes to set on your POCOs properties. You basically have to set the Sitecore field name for each property. Coveo for Sitecore LINQ module will take care of translating the Sitecore field names to Coveo field names (@f[SitecoreFieldName]XXXXX) before generating the Coveo search query that is sent to the Coveo Search Service. If you don't want the field name to be translated (for external content fields), you need to set those fields as external fields in the Coveo.SearchProvider.config file.

Additional resources:

  • http://getfishtank.ca/blog/intro-to-coveo-for-sitecore-contentsearch-linq-api
  • https://markstiles.net/Blog/2013/04/05/linq-to-sitecore.aspx

I hope this answers all your questions. Feel free to comment or ask other questions in new topics.

Thanks,

Jeff

Gravatar for slangevin@coveo.com

Comment by Simon, Jun 30, 2016 6:18 PM

There is now a small tutorial for LINQ with Coveo: https://developers.coveo.com/display/SitecoreV4/LINQ+QueryResults+Example+-+Create+a+Basic+Search+Page

Ask a question