You are here:  » External.php Adding negative terms to search query.

Support Forum



External.php Adding negative terms to search query.

Submitted by Stew on Wed, 2011-03-23 23:39 in

Hi there, hope all is well,

I'd like to be able to add multiple negative terms to a search query if possible, for example something similar to the following calling code:

$queryterm ="jumper -knitted -wooly";

Is this possible with external.php?

Many Thanks

Submitted by support on Thu, 2011-03-24 09:18

Hi Stew,

Sure - the BOOLEAN mode modification can be added to your external.php. In that file, perform a Search and Replace of

AGAINST ('".database_safe($parts[0])."')

REPLACE with:

AGAINST ('".database_safe($parts[0])."' IN BOOLEAN MODE)

You will then be able to set $_GET["q"] parameter in your calling code exactly as in your post by prefixing negative keywords with the - sign.

That covers the full text index, but bear in mind that any query containing a stopword (words like "this", "and" etc. that are considered too frequent to index) or any keyword less than 4 characters uses the basic search method. To support the same -keyword for basic searches, search for the following code beginning around line 480:

          foreach($words as $word)
          {
            $where = "(";

...and REPLACE with:

          foreach($words as $word)
          {
            if (substr($word,0,1)=="-")
            {
              $wheres[] = " name NOT LIKE '%".database_safe(substr($word,1))."%' ";
              continue;
            }
            $where = "(";

Cheers,
David.
--
PriceTapestry.com

Submitted by Stew on Thu, 2011-03-24 18:44

Hi David,

Many thanks for that,

Its strange, I implemented the first part:

------------------------------------------

AGAINST ('".database_safe($parts[0])."')

REPLACE with:

AGAINST ('".database_safe($parts[0])."' IN BOOLEAN MODE)

------------------------------------------

and then when I added a negative term no results showed up at all.

Then when I also implemented the second part:

------------------------------------------

foreach($words as $word)
{
$where = "(";

...and REPLACE with:

foreach($words as $word)
{
if (substr($word,0,1)=="-")
{
$wheres[] = " name NOT LIKE '".database_safe(substr($word,1))."' ";
continue;
}
$where = "(";

------------------------------------------

The results showed up again as normall but with no negative term products removed,

Not sure what I've done here?!

Thanks,

Stew

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

Hi Stew,

Please could you email me your external.php and I'll check it out for you..

Cheers,
David.
--
PriceTapestry.com

Submitted by Stew on Mon, 2011-10-24 16:55

Hi David,

I have the same issue here with a new site/installation of PT - is it OK to email you the external.php again?

Stew

Submitted by support on Mon, 2011-10-24 18:37

Hi Stew,

I just checked back over our email dialog from the previous installation and I recall that I missed out the wildcard (%) characters from the NOT LIKE clauses, so I have corrected this in the modification described above (this comment). If still not working as expected sure forward the file and an example query and I'll take a look...

Cheers,
David.
--
PriceTapestry.com

Submitted by Stew on Tue, 2011-10-25 16:15

Brilliant - thanks for looking at that David. Will give it a go now.

Submitted by Stew on Wed, 2011-10-26 16:36

Worked perfectly - many thanks for that :-)