According to the SOQL and SOSL Reference page on FIND {SearchQuery}:

If the SearchQuery string is longer than 10,000 characters, no result rows are returned. If SearchQuery is longer than 4,000 characters, any logical operators are removed. For example, the AND operator in a statement with a SearchQuery that’s 4,001 characters will default to the OR operator, which could return more results than expected.

This doesn't make sense - "any logical operators are removed" doesn't really mean the same thing as "the AND operator... will default to the OR operator."

I tried running some OR queries without the OR keyword - using spaces or commas in place of OR, to try to mimic the "logical operators are removed" statement, but it didn't return any results, while the original OR query. Does it really jusy mean that AND will be treated as OR? What if my query is > 4000 characters (and less than the upper limit of 10,000 characters), but includes only search terms separated by OR? Will it work as written? Not sure it matters, but assume my SearchQuery will be a bound Apex variable.

1 Answer 1


In theory, it should work as you'd expect (e.g. filters will work correctly, even in excess of 4,000 characters). This is because the Apex runtime behaves differently than the SOAP and REST APIs in regards to queries, especially when bind variables are in play.

Some objects that you can query in SOQL can't be queried in Apex, for example, and you're allowed to use special constructs (Field LIKE :listOfStrings comes to mind) that are not valid in SOQL, and so on. Even worse, many of these special rules aren't documented, so you're right in experimenting with Apex to find those differences.

Also keep in mind that unit tests do not actually perform SOSL, you have to use Test.setFixedSearchResults to test SOSL. This is a further complication, as it means you'll need to set up real data and use an Execute Anonymous script or other production code in order to test theories.

For a valid comparison, try writing a huge query in SOSL and execute it from the API, then try the same in Apex with bind variables. I expect you'd have differences in the results, with Apex being more faithful to the desired query than the API.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .