I've got a client who's asking if their search, which searches for company names which could be searched for in several formats depending on user input, such as the company stored in the database being A J R Kelly Ltd for instance, If a user searches "A J R Kelly" it's found, using;
<cfif pctermsCount gt 0>
AND (LOWER(p.name) LIKE '%#pcTerms#%')
</cfif>
If they search for "Kelly" the company is found, but if they search for a broken version of the string like "A J Kelly" or "AJ Kelly" it's not found.
Is there anything I can do to make it just a little bit more forgiving?
Thanks.
The NOT LIKE operator in SQL is used on a column which is of type varchar . Usually, it is used with % which is used to represent any string value, including the null character \0 . The string we pass on to this operator is not case-sensitive.
STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one.
If you're using MyISAM, you can use full text indexing. See this tutorial
If you're using a different storage engine, you could use a third party full text engine like sphinx, which can act as a storage engine for mysql or a separate server that can be queried.
With MySQL full text indexing a search on A J Kelly
would match AJ Kelly
(no to confuse matters but A, J and AJ would be ignored as they are too short by default and it would match on Kelly.) Generally Fulltext is much more forgiving (and usually faster than LIKE '%string%') because allows partial matches which can then be ranked on relevance.
You can also use SOUNDEX to make searches more forgiving by indexing the phonetic equivalents of words and search them by applying SOUNDEX on your search terms and then using those to search the index. With soundex mary
, marie
, and marry
will all match, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With