SQL plural/singular searches

爱⌒轻易说出口 提交于 2020-01-27 08:39:05

问题


How can I make plural/singular words pull all forms?

Example: If a person searches for "mens rings" how can I check the data base to see if any of the fields contain mens, men, men's, ring, rings, etc.?


回答1:


It dosn't seem that using LIKE would be the best approach for what you need. I would look into MySQL FULLTEXT indexing to get the basic functionality working. I'll have to look into the plural thing a bit more to see if that can be accomplished.

Take a look at these two links for FULLTEXT indexing info.

  • http://dev.mysql.com/doc/refman/4.1/en/fulltext-search.html
  • http://www.petefreitag.com/item/477.cfm
  • http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

Here is a snippet from the second article:

SELECT headline, story FROM news
WHERE MATCH (headline,story) AGAINST ('Hurricane');

UPDATE:

I found this SO post in which a comment seems to suggest that the FULLTEXT indexing will take plural and grammatical forms into account but he dosn't cite a source, so it's hard for me to say for sure. Can you try the FULLTEXT indexing method?




回答2:


The Sphinx full-text search engine for MySQL handles such cases, and a few more (see here).

It uses the Porter Stemmer algorithm [2][3] to map things like "fishing", "fished", "fish", and "fisher" to a root stem of "fish" (see wiki).




回答3:


select * from index_table where item_name rlike '[[:<:]]preform[s]*[es]*[ies]*[[:>:]]';

Check if this helps. My case it worked out. Although will not cover for all plurals but yes for 90-95% cases.

Cheers, Ashish



来源:https://stackoverflow.com/questions/3088433/sql-plural-singular-searches

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