views:

33

answers:

2

How would you implement a custom search engine?

What do you think about something like this:

SELECT *
FROM   jobs
WHERE  job_id IN (
        SELECT job_id
          FROM job_words
         WHERE word_id IN (SELECT word_id FROM words w WHERE text = 'carpenter'))
  AND  job_id IN (
        SELECT job_id
          FROM job_words
         WHERE word_id IN (SELECT word_id FROM words w WHERE text = 'buildings'))

or this:

SELECT j.*
      ,s.matches
  FROM jobs as j INNER JOIN
       (SELECT jw.job_id, count(*) as matches
          FROM job_words AS jw
               INNER JOIN (SELECT word_id FROM words w WHERE text IN ('carpenter', 'buildings')) AS w ON w.word_id = jw.word_id
        GROUP BY jw.job_id) as s ON s.job_id = j.job_id
A: 

You'd be better off building the tables from your keywords in advance. Your code is very inefficient. You're basically running O(n(n+n)) every time you run this code. Instead make the tables for all the 'carpenter' and 'buildings' before hand and if the table for the search query doesn't exist then use the code you posted.

amccormack
'carpenter' and 'buildings' are words inside a job description. This sql statement looks for jobs containing both words.
Eduardo
Right. I think the best performance you're going to get would be to make the tables for all your key words in a job description. Basically to hash all keywords as they are posted. This may be overkill for your project, and it is really only necessary if your job postings are posted a lot less frequently relative to the number of searches you are running.
amccormack
A: 
SELECT * FROM jobs WHERE
    job_id in (SELECT job_id FROM job_words WHERE
        word_id in (SELECT word_id FROM words WHERE text in ('carpenter', 'buildings'))
Will Hartung
This way we'll search for jobs that contain 'carpenter' or 'buildings'.
Eduardo