I need to implement a search option for user comments that are stored in a MySQL database. I would optimally like it to work in a similar manner to a standard web page search engine, but I am trying to avoid the large scale solutions. I'd like to just get a feel for the queries that would give me decent results. Any suggestions? Thanks.
-
4Take a look at the docs: dev.mysql.com/doc/refman/5.0/en/fulltext-search.htmlRichard H– Richard H2011-05-17 13:00:40 +00:00Commented May 17, 2011 at 13:00
-
1That's not really the kind of thing SQL is build for.Thomas Ahle– Thomas Ahle2011-05-17 13:01:01 +00:00Commented May 17, 2011 at 13:01
-
Granted, I didn't use a question mark anywhere in my text. The question is: Does anyone have any suggestions as to how I might go about accomplishing this?jwBurnside– jwBurnside2011-05-17 13:16:33 +00:00Commented May 17, 2011 at 13:16
2 Answers
It's possible to create a full indexing solution with some straightforward steps. You could create a table that maps words to each post, then when you search for some words find all posts that match.
Here's a short algorithm:
- When a comment is posted, convert the string to lowercase and split it into words (split on spaces, and optionally dashes/punctuation).
- In a "words" table store each word with an ID, if it's not already in the table. (Here you might wish to ignore common words like 'the' or 'for'.)
- In an "indexedwords" table map the IDs of the words you just inserted to the post ID of the comment (or article if that is what you want to return).
- When searching, split the search term on words and find all posts that contain each of the words. (Again here you might want to ignore common words.)
- Order the results by number of occurrences. If the results must contain all the words you'd need to find the union of your different arrays of posts.
Comments
As an entry point, you can use MySQL LIKE queries.
For example if you have a table 'comments' with a column named 'comment', and you want to find all comments that contain the word 'red', use:
SELECT comment FROM comments WHERE comment LIKE '% red %';
Please note that fulltext searches can be slow, so if your database is very large or if you run this query a lot, you will want to find an optimized solution, such as Sphinx (http://sphinxsearch.com).