Gossamer Forum
Home : Products : DBMan : Customization :

How to do an AND search with multiple terms?

(Page 1 of 2)
> >
Quote Reply
How to do an AND search with multiple terms?
I've tried searching the posts and FAQ on this, and haven't had much luck. How do I search for two keywords that occur somewhere in the field, but aren't necessarily right together? For example, how would I get a hit while searching for "Four forefathers" in the Gettysburg address?

I can do OR searches with | and regular expressions.

I found some old threads several years ago that talked about a Boolean mod.

Any suggestions? Thanks
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Using your example, I think you would get the correct result if you searched for

Four.*fathers

as a regular expression. The .* characters together mean "one or more characters." It does require that the words are in the same order, so that it wouldn't return something like "When our fathers were four years old...."


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Thanks JPDeni. It would be nice if there were a simply "And" operator like there is for "Or", but at least this is a way to accomplish the task (if you put the search term in both ways).
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
You might try:

(four.*fathers)|(fathers.*four)

as a regular expression.

I'm not sure that will work and I don't have an instance of DBMan handy to test it out, so if you would test it, that would be great. If it does work, we might be able to work out some code so that all a user would have to do is enter

four fathers

and select a checkbox (a new one that we would create) and it would give the same results.

(Edited to fix a dumb mistake of mine)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.

Last edited by:

JPDeni: Feb 17, 2006, 5:56 AM
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Yes!! It works once you put the * after the period in both terms:

(four.*fathers)|(fathers.*four)

If you could help me with the code for creating a new checkbox and entering two terms, that would be great! Would both of the terms have to be a single word?
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Coolness!!! :-D

Quote:
Would both of the terms have to be a single word?

I'm not sure. I think we'll start with single-word search terms to see if it can be done and then expand it to phrases. This is a completely different direction than I've gone in before. I may not be able to get back to this again today. (Family stuff.)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Very cool, let me know how I can help with the testing and thanks again for your help Cool
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Okay. I think this should work. (How many times have I said that and been wrong! LOL!!)

In html.pl, sub html_search options, after

Code:
INPUT TYPE="TEXT" NAME="keyword" SIZE=15 MAXLENGTH=255> Keyword Search <FONT SIZE=-1> (will match against all fields)</FONT><BR>


add
Code:
<INPUT TYPE="CHECKBOX" NAME="and"> Match separate words<BR>


(You can probably come up with some better wording for this.)


In db.cgi, sub query, after

Code:
if ($in{'keyword'}) { # If this is a keyword search, we are searching the same
$i = 0; # thing in all fields. Make sure "match any" option is
$in{'ma'} = "on"; # on, otherwise this will almost always fail.


add
Code:

# **************************************************************
if ($in{'and'}) {
@terms = split (/ /, $in{'keyword'});
$in{'keyword'} = '(' . $terms[0] . '.*' . $terms[1] . ')|(' . $terms[1] . '.*' . $terms[0] . ')';
$in{'re'} = "on";
}
# **************************************************************

(You don't really have to put the #*********** lines in, but it will make things easier to find as we work on this.)

At this point, you should be able to search for two words in any order. They have to be in the "keyword" field. If you put more than two words in the field, anything after the second one will be ignored.

I'm pretty sure I can code for searches using phrases. That shouldn't be too hard. I can probably set it up to work on individual fields, rather than just the keyword, but that'll take more work. I don't think I'm going to be able to do more than two search terms, though.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Thanks JPDeni! I'll try this tonight and let you know how it works.
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Ok, I tried the code and the new check box is now showing up in my search form. But the db.cgi code doesn't seem to be making any difference. When I try to do a search, I get the same results regardless of whether or not I check the box. I'm just supposed to enter two terms, like "Four forefathers" into the search box right?
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Yes. (Well, if it was the real Gettysburg Address it would be "Four fathers." :-) )

Did you check the new box?


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Yes, I get the same search results regardless of whether I check the new box or not.

And it's actually "Four score and seven years ago, our forefathers" Laugh
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
I'll have to set up a database to check it out.

And, the Gettysburg address starts:

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

You can see it at http://www.loc.gov/...its/gadd/gadrft.html in Lincoln's own handwriting.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Looks like "forefathers" is a common misquote, like here:

http://www.ambrosevideo.com/resources/docs/157.PDF

Thanks for the history lesson, you were right!

Also, thanks for your help with this code :)
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
By the way, if it is helpful, you can see the actual search form I'm using here:

