Gossamer Forum
Home : General : Perl Programming :

SQL Query

Quote Reply
SQL Query
I'm working on a quick and dirty search for one of my databases. This database contains links to other sites followed by a description. What I would like to do is match words that are contained in the description in no particular order. For instance: A description may be something like

"This that the other"

So if you do a search for "This that", you will come up with a match, but it you searched for "that this", you obviously wouldn't get a match even though both words are contained in the description. Is there any way around this?
Quote Reply
Re: [BennyHill] SQL Query In reply to
I think you'd need to split the query at the blank spaces so you have an array with each element being a keyword then loop though them - that way it will match records with those words in rather than expecting them in the order they were entered.
Quote Reply
Re: [RedRum] SQL Query In reply to
Thanks. That's basically what I had been trying to do but it was slow as hell and I couldn't seem to order the results in any kind of real order. I spent a couple of hours on mysql.com and FINALLY found something that helped me. This is pretty cool. Results are amazing.

http://www.mysql.com/...Fulltext_Search.html
Quote Reply
Re: [BennyHill] SQL Query In reply to
What were you doing, using Perl to check for matches instead of MySQL? Other than that, searches should not be that slow as long as you've indexed your columns.

I use the following code to generate my queries...
Code:
my @search_fields = qw(Title Description);
my $type = param('type'); ($type = 'keyword') if ($type !~ /^keyword|phrase$/);
my $bool = param('bool'); ($bool = 'and') if ($bool !~ /^and|or$/);
my $cs = param('cs'); ($cs = 0) if ($cs !~ /^0|1$/);
my $ww = param('ww'); ($ww = 0) if ($ww !~ /^0|1$/);
my $page = param('page'); $page ||= 1;
my $max = 100;
my $offset = (($page - 1) * $max) unless ($page <= 1); $offset ||= 0;
my @terms;
($type eq 'phrase') ? (@terms = param('query')) : (@terms = split /\s/, param('query'));
my $where;
foreach my $term (@terms) {
$where .= " (";
foreach my $field (@search_fields) {
if (param('ww')) {
$where .= " $field regexp '" . '[[:<:]]' . $term . '[[:>:]]' . "' or";
} else {
$where .= qq| binary| if (param('cs'));
$where .= qq| $field like '%$term%' or|;
}
}
$where =~ s/or$//;
$where .= ") $bool\n";
}
chomp($where);
$where =~ s/$bool$//;

You should let mySQL handle everything database related unless you've got a really good reason not to.

Fulltext searching is pretty cool, although I don't like their scoring system (less matches -> higher score). Can't wait for MySQL 4.0 to come out so we can use booleans in them.

--Philip
Links 2.0 moderator
Quote Reply
Re: [BennyHill] SQL Query In reply to
Hi,

The next release of Links SQL (and the public beta of Gossamer Forum due out shortly) will have support for full text indexing.

Cheers,

Alex
--
Gossamer Threads Inc.