[SalesForce] Highlight relevant article text display on public knowledge base

I've tried searching but can't find anything to answer my question. We've got a customised version of PKB2 on a force.com site and I'm trying to update it so we can take advantage of keyword highlighting for searches. I've enabled

Highlight relevant article text within search results

in the knowledge settings and it's working as expected in the knowledge app.

I've tested the query my code is producing in the query browser on the dev console and it's working as I would expect, but don't know how to get access to/display the results using apex/visual force.

Example SOSL query of:
FIND {some search words} IN ALL FIELDS RETURNING Faq__kav(KnowledgeArticleId, Title, Summary, UrlName WHERE PublishStatus ='online' AND Language = 'en_US' LIMIT 5) WITH SNIPPET(target_length=300)
SOSL query using WITH SNIPPET

apex piece:

List<List<SObject>> searchList = Search.query(searchQuery);
system.debug(searchList);

Searchlist has a size of 1.
This shows me the arrary of FAQ_kav records and thier data but no reference to snippet.text or snippet.whole.title like I get in the query Editor.

Any ideas?

Best Answer

Looks like the answer is in this post. I had dismissed looking at the Dynamic SOSL hits in google. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_sosl.htm#snippet

Use Dynamic SOSL to Return Salesforce Knowledge Article Snippets To provide users with more context for articles in search results, use the SOSL WITH SNIPPET clause. Snippets make it easier for users to identify the content that they’re looking for when the search term isn’t included in the article summary field. For information about how snippets are generated, see WITH SNIPPET in the Force.com SOQL and SOSL Reference.

To use the SOSL WITH SNIPPET clause in a dynamic SOSL query at run time, use the Search.find method.

Search.SearchResults searchResults = Search.find(SOSL_search_string);

This example exercises a simple SOSL query string that includes a WITH SNIPPET clause. The example calls System.debug() to print the returned article titles and snippets. Your code would display the titles and snippets in a Web page.

Search.SearchResults searchResults = Search.find('FIND \'test\' IN ALL FIELDS RETURNING 
KnowledgeArticleVersion(id, title WHERE PublishStatus = \'Online\' AND Language = \'en_US\') WITH SNIPPET (target_length=120)');

List<Search.SearchResult> articlelist = searchResults.get('KnowledgeArticleVersion');

for (Search.SearchResult searchResult : articleList) { 
    KnowledgeArticleVersion article = (KnowledgeArticleVersion) searchResult.getSObject(); 
    System.debug(article.Title); 
    System.debug(searchResult.getSnippet()); 
} 
Related Topic