You are here:  » Search to include all terms searched


Search to include all terms searched

Submitted by TWDesigns on Wed, 2008-07-30 01:54 in

Hi I'm was looking for a topic that may already cover this but haven't seen one.

Lets say someone searches for Happy Halloween
Search results include all titles that have either "happy" or "halloween" or both.

I would like it to only return products found that match "happy halloween".

Suggestions?

Thanks,
Tommy

Submitted by support on Wed, 2008-07-30 07:34

Hello Tommy,

The easiest way to do this is to use the normal (not Full - Text index) search method for all queries. In search.php; look for the following code on line 77:

        if ($useFullText)

...and simply change this to:

        if (FALSE)

Cheers,
David.

Submitted by TWDesigns on Wed, 2008-07-30 16:29

Hey David,

Line 77 was already set to FALSE but above it around line 69 it said TRUE so I changed it there and the problem is now resolved :)

Thanks!
Tommy.

Submitted by hockey on Thu, 2008-07-31 07:05

I just installed the script and it's great! Awesome job.

I have a VERY similar question as Tommy.

I was wondering how I would modify so the search is narrowed down. Currently (as Tommy pointed out) if you do a search for "shiny red shoes", you will get results with any of those three words included in the product name. However, how would you set it up so the query would pull results that only included all of those three words...

Meaning...

Expensive shiny red shoes
Red Shiny Shoes
Shoes Shiny Red
etc...

could all theoretically be returned.

So where Tommy wanted an exact phrase match, I just want to make sure results are returned that simply include at least all of the searched for terms. Is this possible?

Submitted by support on Thu, 2008-07-31 08:21

Hi,

The basic search method should work like this already; but to make the full text search method (used where all words are at least 4 characters) you need to force "BOOLEAN MODE" and prefix each word with "+" to indicate "AND". In search.php, look for the following code starting at line 82:

          $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($parts[0])."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($parts[0])."') GROUP BY name";
          $sqlResultCount = "SELECT COUNT(DISTINCT(name)) as resultcount FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($parts[0])."')";

...and change this to:

          $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)";

Cheers,
David.

Submitted by Syed on Fri, 2008-08-01 23:40

Hi David,

Instead of Advanced search, i implemented your above code and it works perfect. The number of products listed are less.

You are doing a great job.

Cheers,
Syed

Submitted by support on Tue, 2010-06-15 12:17

Hi everyone,

Just updating this thread to document enabling BOOLEAN MODE (all words) full text search for the latest distribution.

Look for the following code beginning at line 155 of search.php:

          $where = "MATCH ".$matchFields." AGAINST ('".database_safe($parts[0])."')";
          $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH ".$matchFields." AGAINST ('".database_safe($parts[0])."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." GROUP BY name";
          $sqlResultCount = "SELECT COUNT(DISTINCT(name)) as resultcount FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere;

...and REPLACE with:

          $match = "+".str_replace(" "," +",$parts[0]);
          $where = "MATCH ".$matchFields." AGAINST ('".database_safe($match)."' IN BOOLEAN MODE)";
          $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH ".$matchFields." AGAINST ('".database_safe($match)."' IN BOOLEAN MODE) AS relevance FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." GROUP BY name";
          $sqlResultCount = "SELECT COUNT(DISTINCT(name)) as resultcount FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere;

Cheers,
David.

Submitted by Perce2 on Thu, 2011-03-24 18:58

Hi, is there any chance this page, including both solutions, can be updated to include the latest distribution please ?

Thanks,
Graham

Submitted by support on Thu, 2011-03-24 19:27

Hi Graham,

In the latest distribution (12/10A) the basic search method (used if query contains stopwords or any keyword < 4 characters) is already logic AND. To modify the full text search as above, look for the following code at line 215:

          $where = "MATCH ".$matchFields." AGAINST ('".database_safe($parts[0])."')";

...and REPLACE with:

          $where = "MATCH ".$matchFields." AGAINST ('+".str_replace(" "," +",$parts[0])."' IN BOOLEAN MODE)";

Cheers,
David.
--
PriceTapestry.com

Submitted by Perce2 on Thu, 2011-03-24 20:04

Sorry, don't understand, is part of the reply missing ?
Are you saying we can't achieve this kind of search result with the latest release of PT ?

Thanks,

Graham

Submitted by support on Thu, 2011-03-24 20:06

Hi Graham.

Sorry - a character in my reply prevented most of it from being displayed - corrected above...

Cheers,
David.
--
PriceTapestry.com

Submitted by support on Thu, 2014-10-02 12:24

Hi everyone,

BOOLEAN MODE modification for latest distributions, in search.php look for the following code beginning at line 246 (13/03A through 14/06A), line 282 (since 15/09A)

$where = "MATCH ".$matchFields." AGAINST ('".database_safe($parts[0])."')";
$sql = "SELECT SQL_CALC_FOUND_ROWS id,COUNT(id) AS numMerchants,MIN(price) as minPrice, MATCH ".$matchFields." AGAINST ('".database_safe($parts[0])."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." GROUP BY search_name";

...and REPLACE with:

$match = "+".str_replace(" "," +",$parts[0]);
$where = "MATCH ".$matchFields." AGAINST ('".database_safe($match)."' IN BOOLEAN MODE)";
$sql = "SELECT SQL_CALC_FOUND_ROWS id,COUNT(id) AS numMerchants,MIN(price) as minPrice, MATCH ".$matchFields." AGAINST ('".database_safe($match)."' IN BOOLEAN MODE) AS relevance FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." GROUP BY search_name";

Cheers,
David.
--
PriceTapestry.com