You are here:  » Adding category to search function

Support Forum



Adding category to search function

Submitted by speak2daryl on Tue, 2009-11-10 17:03 in

Hi,

I have added the extended the search to include the description as detailed in node 1224, is it possible to extend the search to the category also?

For example, if someone searches for LCD Televisions it would pick up the category and display some of the results from the LCD Television category.

I am looking to implement this because some of my merchants have poor product titles and description.

Thanks

Submitted by support on Tue, 2009-11-10 18:53

Hi Daryl,

The same process can be easily extended to the category field. In terms of the SQL required to create the index, it would be:

CREATE FULLTEXT INDEX namedescriptioncategory ON products (name,description,category);

...or of course the corresponding makeIndex.php script:

<?php
  set_time_limit
(0);
  
ignore_user_abort();
  require(
"includes/common.php");
  
$sql =
  
"CREATE FULLTEXT INDEX namedescriptioncategory
     ON "
.$config_databaseTablePrefix."products (name,description,category)";
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

...and with that in place, the modification in search.php would be to do a Search and Replace on:

MATCH name

with:

MATCH (name,description,category)

The above is all that is required for the Full Text index to support the category field. In addition, for the basic search method (used when any keyword in the query is less than 4 characters), in search.php look for the following code on line 91:

$wheres[] = "search_name LIKE '%".database_safe($word)."%'";

...and REPLACE this with:

$wheres[] = "(search_name LIKE '%".database_safe($word)."%' OR description LIKE '%".database_safe($word)."%' OR category LIKE '%".database_safe($word)."%')";

Hope this helps!

Cheers,
David.

Submitted by speak2daryl on Wed, 2009-11-11 16:31

Hi,

That works really well, thanks for all your help David.

Submitted by lunen on Sat, 2011-10-01 03:54

David,

I would like to do this but with more categories. I added "cat2" and "cat3" as fields that I capture from datafeeds. Can I just follow the methods listed above and then have the line?:

CREATE FULLTEXT INDEX namedescriptioncategory ON products (name,description,category);

changed to

CREATE FULLTEXT INDEX namedescriptioncategory ON products (name,description,category,cat2,cat3);

Submitted by support on Sat, 2011-10-01 10:11

Hi lunen,

Yes that will do the trick - with the index built in the latest version look for the following code at line 215 of search.php

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

...and REPLACE with:

          $matchFields = "name,description,cagtegory,cat2,cat3";
          $where = "MATCH ".$matchFields." AGAINST ('".database_safe($parts[0])."')";

(where the field list in $matchFields matches the fields you created the index against)

Cheers,
David.
--
PriceTapestry.com

Submitted by lunen on Sun, 2011-10-02 22:36

just curious, were does the index reside? I don't see it on phpmadmin?

Submitted by support on Mon, 2011-10-03 07:03

Hi Lunen,

If you browse to the structure page of a table in phpMyAdmin below the structure information there should be an Indexes table showing all indexes created on that table...

Cheers,
David.
--
PriceTapestry.com