I'm trying to do a full-text search in three seperate tables and sort the results by relevancy. During my searches for the answer, i found out that i can't use fulltext search in multiple tables. So i added a seperate fulltext index for each column i want to search. Now the problem is that i can do the search but i can't do sorting as i would like to.
Here's my tables
CREATE TABLE books (
bookID int(11) NOT NULL AUTO_INCREMENT,
title varchar(300) NOT NULL,
authorID int(11) NOT NULL,
FULLTEXT KEY title (title)
)
CREATE TABLE IF NOT EXISTS authors (
authorID int(11) NOT NULL AUTO_INCREMENT,
authorNamevarchar(200) NOT NULL,
FULLTEXT KEY authorName(authorName)
);
CREATE TABLE IF NOT EXISTS chapters (
chapterID int(11) NOT NULL AUTO_INCREMENT,
bookID int(11) NOT NULL,
content longtext NOT NULL,
FULLTEXT KEY content (content)
);
And my sql query. This is where I'm stuck.
SELECT *,
MATCH(books.title) AGAINST('$q') as tscore,
MATCH(authors.authorName) AGAINST('$q') as ascore
MATCH(chapters.content) AGAINST('$q') as cscore
FROM books
LEFT JOIN authors ON books.authorID = authors.authorID
LEFT JOIN chapters ON books.bookID = chapters.bookID
WHERE
MATCH(books.title) AGAINST('$q')
OR MATCH(authors.authorName) AGAINST('$q')
OR MATCH(chapters.content) AGAINST('$q')
ORDER BY ???? DESC
Now with this query i can do sorting by titles, authors or contents. What i want to do is, get the relevancy for all the three columns together and order the results by that.
And, yes i'm aware of other search engines like lucene or sphinx, but i'm not planning to use them now.
Thanks in advance.
You should be able to add the tscore, ascore, and cscore values in the ORDER BY clause.
Try this:
SELECT *,
MATCH(books.title) AGAINST('$q') as tscore,
MATCH(authors.authorName) AGAINST('$q') as ascore,
MATCH(chapters.content) AGAINST('$q') as cscore
FROM books
LEFT JOIN authors ON books.authorID = authors.authorID
LEFT JOIN chapters ON books.bookID = chapters.bookID
WHERE
MATCH(books.title) AGAINST('$q')
OR MATCH(authors.authorName) AGAINST('$q')
OR MATCH(chapters.content) AGAINST('$q')
ORDER BY (tscore + ascore + cscore) DESC