Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencart - Search regardless accent

It had been written many times already that Opencart's basic search isn't good enough .. Well, I have came across this issue:

When customer searches product in my country (Slovakia (UTF8)) he probably won't use diacritics. So he/she writes down "cucoriedka" and found nothing.

But, there is product named "čučoriedka" in database and I want it to display too, since that's what he was looking for.

Do you have an idea how to get this work? The simple the better!

like image 202
Jožko Golonka Avatar asked Feb 06 '13 22:02

Jožko Golonka


1 Answers

I'm ignorant of Slovak, I am sorry. But the Slovak collation utf8_slovak_ci treats the Slovak letter č as distinct from c. (Do the surnames starting with Č all come after those starting with C in your telephone directories? They probably do. The creators of MySQL certainly think they do.)

The collation utf8_general_ci treats č and c the same. Here's a sql fiddle demonstrating all this. http://sqlfiddle.com/#!9/46c0e/1/0

If you change the collation of the column containing your product name to utf8_general_ci, you will get a more search-friendly table. Suppose your table is called product and the column with the name in it is called product_name. Then this SQL data-definition statement will convert the column as you require. You should look up the actual datatype of the column instead of using varchar(nnn) as I have done in this example.

 alter table product modify product_name varchar(nnn) collate utf8_general_ci

If you can't alter the table, then you can change your WHERE clause to work like this, specifying the collation explicitly.

WHERE 'userInput' COLLATE utf8_general_ci = product_name

But this will be slower to search than changing the column collation.

like image 69
O. Jones Avatar answered Nov 15 '22 19:11

O. Jones