function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
paul-lmipaul-lmi 

SOSL with SF Knowledge

I'm building a custom agent integration where I need to search several different article types at the same time.  As I've added on two more article types than the default FAQ, I hit a small issue.  The 3rd document type, Link, never returns in search results via Apex.

 

Here's the code relevant to the query:

 

Public Static List<List<sobject>> searchDocs(string keywords, string productcode, string langcode, integer numresults){	
	//make the search terms SOSL injection safe
	keywords = String.escapeSingleQuotes(keywords);
	
	//ensure categories are never null
	if(productcode == '' || productcode == NULL){productcode = 'AllProducts';}
	if(langcode == '' || langcode == NULL){langcode = 'en';}
	if(numresults > 100){numresults = 100;}	/** cap the results at 100 for performance */
	if(numresults < 1 || numresults == NULL){numresults = 5;}	/** set a default of 5 */
	
	//add the __c requried for custom data category names
	productcode = productcode + '__c';
	langcode = langcode + '__c';
	string query = 'FIND \'' + '*' + keywords + '*' + '\'' +
		'IN ALL FIELDs Returning ' + 
		'FAQ__kav(ID, Title, Localized_Title__c, UrlName WHERE PublishStatus = \'Online\'), ' + 
		'Documentation__kav(ID,Title, Localized_Title__c, UrlName WHERE PublishStatus = \'Online\'), ' +
		'Link__kav(ID,Title,Localized_Title__c,UrlName WHERE PublishStatus = \'Online\') ' +
		'WITH DATA CATEGORY Products__c AT ' + productcode + ' ' +
			'AND Languages__c AT ' + langcode + ' LIMIT ' + numresults;
	  
	List<List<SObject>> qry = search.query(query);

	system.debug('\n\nSearch results: ' + qry + '\n\n');
	return qry;	
}

 

 

So if I pass in keyword as 'guide', productcode as 'lmirescue', lang as 'en', and numresults as 5, I get results on both FAQ__kav and Documentation__kav, but not Link__kav.  If I submit the same search via the Articles tab itself, filtering by the data categories, I get the expected search results.

 

The only difference in the structure is that Link__kav has no rich text fields.  It's simple a couple of text fields for Title (default), Localized_Title__c (text, 255), Url (Text, 255).

 

My question is, why would SOSL not pick up articles by the text fields defined for the article type if I'm using the IN ALL FIELDS syntax?  Should at least the default Title field be indexed?  Isn't SOSL supposed to be the same underlying search engine as the actual user interface?  Am I going batty?

Best Answer chosen by Admin (Salesforce Developers) 
paul-lmipaul-lmi

figured it out.  it was an issue with the query.  i wasn't aware that in SOSL, your LIMIT statement should go inside each object you're returning if you want an equal/max number of items per object.

 

query changed to:

 

'FIND \'' + '*' + keywords + '*' + '\'' +
		'IN ALL FIELDs Returning ' + 
		'FAQ__kav(ID, Title, Localized_Title__c, UrlName, LastPublishedDate WHERE PublishStatus = \'Online\' LIMIT ' + numresults + '), ' + 
		'Documentation__kav(ID,Title, Localized_Title__c, UrlName, LastPublishedDate WHERE PublishStatus = \'Online\' LIMIT ' + numresults + '), ' +
		'Link__kav(ID,Title,Localized_Title__c,UrlName,LastPublishedDate WHERE PublishStatus = \'Online\' LIMIT ' + numresults + ') ' +
		'WITH DATA CATEGORY Products__c AT ' + productcode + ' ' +
			'AND Languages__c AT ' + langcode;

 

 

All Answers

paul-lmipaul-lmi

figured it out.  it was an issue with the query.  i wasn't aware that in SOSL, your LIMIT statement should go inside each object you're returning if you want an equal/max number of items per object.

 

query changed to:

 

'FIND \'' + '*' + keywords + '*' + '\'' +
		'IN ALL FIELDs Returning ' + 
		'FAQ__kav(ID, Title, Localized_Title__c, UrlName, LastPublishedDate WHERE PublishStatus = \'Online\' LIMIT ' + numresults + '), ' + 
		'Documentation__kav(ID,Title, Localized_Title__c, UrlName, LastPublishedDate WHERE PublishStatus = \'Online\' LIMIT ' + numresults + '), ' +
		'Link__kav(ID,Title,Localized_Title__c,UrlName,LastPublishedDate WHERE PublishStatus = \'Online\' LIMIT ' + numresults + ') ' +
		'WITH DATA CATEGORY Products__c AT ' + productcode + ' ' +
			'AND Languages__c AT ' + langcode;

 

 

This was selected as the best answer
Kaushik_SupportKaushik_Support

Hi Paul,

 

I am implementing something similar.

But the issue I am facing is getting relevant results on to like if i have multiple words to search then it returns result from different article types grouped together not as a whole.

 

 I want most relevant results on top of list and that article may be from any of the Article Type.

 

Any urgent pointer will be really helpful.

 

Thanks,

Ray

 

francoisLfrancoisL

In this case, you need to query KnowledgeArticleVersion object and not article type objects.