How to make MySQL treat underscore as a word separator for fulltext search?

爷,独闯天下 提交于 2019-12-10 17:57:32

问题


I am using MySQL fulltext and PHP (codeigniter) to search a database containing RSS items. Problem is some of these items's titles use underscores instead of spaces. Since MySQL considers underscores as part of a word, these items will never be matched in the search, unless the user types the exact title including underscores.

Server is shared so I don't have access to MySQL Server System Variables.

Can this behavior be changed in some other way? Can this maybe be done through the search query itself?

I know I could just replace all underscore occurrences in the DB by spaces, but this would compromise the original integrity of those titles though. Just wondering if there's another way of doing this.


回答1:


I know I could just replace all underscore occurrences in the DB by spaces, but this would compromise the original integrity of those titles though. Just wondering if there's another way of doing this.

You can instead of replacing underscores in original title field, use a separate field dedicated to fulltext searches.

This allows you to replace underscores, plus aggregates keywords into this field (category names, authors, tags, etc.) to enhance search results relevance. We used this a lot of times with success for getting rid of HTML tags in content infering with search




回答2:


I don't think this can be done without access to the server. The only way I have ever seen to do it is the first comment on this mySQL manual page ("How I added '-' to the list of word characters"). It requires stopping the server and changing internal configuration.

Your best bet is probably creating a second column with removed underscores, and to search that.



来源:https://stackoverflow.com/questions/2251518/how-to-make-mysql-treat-underscore-as-a-word-separator-for-fulltext-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!