I noticed somewhere on the site that the search function's been improved - when you type "large television" and there's nothing large, it'll show you televisions anyway.
Ideal for 99% of scripts - but unfortunately not mine! On my site, if you search for "silver pendant" (no quotes...), you want a pendant that's silver, not just anything that's either silver or a pendant.
Hope that makes sense. If you take a look at http://www.jewelryresults.com/search.php?q=silver+pendant - you'll see why. It's even more necessary to do it this way with the "top results" section.
Got any old code I can use to make "more than one worded" queries find items with only both terms in?
Thanks in advance
Hi David,
So from what I've read we need to basically get the server to add a "+" to each word if it's got more than two - vaguely correct?
Boolean full-text searches seem to be a good solution.
I do have version 4.27, so that shouldn't be a problem. If you're going to do this at some stage anyway, it would be great if you could have a go at the code and I'll let you know if it works.
Cheers
Here's the code to generate the BOOLEAN MODE query - i've looked at the resulting SQL and it looks the same as the example in the docs so fingers crossed...!
<?php
if (strlen($parts[0]) > 3)
{
$match = "+".str_replace(" "," +",$parts[0]);
$sql = "SELECT *, MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($match)."' IN BOOLEAN MODE) AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($match)."' IN BOOLEAN MODE) GROUP BY name";
$sqlResultCount = "SELECT COUNT(DISTINCT(name)) as resultcount FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($match)."' IN BOOLEAN MODE)";
$orderBySelection = $orderByFullText;
}
else
{
// .... rest of script .....
?>
The code above replaces the the code used to generate the SQL for queries greater than 3 characters in length - look for the if (strlen($parts[0]) > 3) on line 63 of search.php (same place you would have made the changes above). The changes are to the 2 SQL statment construction lines, and the $match = statement which adds the + to the front of each word in the users' query.
Cheers!
David.
I wish I had known that - I've just spent £200 on an external search engine script for the site - :-(
Simon
Simon - what script did you go for ?
ive had a look around at a few external search scripts myself
ive been looking to get the edge with search results returned by the script- so the buyer gets exactly what they want and i get a commision :-)
- looking forward to trying davids new example though
I would happily pay for a really powfull search function to add to this script (hint hint -David cough, cough pro version )
It would certainly be tricky to setup but something might be possible with Lucene:
Hi Harvey,
You can switch back to the "old way" really easily. In search.php on Line 63 you'll see this code:
if (strlen($parts[0]) > 3)
That code forces the full text index (as you described) for queries greater than 3 characters in length. To go back to the old method for all queries, the easiest way is to change this to:
if (0)
...in other words "never do this, always use the old method". However, the old method is not without its shortcomings. You mentioned that you want both "silver" AND "pendant" - the old search method would only find a "silver pendant", it would not match "Pendant, Silver" for example.
To do exactly what you want requires boolean full text matching where every search term is preceeded by a "+" character. This requires MySQL version 4.01 or later:
http://dev.mysql.com/doc/refman/4.1/en/fulltext-boolean.html
I've not yet written the code to support boolean full text matching but it should be a straight forward mod - see if you've got MySQL 4.1 or later (version checker) and if you have i'll have a "guess" at the code for you and see if it works!
Cheers,
David.