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
Rakesh ERakesh E 

How to search in specific fields using SOSL queries

  Hi,

 

  I want to use SOSL for searching a value in some(specific) fields rather than in ALL fileds.

  i have observed that only dataType can be mentioned there like Phone Fields or Email  fields .

 

 please let me know if there is any possibility to search in specific fileds.

 

 

thanks in advance

 

bob_buzzardbob_buzzard

No, you can't search in specific fields.  If you need to find text that appears in specific fields, you should use a SOQL query where you can identify the specific field and search string(s).

 

The only way to achieve this in SOSL would be to execute the search and then post-process the results to filter out those with fields that don't contain the search query.

Rakesh ERakesh E

oh thank you for your reply.

 

i wanted to explain my scenario here.

 

i am displaying a set of fields to the user and when user enters any search value , that has to be serached only in the fields which we are displaying.

 

and one more doubt , can we use OFFSET in SOSL queries (we have pagination)

 

Thanks

 

Regards

Rakesh

bob_buzzardbob_buzzard

This sounds like a use case for SOQL - you can dyamically build the query to check the fields that you have displayed.

 

No, you can't use pagination in SOSL - it returns a maximum of 200 results.

George NguyenGeorge Nguyen
If you know which object that field belongs to you can use the WHERE clause in that object (just like you would in SOQL).
Rimali Gurav 5Rimali Gurav 5
Hello Bob
Can you please advice.
I have similar requirement wherein have salesforce site  where the VF page displays some Contact fields(totally on contact object and it's fields). On that user can search for Name ,Title ,Company fields. This is all done with SOQL.
Now we have encrypted(shield platform encryption) all this fields and hence I have to convert this SOQL to SOSL as filters are not supported on encrypted fields. Also I have Pagination on that VF page.
I have modified the SOQL to SOSL something like below:
if(String.isNotEmpty(searchContactsName) || String.isNotEmpty(searchContactsTitle) || String.isNotEmpty(searchContactsOrg) ) {
            showTheCount = true;
           
            if(String.isNotEmpty(searchContactsName)) {
            strSearch =+ '("' + searchContactsName  + '")'; 
            strConcat = true;
            }
            if(!String.isEmpty(searchContactsTitle)){
            if(!String.isEmpty(strSearch )){
                strSearch =+ ' OR ';
                
            }
            strSearch =+ '("' + searchContactsTitle + '")';
            strConcat = true;
            }
            if(!String.isEmpty(searchContactsOrg)){
            if(!String.isEmpty(strSearch )){
                strSearch =+ ' OR ';
                
            }
            strSearch =+ '("' + searchContactsOrg + '")';
            strConcat = true;
            }
            
            String searchQuery = 'FIND \'' + String.escapeSingleQuotes(strSearch)+ '\' IN ALL FIELDS RETURNING Contact(Name,Title )';
            List<List<sObject>> searchConLead = search.query(searchQuery);

However am getting error like  --------- line 1:178 no viable alternative at character '"'.
Can you please advice here how can I achive this.