Fulltext Indexes were a new feature in MySQL 5.0, and they're meant to be a wonderful way of searching through large tables with Text column types (or Varchar, for that matter).
I was curious to see how a Fulltext search differed from a regular pattern match search. So I set up Fulltext Indexes on the line column (i.e. the bit with the text in). The syntax for searching through Fulltext Indexes involves the keywords MATCH and AGAINST. Let's find out how times Abraham is mentioned in the bible, first using the Fulltext Index:
And now using LIKE, i.e. regular MySQL pattern match syntax:
Hmm, that's interesting, we get more lines matching Abraham back from the pattern match (LIKE) search. How come? The next query shows all the lines which matched the LIKE search but didn't match the Fulltext search. It's a Left Join, which is the kind of query we use to find all the stuff which doesn't match the first table. Normally one would join two tables, but here we join two subqueries together, as though they were tables. It seems a little weird, but as each subquery produces a table-like result, it is legitimate SQL. We do have to provide a table alias for each subquery though, so MySQL can kid itself it's dealing with tables. The order of subqueries is important. The LIKE subquery goes first, as that is the one with the most rows in.
Bingo! They're all Abraham's rather than Abraham. MATCH only deals in whole words, whereas LIKE isn't so fussy.
This is really pointed up in the next query which shines a light on how MATCH works. It assigns a number to each row. The greater the number the better the match, or to put it another way,the higher the relevance. Notice the relevance is 0 for the lines which say Abraham's.
Comments
view whole verse
Hi there, thanks for all the help on your website, it has been very helpful. However i have created a small page for myself that will bring back the book, volume, verse and complete line after giving it something to search for. I have been trying to then bring back the whole verse using the information brought back from mysql.
So i created a form form the results, to insert what i thought would be sufficent info to do this, but its not working.
I have the following:
select * from bible join books on bible.books = books.abbr where books.name ='$name' AND `volume` ='$volume' AND `verse` ='$verse
But this does not work. Would be grateful if you could give some pointers how to bring back a whole verse based on a search from a certain line.
The example i used was search for "land of nod", this brings back only one line (from Genesis, Old Testament, cap 4, verse 16.
Thanks in advance, Dave.