开发者

Accent-insensitive searches / problems with utf8_general_ci collation

开发者 https://www.devze.com 2023-03-29 03:16 出处:网络
Edit: if you\'re here because you\'re confused by the polish collation in MySQL, read this. I\'m trying to perform a full-text search on a table of polish cities and many of them contain accented cha

Edit: if you're here because you're confused by the polish collation in MySQL, read this.

I'm trying to perform a full-text search on a table of polish cities and many of them contain accented characters. It's meant to be used in an ajax call for auto completion so it would be nice if the search was accent-insensitive. I've set the collation of the rows to ut8_polish_ci. Now, given the city "Zelów", I query the database like this

SELECT * FROMcitiesWHERE MATCH( city ) AGAINST ("zelow")

but to no avail. Mysql returns an empty result. I've tried different accents, tried adding different collations to the开发者_StackOverflow社区 query but nothing helped. I'm not sure how I should approach this because accent-sensitivity seems to be poorly documented. Any ideas?


EDIT

So I found out that the case-insensitive full-text searches are performed only IN BOOLEAN MODE, so the correct query would be

SELECT * FROMcitiesWHERE MATCH( city ) AGAINST ( "zelow" IN BOOLEAN MODE )

Previously I thought otherwise due to a misleading comment on dev.mysql.com. There might be more to it but I'm just really confused right now.

Anyway, as mentioned in the comments below, I have UNIQUE index on the cities column so changing the collation of the table to accent-insensitive utf8_general_ci is out of the question.

I realized however, that the following query works quite well on a table with utf8_polish_ci collation:

SELECT * FROMcitiesWHERE city LIKE 'zelow' COLLATE utf8_general_ci

It would seem now that the most reasonable solution would be to do a full-text search in a similar fashion:

SELECT * FROMcitiesWHERE MATCH( city ) AGAINST ( 'zelow' IN BOOLEAN MODE ) COLLATE utf8_genral_ci

This however yields the following error:

#1253 - COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'binary'

This is really starting to get on my nerves. Might as well abandon full-text search in favour of a simple where-like approach but it doesn't seem sensible in a table with almost 50k records which will be intensively queried...


LAST EDIT Ok, the thing with boolean mode was partly bullshit. Only partly because it indeed works as I said, however, on a utf8_general_ci it works the other way around. I'm utterly perplexed and have no will to study this issue further. I decided to drop the UNIQUE index (no further cities will be added anyway so no need to make a big deal out of it) and stick with the utf8_general_ci table collation. I appreciate all the help, it steered me in the right direction.


Change your collation to utf_general_ci. It ignores accent when searching and ordering but still stores them correctly.


MySQL is very flexible in the encoding/collation area, maybe too flexible. When changing your encoding/collation, make sure you are converting the table, not just changing the encoding/collation types.

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

You can also convert individual fields, so your table can have a collation setting of utf8_general_ci, but you can change one or more fields so they use some other collation. Base on the "binary" error you are seeing, it seems your text field might have a collation of UTF8-BIN (or be a blob). Can you post the result of CREATE TABLE?

Remember, the CHARACTER SET (encoding) is how the data is stored, the collation is how it is indexed. Not all combinations work.

My original problem, and question, might help a little: Converting mysql tables from latin1 to utf8


If you try :

select * from cities where cityname like 'zelow'


Change your collation from binary to utf8_bin. utf8_bin should be compatible with utf8_general_ci, but will still allow you to store city names with differing accents.

0

精彩评论

暂无评论...
验证码 换一张
取 消