MySQL搜索汉字
假设我有一行:
一天吃一个苹果
有人输入查询:
天苹果
我是否应该分解查询中的字符,并单独执行LIKE % %
匹配每个字符与行的匹配,或者是否有更简单的方法来获取包含两个字符之一的行? FULLTEXT 不适用于 CJK 字符。
谢谢!
Let's say I have a row:
一天吃一個蘋果
Someone enters as a query:
天蘋
Should I break up the characters in the query, and individually perform a LIKE % %
match on each character against the row, or is there any easier way to get a row that contains one of the two characters? FULLTEXT won't work with CJK characters.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正在搜索字符串中的所有查询字符并关心字符顺序。
首先拆分用户查询。对
天
和苹果
说天苹果
。然后使用
LIKE
和%
构造 SQL 查询,例如WHERE x LIKE '%天%苹果%'
。Supposed you are searching for all query characters in a string and care about the character order.
First split the user query. Say
天蘋
to天
and蘋
.Then construct a SQL query with
LIKE
and%
, for example,WHERE x LIKE '%天%蘋%'
.看来我们需要一个词汇来执行正确的中文搜索。他们没有空间,但他们仍然有文字。
例如,我们在输入中输入了类似“ieatpear”的内容。
我们应该将其拆分为任意数量的 1-4 个字母的单词。
i ie iea ieat e ea 吃 eatp a at atp atpe t tp tpe tpea p pe 豌豆梨 e ea 耳朵 a r r
比我们的算法找到真正的单词 - 我吃梨耳。下一步,我们可以排除 Ear,因为它已经在 pear 中,并在预先分割的词汇表中执行搜索。
It seems we need a vocabulary to perform correct chinese search. They do not have spaces yet they still have words.
For example, we've got something like "ieatpear" on input.
We should split it into any possible amount of words of 1-4 letters.
i ie iea ieat e ea eat eatp a at atp atpe t tp tpe tpea p pe pea pear e ea ear a ar r
Than our algorhythm finds real words - I eat pear ear. On the next step we can exclude ear for it's already in pear and perform search within pre-splitted vocabulary.