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
bohemianguy100bohemianguy100 

limit the fields searched in sosl query

I am using a sosl query to search products, but I would like to limit the fields that are searched.  Is there a way to limit what fields are searched?

Here is my method:

private List<Product2> runSoslToExecute() {
  List<List<Product2>> searchResults = [FIND :query IN Name FIELDS RETURNING Product2 (Id, Name, Stock_Number__c, Serial_Number__c, Model__c, Year__c)];
  List<Product2> results = new List<Product2>();
  for(Product2 p : searchResults[0]) {
   results.add(p);
  }
  return results;
}

Thanks for any help.
Best Answer chosen by bohemianguy100
bob_buzzardbob_buzzard
You can't limit the fields that are searched - you'd have to use a SOQL query for that, which allows you to specify which fields are considered as part of the where clause.  The best you can do in SOQL is restrict the fields that are returned and then post process the results to check if the search string appears in the returned fields. If there are no matches in the fields that you are interested in you can then discard the record from the results.