[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ic] Prevent search from matching on html


On Oct 26, 2006, at 5:29 PM, Kevin Walsh wrote:

Josh Lavin <suppressed> wrote:
I am finding that when we use HTML in our product descriptions, the
search results will include products where an HTML tag matched the
search query.

Simple example: if my description contains "<h2>Features</h2>" and
someone searches for 'h2', then that product will be returned in the
results.

I would like to avoid this, and figured I needed a custom SearchOp,
but I'm having no luck with this one:

CodeDef not_tags SearchOp
CodeDef not_tags Routine <<EOR
sub {
         my ($self, $i, $pat) = @_;

         return sub {
             my $string = shift;
             $string =~ s:<[/\w].*?\s?/?>::gi;
             return $string;
         };
}
EOR

The idea is to remove any HTML tags before searching. Any ideas?

You are always returning a true value.  A SearchOp's coderef needs
to return true if a match is found or false if no match is found.

Try something like this instead:

    CodeDef not_tags SearchOp
    CodeDef not_tags Routine <<EOR
    sub {
        my ($self, $i, $pat) = @_;
        $pat = qr/$pat/i;

        return sub {
            my $string = shift;

            $string =~ s:<[/\w].+?>::gi;
            return $string =~ $pat;
        };
    }
    EOR

Thanks, Kevin. That did the trick, but only after I made a change to Search.pm, which you mentioned here: http://www.icdevgroup.org/pipermail/interchange-users/2006-July/ 045747.html

I was getting the same "Can't use an undefined value as an ARRAY reference" error.

After making the change, I then started getting random bareword errors. When searching for 'berkey' (a real product), sometimes it would get the result, and other times this:

Limit subroutine creation: Bad code: Bareword found where operator expected at (eval 1224) line 8, near "] aqberkey" syntax error at (eval 1224) line 8, near "] aqberkey "

This is mentioned here:
http://www.icdevgroup.org/pipermail/interchange-users/2004-June/ 039504.html

Another thing, perhaps unrelated, is how to search multiple fields with a custom SearchOp and get it to work on all? E.g.:
sf=description
vs.
sf=description,comment

Thanks much,
--
Josh Lavin
Kingdom Design   http://www.kingdomdesign.com/

_______________________________________________
interchange-users mailing list
suppressed
http://www.icdevgroup.org/mailman/listinfo/interchange-users


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.