Jump to content

Full-text searching two tables


Tachyon

Recommended Posts

I'm trying to create a full-text search engine for my blog.  I read the tutorial, and I grasp the basics of how to search a single table.  However, when a user enters a search term, I want to search both the table of blog posts (vsns_news) and the table of comments (vsns_comments) for the term and treat the results on equal footing.  That is, I don't want to display first the results from blog posts and then the results from comments: they should intermingle in order of decreasing relevancy.

 

However, I can't for the life of me come up with a query that will search both tables and return a combined result set for them.  Is there a way to use just one query?

Link to comment
Share on other sites

Just a stab in the dark but on the line FROM Table name try FROM TABLE1 and TABLE 2 WHERE Field Like $criteria

fyi though this will require that the field name be the same in both tables

The field name is not going to be the same in both tables--in fact, I am searching against multiple fields.

 

This is one way I tried it:

SELECT *, MATCH (vsns_news.heading, vsns_news.content) AGAINST ('" . $term . "') as news_score, MATCH (vsns_comments.comment) AGAINST ('" . $term . "') as comment_score FROM vsns_news, vsns_comments WHERE MATCH (vsns_news.heading, vsns_news.content) AGAINST ('" . $term . "') OR MATCH (vsns_comments.comment) AGAINST ('" . $term . "') AND vsns_comments.queue = '0' AND vsns_news.queue = '0' ORDER BY news_score, comment_score DESC LIMIT $from, $results

 

I know it's a horrific statement; at this point I was just fiddling with things to see if I could get a good result.

Link to comment
Share on other sites

I'm aware of the purpose of a table join. ;)  I'm just not sure how I would format my MATCH () ... AGAINST () statements in an SQL query with a table join ... it's a sight more complex than I've ever done.

 

The news table, vsns_news, has an ID field and vsns_comments has an article_id field that corresponds to the matching ID in a record in vsns_news.  So matching up the tables is no problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.