http://trialsofascension.net/...lt&view_search=1
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
The problem is that I don't know enough about what's in your database to know what search terms would give me a return. If I make my own, I can create some records that I know will work. I'm just away from my regular computer and I'm having trouble with some really basic stuff like uploading to my webspace.

And, yes, the Gettysburg address mis-quote is quite common. :-)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
I've got one set up. Go to http://www.jpdeni.com/cgi-bin/dbmtest/db.cgi to test out the search. Go ahead and log in with the admin/admin username and password.

The new "and" search is below the keyword field -- "Match separate words"

I threw in a bunch of quotations. These are the searches I tried:

summer winter -- no options checked -- 1 record
winter summer -- no options checked -- 0 records
summer.*winter -- reg. expr. option checked -- 2 records
winter.*summer -- reg. expr. option checked -- 2 records
summer|winter -- reg. expr. option checked -- 7 records
(summer.*winter)|(winter.*summer) -- reg. expr. option checked -- 4 records
summer winter -- match separate words option checked -- 4 records

You can add some more records if you want to try things out. Each record has a counter and four text fields that you can put anything in.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Are you entering the two words in Text 1 box? Or in 2 different boxes? When I try entering summer.*winter in text box 1 I get no matching records (Reg. Expression checked).

If you want to have a look at my database, try searching on these two words:

combat skill

combat skill (with nothing checked) gets 3 matches
combat.*skill (reg. expression checked) gets 24 matches
skill.*combat (reg. expression checked) gets 18 matches

However, skill combat (search two words checked) gets no matches

Any ideas?
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
Everything has to be entered into the keyword field, below all the text fields. That's the only place that the new code works.

I'll take a look at your database again, using your search terms.

Edited to add:

Try enabling debugging in your .cfg file so I can see what's going on.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.

Last edited by:

JPDeni: Feb 17, 2006, 7:01 PM
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Yes! The problem was that I had changed the name of that field to Quote instead of keyword. So I replaced every occurrence of "keyword" in the db.cgi file with "Quote" and it now works. The strange thing is that there was other code that was using "keyword" and my database has worked fine up to this point. Is anything funky going to happen since I did a universal replace?
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
I don't know if you're going to have problems or not. I think that the keyword field is only used in searches, so you should be okay. If things start acting funky, you may need to look at it again.

Now I'll try working on phrases. I may need to sleep on it, though. I do my best work when I'm dreaming. :-D


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Very cool, thanks again for your help!
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
I was doing more testing this morning, and ran into a snag. Before the code change, I was able to search on multiple fields. For example, I could choose "Communication" from the Category field and type in a word for the Key Words field, and it would only find that word within the Communication category. Now with the code change, it ignores anything I've selected in the Category field. So it returns that word within all the Categories. Any idea on how to fix this?
Quote Reply
Re: [Xerin1] How to do an AND search with multiple terms? In reply to
The way to fix it would be to have the fields the way they're supposed to be, with both a Quote field (to search in the Quote field) and a true keyword field, which would search on all fields. This is the funky thing that is the result of changing the name of that field.

I probably would have coded things differently if I'd known that you had altered the keyword field, so the new "and" search would only work on that field.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] How to do an AND search with multiple terms? In reply to
Is there any chance you could help me with the revised code, given that the field being searched is the Quote field? Does there have to be a true keyword field? Everything works great with my database, except this new feature (which I would love to include if we can figure out how).
> >