[SalesForce] SOSL and “ends with” wildcard

I am trying to implement a SOSL search function that uses an "ends with" wildcard, like *searchString in SOQL. No luck! I know about this limitation of SOSL

Asterisks match zero or more characters at the middle or end (not the
beginning) of your search term.

So if i search for the "Home" and enters just "ome" i will get no results returned.

Is there are some workarounds for this?

Best Answer

You can perform a normal SOQL query. The reason why you can't wildcard at the beginning is because the index, like in all database systems, indexes from the beginning of each word. In the case where you have a word like "Home", the SOSL index would contain an entry like:

Term Matching Ids Home Record1, Record5, Record27, ...

Thus, it can match Ho*, or Home*, or Home, but not *ome. This restriction exists because (1) it would be too expensive to index every permutation (e.g. it would have to index home, ome, me, and e, instead of just home), and (2) without a partial index described in 1, a frontal wildcard would cause a full table scan of the index, which is impractical computationally.

Instead, you'd have to just perform a normal SOQL query for each table on each field where you want to find the "ends with" term.

Related Topic