Page 1 of 1

Search 'Algorithm'

Posted: Sat Jul 22, 2006 10:47 pm
by LiveFree
Ok so maybe it's not an 'algorithm' so-to-speak but here is how I do it:

I get 10 rows from a DB (pagination), I then loop through each one with a preg_match looking like:

Code: Select all

while ($row = mysql_fetch_array($sql)){
if (preg_match("#(.*?)$name(.*?)#i", $row['name'])) $points += 1;

if ($points >= $total_points (2)) $hits[] = $row['id'];
So basically, if the preg finds the entered text anywhere in the DB record, it adds a point and if the points are greater than 2, that record is considered a 'hit'.

Can anyone tell me how I might go about improving this?

Thanks

Posted: Sat Jul 22, 2006 10:55 pm
by shoebappa
I'm no search expert but I know there's no future in preging through everything for matches. What DB are you using?

Posted: Sat Jul 22, 2006 10:58 pm
by LiveFree
MySQL :D

Posted: Sat Jul 22, 2006 11:03 pm
by shoebappa
Is there any reason the MySQL full text Indexes and search functionality won't work for you?

Posted: Sun Jul 23, 2006 11:16 am
by LiveFree
Isnt it true that Fulltext keys will slow down the querying on that column?

Posted: Sun Jul 23, 2006 3:17 pm
by johno
Tucker wrote:Isnt it true that Fulltext keys will slow down the querying on that column?
Actually all indexes are made to make searching a lot faster. Where do you have that they slow things down?

Indexes make inserts/updates/deletes slower, but not selects.

Posted: Sun Jul 23, 2006 3:27 pm
by shoebappa
Yeah, I've never heard that indexes slowing down querying, fulltext or otherwise. The point of them is to speed things up, and they are seperate from the tables and used when necessary.

What it would slow down is that they have to be maintained, so on insert and update the index needs to also be updated, but these are typically few and far between. Selects performance is what you typically want to improve...

MySQL full-text searches are much faster and more powerful and than any search algorithm you're going to put together with regexes, unless of course you're only trying to search for that one word in a 10 row result set as demoed in your code.