You are here:  » Expanding search to include custom fields


Expanding search to include custom fields

Submitted by support on Thu, 2016-06-16 12:02 in

Hi everyone,

A couple of users have asked recently about including custom fields in search so I just wanted to document the changes required to do this. Continuing the custom fields example of a new "keywords" field, to include this in search firstly the FULLTEXT index must be re-created to include the custom field. The folowing dbmod.php script will apply this change - run once from the top level of your Price Tapestry installation and then delete the file.

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
$config_databaseDebugMode TRUE;
  if (
$config_searchDescription)
  {
    
$matchFields "name,description,keywords";
  }
  else
  {
    
$matchFields "name,keywords";
  }
  
$sql "CREATE FULLTEXT INDEX name_3 ON `".$config_databaseTablePrefix."products` (".$matchFields.")";
  
database_queryModify($sql,$result);
  print 
"Done.\n";
?>

Don't forget to include the custom field(s) to be added to the FULLTEXT index in the value for $matchFields at both lines 9 and 13. I've coded the above in this way so that the code matches identically that found in search.php.

Next, edit search.php and look for the following code at line 273:

          if ($config_searchDescription)
          {
            $matchFields = "name,description";
          }
          else
          {
            $matchFields = "name";
          }

...and REPLACE with:

          if ($config_searchDescription)
          {
            $matchFields = "name,description,keywords";
          }
          else
          {
            $matchFields = "name,keywords";
          }

Finally, since Price Tapestry falls back onto a basic search method if FULLTEXT is not an option for the query (one or more keywords is a stopword or less than 4 characters in length), look for the following code at line 269:

            $where .= "search_name LIKE '%".database_safe($word)."%'";

...and REPLACE with:

            $where .= "search_name LIKE '%".database_safe($word)."%'";
            $where .= " OR keywords LIKE '%".database_safe($word)."%'";

Equivalent Changes for PriceTapestry.org for WordPress Plugin

Edit pto_search.php and look for the following code at line 177:

        if ($pto_config_searchDescription)
        {
          $matchFields = "name,description";
        }
        else
        {
          $matchFields = "name";
        }

...and REPLACE with:

        if ($pto_config_searchDescription)
        {
          $matchFields = "name,description,keywords";
        }
        else
        {
          $matchFields = "name,keywords";
        }

And then the following code at line 200:

          $where .= "search_name LIKE '%".$wpdb->escape($word)."%'";

...and REPLACE with:

          $where .= "search_name LIKE '%".$wpdb->escape($word)."%'";
          $where .= " OR keywords LIKE '%".$wpdb->escape($word)."%'";

Cheers,
David
--
PriceTapestry.com