7

I have two queries

List<sobject> soslRecords1 = Search.query('FIND {I need help res*} 
      in all fields returning Incident__c (id) 
      where ID  IN:rec_ids')[0];

List<sobject> soslRecords2 = Search.query('FIND {I need help res*} 
      in all fields returning Incident__c (id)')[0];

In first query rec_ids is Set of 2000 Id's of Incident__c object which returns 2000 matching records correctly, whereas from second query only 250 records are returning even if there are more than 2000 records in Incident__c object.

Can you please tell me why this is happening?

6
  • Are those two queries in separate classes? What is the sharing model for the object you are querying? Are you running both queries as the same user? – Alex Tennant May 25 '15 at 9:21
  • These two queries are in the same class. The sharing model for this object is private and Grant Access Using Hierarchies checkbox is set to true. Yes I am running both the queries as the same user (Imported more than 2000+ records as same user and querying it as same user). One more thing I have tried out is - Imported 2000+ records in Account object and queried it using the second query, In this case as well I am getting only 250 records. – Shilpa May 25 '15 at 9:30
  • 3
    I just encountered a similar problem. Adding a where clause in the search term would push the results out to the full 2000 record limit. Otherwise I only got 250 records. I found even just adding an ORDER BY clause would use the higher limit. – Daniel Ballinger May 6 '16 at 1:35
  • @DanielBallinger - i just tried adding a where clause and also a order by condition in the SOSL, but still i get 250. Are you sure it gets extended to 2K if you add a where or order by clause. Stuck with same issue and found this thread, will be great if anyone can help out with any new info. – Anil Shivaraj Jun 21 '16 at 3:37
  • @AnilShivaraj Very certain, but the exact reason isn't clear. It would appear that it isn't a definitive solution. Try adding a where clause over an indexed field. – Daniel Ballinger Jun 21 '16 at 8:57
2

This is strange behavior of SOSL. SOSL returns only 250 records unless you use order by Clause and that will work only if you are returning only one object.

For eg. this query will return only 250 records from Contact Find {Industry} RETURNING Work__c(ID Order by Name), Contact(FirstName,LastName Order by createdDate)

but below query will return 2000 records if you have more than 2000 matching records in salesforce. Find {Industry} RETURNING Contact(FirstName,LastName Order by createdDate)

1

Here is the explanation for this SOSL behaviour :

What are the main limits in relation to SOSL?

The record data in Salesforce is stored and indexed in partitions. Records for the same entity can be stored across several partitions. Salesforce searches across up to a maximum of 50k records per partition and can collect up to a maximum of 10k records per entity per partition. After searching all partitions, the maximum results the can be returned per entity is 2,000.

What is SOSL truncation?

Truncation is the process of the search of partitions is completed. After the SOSL completes the scanning of all partitions (with whatever limit is being used), we then apply sharing and any "WHERE" clauses to the returned rows. This can result in eliminating all returned rows and a possibility of returning 0 records to the end user.

for more details refer here

1
  • 1
    I have read your blog entry and I still don't understand it. You write about Limit #5, but I can only count 4 limits in your story. – Sander de Jong Feb 27 '20 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.