[Xapian-discuss] Working demo of search engine using boolean query.

Kevin Duraj kevin.softdev at gmail.com
Fri Feb 2 03:07:26 GMT 2007


Lately I was reading many articles about using boolean queries for search
engine but I haven't seen any complete working demo. Therefore I put
together very simple working demo of search engine using boolean query. Feel
free to suggest any performance improvement or error while keeping it as
simple as possible for understanding.
Thanks,


-Kevin Duraj
http://myhealthcare.com

#------------------------------------------------------------------------------#
# Sample Data #
#------------------------------------------------------------------------------#
url=webmd.com
text=fitness health cancer

url=health.com
text=diseases health calorie disability

url=healthfinder.gov
text=medications diet food

url=myhealthcare.com
text=Nanotechnology nanoscale science


#------------------------------------------------------------------------------#
# scriptindex
#------------------------------------------------------------------------------#
url  : index=S field
text : indexnopos truncate=1024 field



#------------------------------------------------------------------------------#
# Perl Search Script
#------------------------------------------------------------------------------#
#!/usr/bin/perl -w
use strict;
use ExtUtils::testlib;
use Search::Xapian qw/:all/;
#------------------------------------------------------------------------------#

my $primary_terms = "Swebmd.com Shealth.com Smyhealthcare.com
Shealthfinder.gov";
my $secondary_terms = "fitness Diseases diet science ";
$secondary_terms =~ tr/A-Z/a-z/; # convert to lower case

#------------------------------------------------------------------------------#

my $db = Search::Xapian::Database->new( '/home/myhealthcare/xapian_index' );
my $enq = $db->enquire();

#------------------------------------------------------------------------------#
my @primary_terms   = split ' ', $primary_terms;
my @secondary_terms = split ' ', $secondary_terms;
#------------------------------------------------------------------------------#

my $query1 = Search::Xapian::Query->new( OP_OR, @primary_terms );
my $query2 = Search::Xapian::Query->new( OP_OR, @secondary_terms );

my $boolean_query = Search::Xapian::Query->new( OP_AND, $query1, $query2 );
$enq->set_query( $boolean_query );

#------------------------------------------------------------------------------#

printf "Parsing query '%s'\n", $enq->get_query()->get_description();

my $total = $enq->matches(1, 100000000);
print "Total: $total results found.\n------------------------\n";
my @matches = $enq->matches(0, 15);

#------------------------------------------------------------------------------#
foreach my $match ( @matches )
{
  printf "ID %d %d%%", $match->get_docid(), $match->get_percent();
  my $doc = $match->get_document();
  printf " [ %s ]", $doc->get_data();
  print "\n";
}
#------------------------------------------------------------------------------#


More information about the Xapian-discuss mailing list