简体   繁体   中英

Need a PHP MySQL script to search for keywords in a database

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.

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:

  1. When a comment is posted, convert the string to lowercase and split it into words (split on spaces, and optionally dashes/punctuation).
  2. 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'.)
  3. 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).
  4. 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.)
  5. 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM