Gossamer Forum
Home : Products : DBMan : Installation :

how to do multiple word search in a field

Quote Reply
how to do multiple word search in a field
Hi,
I'm building a photo gallery, and intend to classify via using a KEYWORD field...eg. 'nature scenery trees oak'
Let's say that i want to search 'tree scenery' via the standard search form...unfortunately it doesn't allow searches for multiple words...I can search 'tree' fine and ' scenery' fine by themselves..but when i search 'tree scenery' it balks...
I'm a novice Perl user, so i was wondering is there a solution to this..? Thanks for a wonderful script!
Quote Reply
Re: how to do multiple word search in a field In reply to
The script does not do OR searches on the same field. It probably should though.

A workaround is to define categories (this may not be a good idea in your situation) and then just have checkboxes for each category. A user can then just check off each entry.

Hope that helps,

Alex
Quote Reply
Re: how to do multiple word search in a field In reply to
Hi,
Solved my problem with multiple field searches by using the regular expression option..i.e.

1. for AND multiple words

regex = word1/&&/word2/&&/word3 ..etc

2. for OR multiple words

regex = word1|word2|word3 ..etc

I've modified the script to accept multiple word searches and it works fine so far..

Quote Reply
Re: how to do multiple word search in a field In reply to
Hi Kenneth,

Did you implement this on the users side or the script side? i.e. do you ask your user to enter in phrases like 'nature|scenery|trees' or do you take the query and fix it up inside of the code?

One thing to watch out for is that you quote any words the user enters. If he puts in something like 'nature/' that can mess up your regex.

Cheers,

Alex
Quote Reply
Re: how to do multiple word search in a field In reply to
This was a good thread from a while back that could help with the 'cross-field multi-word searching' problem.

Kenneth, could you answer Alex's question on server-side vs. user-side queries?

If you did find a way to parse multiple words typed in by the user and create a regex search, I'd love to know how you did it!!!

Thanks.
Quote Reply
Re: how to do multiple word search in a field In reply to
Hi,

Well, I did it in the server side by modifying the query() function. Basically, I used the regexp function in the search function in DBMAN...if u want
to search for e.g. 2 words

- 'word a' AND 'word b' then the regexp is 'word a'/ && /'word b'
- 'word a' OR 'word b' then the regexp is 'word a'/ | | /'word b'

so, u need to modify the sub query function where it builds the regexp

# 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.

foreach $field (@search_fields) {

my $tmpreg = "$in{$db_cols[$field]}";

# u can use a marker of whether u want AND or OR for ur multiple words
# e.g. u can use a SELECT field to show option to match ALL words, or match
ANY word
# in the HTML.PL under view_search
# <select>
# <option name=match_all value=1>Match All words
# <option name=match_all value=0> Match Any word
# this will allow the query function to generate the appropriate regexp for
the multi word search

$tmpreg=~s/\s+/ /g; # strip excess spaces

($in{'match_all'}) ? $tmpreg=~s/\s/\/i&&\//g) :
($tmpreg=~s/\s/\/i|\//g); # generate the regexp of AND and OR depending on
ur choice

}

}

($in{$db_cols[$field]} eq "*") and ($tmpreg = ".*"); # A "*" matches
anything.

# add /i after $tmpreg for case insensitive search

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

I'm a novice perl programmer and i guess this is quite a kludge, but at least the user doesn't need to know regex to do multiple word searches. The only thing that the user needs to do is make sure he separates each word with a space...but i guess one could use commas as delimiters too. The best would be to allow things like

'word a' AND 'word b' OR 'word c'...etc

i.e. english like statements..then to translate it into regex

Hope there's a more elegant solution though Smile
Quote Reply
Re: how to do multiple word search in a field In reply to
Is this a 'cross-field multi-word searching' solution or just single-field?
If not, is there a 'cross-field multi-word searching' solution for dbman?
Is there anything missing in script above, because it doesnīt work yet.
Quote Reply
Re: how to do multiple word search in a field In reply to
Hi,could anyone tell me what wrong with this:
I get a syntax error on

($in{'match_all'}) ? $tmpreg=~s/\s/\/i&&\//g) :

and here:

$regexp_func[$field] = eval "sub { m/$tmpreg/i }";
$regexp_bold[$field] = $tmpreg;
}
( how many "}" do I need here )

As you probably can see I donīt know much about perl. But anyway , please help.
I really need this feature.
Thanks
Quote Reply
Re: how to do multiple word search in a field In reply to
Hi,

I think
($in{'match_all'}) ? $tmpreg=~s/\s/\/i&&\//g) :

should end with ; insted of : (i think)..

but it seems the script still not works if you change : to ;

Who can help??