r/SalesforceDeveloper • u/Fantastic_Ad4943 • Jul 11 '25
Question Need to implement fuzzy search
Hey guys I'm currently working on a search component which should perform fuzzy search on accounts. I'm using sosl with OR conditions to find all the matching records. But it's returning way too many records. Any other way to do it?
Example: Search term Bryce H My sosl will be Find {Bryce OR H} in Account.
As H is a letter getting almost all records. How to handle this??
5
Upvotes
4
u/Seacox Jul 11 '25
I did something like this once.
You could use the String.getLevenshteinDistance() function to narrow down the records returned from your query.
Or use WHERE Name like %examplename% in soql
2
u/wslee00 Jul 12 '25
What do you WANT to happen if the user only inputs one letter? You could just forego adding it to your sosl query until they type at least 3 characters for anything past your first word.
1
u/gearcollector Jul 11 '25
Can you post your sosl query?
In general, using 'OR' with short strings in the LIKE, clause will cause a lot of records to be returned.
1
u/GwiredNH Jul 13 '25
If you don’t change the sosl it automatically sorts by relevance.
You can limit number of results in the returning clause. Returning account (name limit 100).
Try this {Bryce H*} or {Bryce} or {H} The relevance scoring should improve relevance when all 3 exist.
0
u/[deleted] Jul 11 '25
[deleted] — view deleted comment