You are here:  » Negative Keywords In Plugin


Negative Keywords In Plugin

Submitted by stew on Wed, 2015-08-12 07:43 in

Hi David,

Hope all well, found one answer for this in the forum but think the code has changed, is there a way to implement negative keywords in the WP plugin?

e.g: [pto search="table -plastic"]

Thanks,

Stew

Submitted by support on Wed, 2015-08-12 09:55

Hello Stew,

What you need to do is enable BOOLEAN MODE in the full text search handling. To do this, edit pto_search.php and look for the following code at line 186:

  $pto_searchSelect .= ",MATCH ".$matchFields." AGAINST ('".$wpdb->escape($parts[0])."') AS relevance";
  $where = "MATCH ".$matchFields." AGAINST ('".$wpdb->escape($parts[0])."')";

...and REPLACE with:

  $pto_searchSelect .= ",MATCH ".$matchFields." AGAINST ('".$wpdb->escape($parts[0])."' IN BOOLEAN MODE) AS relevance";
  $where = "MATCH ".$matchFields." AGAINST ('".$wpdb->escape($parts[0])."' IN BOOLEAN MODE)";

With that in place, you can construct queries using any of boolean mode's operators, which are all described on the the following page:

https://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Thu, 2015-08-13 08:06

Hi David,

Many thanks for that, I've put into place, not sure am doing anything wrong but now for example when I use:

[pto search="+table -plastic"]

The results are all plastic tables first and then other tables next. Rather than excluding results with the word 'plastic'

Any ideas here?

Many Thanks

Submitted by support on Thu, 2015-08-13 08:32

Hi Stew,

Ah - the query value is being normalised (which strips "+" and "-" by default) but this can be safely removed which will enable you to use the full flexibility of booleam mode queries. To do this, in pto.php look for the following code at line 159:

  $pto_q = (isset($pto_q)?pto_common_normalise(urldecode($pto_q),":\."):"");

...and REPLACE with:

  $pto_q = (isset($pto_q)?urldecode($pto_q):"");

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Thu, 2015-08-13 09:07

Hi David,

Brilliant - many thanks that's now working - kind of..

What I'm seeing is if I use the query:

[pto search='table -plastic']

This works well & removes the plastic tables.

However when's its a query involving 2 words e.g.:

[pto search='mens tanktop']

and I need to remove woollen results for example:

[pto search='mens tanktop -woollen']

I end up with zero results "Sorry, there are no results to display."

I've tried using the + symbols e.g:

[pto search='+mens +tanktop -woollen']

but getting the same thing,

Any ideas what this could be?

Many Thanks,

Submitted by support on Thu, 2015-08-13 09:26

Hi Stew,

I'm not sure this will make a huge difference but the first thing I would suggest is to remove the stemming, which automatically adds the non-plural of any keyword ending in "s" so that would affect this example - to do this, edit pto_search.php and look for the following code beginning at line 165:

        foreach($words as $word)
        {
          if (substr($word,-1)=="s")
          {
            $newWords[] = substr($word,0,-1);
          }
        }

...and either comment out or delete that section...

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Thu, 2015-08-13 09:43

Hi David,

Thanks very much for that - it's working, with a few different negative variations it seems to exclude OK!

Last query on this if you're able to help re 3 letter words for example:

[pto search="snowboard -hat"]

'hat' never seems to get picked up, or 'bag' for example - is there a way to add three letter words to the query length?

Submitted by support on Thu, 2015-08-13 10:13

Hi Stew,

If you have access to your server's MySQL configuration you may be able to reduce the minimum word length, which is configurable in recent versions of MySQL through the ft_min_word_len parameter in my.cnf. After changing the minimum word length you would need to re-import all feeds for in order to trigger re-building of the index.

With that in place, there is also a check in the SQL construction code that uses a basic search method if any keyword in the query is less than 4 characters, so if you change the minimum word length to 2, edit pto_search.php and look for the following code at line 150:

  if (strlen($word) <= 3 || in_array(strtolower($word),$pto_stopwords))

...and REPLACE with:

  if (strlen($word) < 2 || in_array(strtolower($word),$pto_stopwords))

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Tue, 2015-08-18 09:04

Hi David,

Many thanks for that is a great help.

Discussing with host, they're saying as currently on a shared server I'm limited to access- will see what they'll let me do! If not will try & get moved to a VPS.

Thanks,

Stew