Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I am working on a search engine for a knowledge base I have been involved with, and I think I have programmer's block! I am having trouble getting my head around the structure I need to generate for the different combinations required.

There are many fields, the search should AND the criteria in each box and OR them between each field.
e.g.
Article ID field - 12 AND 43 AND 78
OR
Resolution field - word1 AND word2

There is one search I want to combine two fields called 'subject' and 'keywords', so there will exist a single search box to look at both. Spaces are delimiters.

foreach (@{$parameters->{$_}}) { if($_ ne "" && $_ !~ /^all/i) { $query_field = $_; # Remove unnecessary spaces. $_ =~ s/\s{2,}/ /g; $_ =~ s/^\s+//g; $_ =~ s/\s+$//g; @space_split_array = split / /, $_; foreach (@space_split_array) { if ($_ =~ /^\d+$/ && scalar(@space_split_array) == 1 && $_ eq $query_field) { $where_clause .= "($real_field_name = $_) OR " +; } else { $where_clause .= "($real_field_name LIKE '%$_% +') AND "; } } $where_clause =~ s/AND $/OR /; } }

The above code works for searching single fields. The first part of the SQL query already exists, here we create the WHERE clause to find the information specified by the user. The space_split_array takes in a string entered by a user in a search box e.g. "word1 word2 word3" and processes each sequence of chars separated by a space. AND is placed between criteria and OR is appended to the end to OR between search fields.

In the subject_keyword field, if I type 'apache server', this is what I want to happen:
All articles with:
apache AND server as keywords
AND
apache AND server in the subject
AND
apache as a keyword AND server in the subject
AND
apache in the subject AND server as a keyword

The search should work similarly for 1 word or more, so for one word:
apache as a keyword
AND
apache in the subject
AND
apache in the subject and as a keyword

And for 3 words - K denotes keyword, S denotes subject:
apache server perl
S AND S AND S
S AND S AND K
S AND K AND K
S AND K AND S
K AND S AND K
K AND S AND S
K AND K AND S
K AND K AND K

You can probably see how I am getting confused, with the logic more than anything; I've probably been looking at it too long! I hope you guys can shed some light on this for me.
Thanks in advance, JohnnyC

Replies are listed 'Best First'.
Re: Combining two search fields
by pbeckingham (Parson) on Jun 22, 2004 at 13:03 UTC

    I believe you are confusing AND and OR.

    You need an algorithm that lets you easily generate correct SQL from a list of keywords and a list of subjects. It looks like you are heading in that direction.

Re: Combining two search fields
by jZed (Prior) on Jun 22, 2004 at 17:42 UTC
    I'm afraid there are too many levels of $_ for me to want to try to read your code, perhaps you'd also have an easier time reading it if you named your variables. Also, you did a pretty good job of describing what you want to happen, but you didn't describe how the behaviour of your script is different from that. Does your script misbehave, if so, how does the output differ from what you expect?

    You may also want to look at the various modules on CPAN which buitl SQL queries from perl structures (e.g. SQL::QueryBuilder::Simple)