Gossamer Forum
Home : Products : DBMan : Customization :

Boolean Search Script

Quote Reply
Boolean Search Script
I just applied changes to db.cgi to allow a Boolean search. The script I used included options for AND, OR, and MATCH ALL. The problem I am having is I have lost the ability to search on any part of a word. Can anyone tell me what I need to change so I can search on keyword (for example) webmaster by just entering the word master? Right now I need to use the whole word or I get an error. If the field contains (for example) My Own Webmaster , I can find it using My or Own or Webmaster, but not master. A whole word must be used. This partial word search used to work before I added the boolean search script but I can't figure out why it doesn't anymore. Any help is greatly appreciated. The script I used is shown below. I used the radio buttons in my form:


Developed by: leisurelee

DBMan - Code Modification - Boolean v1.1
======================================

Named after the nineteenth-century mathematician George Boole, Boolean logic is a form of algebra in which all values are reduced to
either TRUE or FALSE. Boolean logic is especially important for computer science because it fits nicely with the binary numbering
system, in which each bit has a value of either 1 or 0. Another way of looking at it is that each bit has a value of either TRUE or FALSE.

Boolean Operators included in this script are AND and OR :

x AND y Result is TRUE if both x and y are TRUE. Otherwise the result is FALSE.
x OR y Result is TRUE if either x or y is TRUE. Otherwise the result is FALSE.

=============== INSTRUCTIONS: ==================

Step 1:
Place the following code in sub query of your db.cgi file. It replaces the 9 lines of code between:

# Now let's build up all the regexpressions we will use. This saves the program
# from having to recompile the same regular expression every time.

and:
# Now we go through the database and do the actual searching.
# First figure out which records we want:


if($in{'bool'}) {
($in{'bool'} eq 'and') ? ($bool = '&&') : ($bool = '||');

foreach $field (@search_fields) {
$finalreg = '';

foreach my $word (split /\s/, $in{$db_cols[$field]}) {
next if ($word =~ /^\s*$/);
$tmpreg = $word;
(!$in{'re'}) and ($tmpreg = "\Q$tmpreg\E");
($in{'ww'}) and ($tmpreg = "\\b$tmpreg\\b");
(!$in{'cs'}) and ($tmpreg = "(?i)$tmpreg");
$finalreg .= " m/$tmpreg/o $bool";
}
if ($in{$db_cols[$field]} eq "*") {
$finalreg = "m/.*/";
}
else {
chop $finalreg; chop $finalreg;
}
$regexp_func[$field] = eval "sub { $finalreg }";
}
}

else {

foreach $field (@search_fields) {
my $tmpreg = "$in{$db_cols[$field]}";
(!$in{'re'}) and ($tmpreg = "\Q$tmpreg\E");
($in{'ww'}) and ($tmpreg = "^$tmpreg\$");
(!$in{'cs'}) and ($tmpreg = "(?i)$tmpreg");
($in{$db_cols[$field]} eq "*") and ($tmpreg = ".*");

$regexp_func[$field] = eval "sub { m/$tmpreg/o }";
$regexp_bold[$field] = $tmpreg;
}
}

Step 2:

Now decide where you wish to permit boolean searching, and how you wish to provide boolean options. Below are three examples of
how you could call DBMan from an external website to perform a boolean/keyword search:

SELECT BOX:

<FORM ACTION="/cgi-bin/db.cgi" METHOD="GET">
<INPUT TYPE="HIDDEN" NAME="db" value="default">
<input type="HIDDEN" name="uid" value="default">
<input TYPE="TEXT" NAME="keyword" SIZE=40 MAXLENGTH=255>

<SELECT NAME="bool">
<OPTION VALUE="key">Match Phrase
<OPTION VALUE="and">Match All Words
<OPTION VALUE="or">Match Any Words
</SELECT>

<input TYPE="HIDDEN" NAME="mh" VALUE="10">
<input TYPE=SUBMIT NAME="view_records" VALUE="View Records">
</form>

BUTTONS:

<FORM ACTION="/cgi-bin/db.cgi"
METHOD="GET"> <INPUT TYPE="HIDDEN" NAME="db" value="default">
<input type="HIDDEN" name="uid" value="default">
<input TYPE="TEXT" NAME="keyword" SIZE=40 MAXLENGTH=255>

<INPUT TYPE="RADIO" NAME="bool" VALUE=""> Match Phrase<BR>
<INPUT TYPE="RADIO" NAME="bool" VALUE="and"> Match All Words<BR>
<INPUT TYPE="RADIO" NAME="bool" VALUE="or"> Match Any Words<BR>

<input TYPE="HIDDEN" NAME="mh" VALUE="10">
<input TYPE=SUBMIT NAME="view_records" VALUE="View Records">
</form>

HIDDEN FIELD:

<FORM ACTION="/cgi-bin/db.cgi" METHOD="GET">
<INPUT TYPE="HIDDEN" NAME="db" value="default">
<input type="HIDDEN" name="uid" value="default">
<input TYPE="TEXT" NAME="keyword" SIZE=40 MAXLENGTH=255>

<INPUT TYPE="HIDDEN" NAME="bool" VALUE="and">

<input TYPE="HIDDEN" NAME="mh" VALUE="10">
<input TYPE=SUBMIT NAME="view_records" VALUE="View Records">
</form>

Extra Code - Results Bolding

4/26/00 - The following code was developed to bold boolean search results, much as the default DBMan does for keyword search
results. This code is not required for the boolean MOD to function properly.

Step 1:
In the boolean code above, change this section of code:

$finalreg .= " m/$tmpreg/o $bool";
}
if ($in{$db_cols[$field]} eq "*") {

to the code provided below:

$finalreg .= " m/$tmpreg/o $bool";
$k++;
$regexp_bold[$k] = $tmpreg;
}
if ($in{$db_cols[$field]} eq "*") {



Step 2:
Further down, replace this section of code:

# Bold the results
if ($db_bold and $in{'view_records'}) {
for $i (0 .. (($#hits+1) / ($#db_cols+1)) - 1) {
$offset = $i * ($#db_cols+1);
foreach $field (@search_fields) {
$hits[$field + $offset] =~ s,(<[^>]+> )|($regexp_bold[$field]),defined($1) ? $1 : "$2",ge;
$hits[$field + $offset] =~ s,(<[^>]+> )|($regexp_bold[$in{'sb'}]),defined($1) ? $1 : "$2",ge; }
}
}
return ("ok", @hits);
}

with the code provided below:

# Bold the results
if ($db_bold and $in{'view_records'}) {
for $i (0 .. (($#hits+1) / ($#db_cols+1))- 1) {
$offset = $i * ($#db_cols+1);
foreach $field (@search_fields) {
if($in{'bool'}) {
for $m (1 .. $k) {
$hits[$field + $offset] =~ s,(<[^>]+>)|($regexp_bold[$m]),defined($1) ? $1 : "<B>$2</B>",ge;
@html_search_form;
}
} else {
$hits[$field + $offset] =~ s,(<[^>]+> )|($regexp_bold[$field]),defined($1) ? $1 : "<B>$2</B>",ge;
}
}
}
}
return ("ok", @hits);
}

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

Credits:
This code was developed April 10, 2000 with the assistance of Alex (author, DBMan), leisurelee, and OldMoney (code wizard).
The Bold code was provided by Alan Pollenz (DBMan user coder).

To see this code in development, and to check for any new modifications, visit the DBMan forum thread at:
http://gossamer-threads.com/p/002425.html also see: http://gossamer-threads.com/p/112154