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
Suma G 7Suma G 7 

Want to search in specific fields not ALL FIELDS in SOSL

Hi All,

i have a SOSL in my Apex code which like below:
String searchQuery = 'FIND \'' + String.escapeSingleQuotes(searchInput) + '\' IN ALL FIELDS RETURNING contentVersion  (Title, Tagscsv, Id, Availability__c, Colour_Enabled__c' + queryWhere + ' ORDER BY Title LIMIT 500) ';

But, my requirement is lik, i should not search in ALL FIELDS, but i have some specific fields like Title and Tagcsv .. any suggestions to achieve this. 

Thanks in advance.

Regards,
Suma. 
 
Best Answer chosen by Suma G 7
mritzimritzi
Hi Suma,

SOSL is used only when you don't know which Field/Object to search for the keyword.

When you know the Object to be queried, fields to be returned, fields to be searched upon, then you shoud used SOQL.

Sample:
 
Select Title, Tagscsv, Id, Avialability__c, Colour_Enable__c
From contentVersion
Where Field_Name LIKE '%'+searchInput+ '%'
Order By Title
Limit 500
(Please change field api names and Object api names to match with correct api names in your org)

If you think that SOQL won't address your problem then you can write SOSL query. But SOSL can only search on a common group of fields like Name, Email, Phone. You can't write SOSL to search only on selected fields of your choice.

More info here:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_in.htm


If this helps solve your problem
Please mark this as Best Answer.

 

All Answers

mritzimritzi
Hi Suma,

SOSL is used only when you don't know which Field/Object to search for the keyword.

When you know the Object to be queried, fields to be returned, fields to be searched upon, then you shoud used SOQL.

Sample:
 
Select Title, Tagscsv, Id, Avialability__c, Colour_Enable__c
From contentVersion
Where Field_Name LIKE '%'+searchInput+ '%'
Order By Title
Limit 500
(Please change field api names and Object api names to match with correct api names in your org)

If you think that SOQL won't address your problem then you can write SOSL query. But SOSL can only search on a common group of fields like Name, Email, Phone. You can't write SOSL to search only on selected fields of your choice.

More info here:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_in.htm


If this helps solve your problem
Please mark this as Best Answer.

 
This was selected as the best answer
Suma G 7Suma G 7
Thank you for the information. It really helps me alot.