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
In reply to Combining two search fields by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |