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


In reply to Combining two search fields by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.