1#!/usr/bin/perl 2# 3# Simple command-line match decider example 4# 5# Copyright (C) 2003 James Aylett 6# Copyright (C) 2004,2007,2009 Olly Betts 7# 8# This program is free software; you can redistribute it and/or 9# modify it under the terms of the GNU General Public License as 10# published by the Free Software Foundation; either version 2 of the 11# License, or (at your option) any later version. 12# 13# This program is distributed in the hope that it will be useful, 14# but WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16# GNU General Public License for more details. 17# 18# You should have received a copy of the GNU General Public License 19# along with this program; if not, write to the Free Software 20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 21# USA 22 23use 5.006; 24use strict; 25use warnings; 26 27use Xapian (':all'); 28 29# This example runs a query like simplesearch does, but uses a MatchDecider 30# (mymatchdecider) to discard any document for which value 0 is equal to 31# the string passed as the second command line argument. 32 33# We require at least three command line arguments. 34if (scalar @ARGV < 3) { 35 print STDERR "Usage: $0 PATH_TO_DATABASE AVOID_VALUE QUERY\n"; 36 exit(1); 37} 38 39my $avoid_value; 40 41sub mymatchdecider { 42 return $_[0]->get_value(0) ne $avoid_value; 43} 44 45eval { 46 # Open the database for searching. 47 my $database = Xapian::Database->new(shift @ARGV); 48 49 # Start an enquire session. 50 my $enquire = Xapian::Enquire->new($database); 51 52 $avoid_value = shift @ARGV; 53 54 # Combine the rest of the command line arguments with spaces between 55 # them, so that simple queries don't have to be quoted at the shell 56 # level. 57 my $query_string = join ' ', @ARGV; 58 59 # Parse the query string to produce a Xapian::Query object. 60 my $qp = Xapian::QueryParser->new(); 61 my $stemmer = Xapian::Stem->new("english"); 62 $qp->set_stemmer($stemmer); 63 $qp->set_database($database); 64 $qp->set_stemming_strategy(STEM_SOME); 65 my $query = $qp->parse_query($query_string); 66 print "Parsed query is: $query\n"; 67 68 # Find the top 10 results for the query. 69 $enquire->set_query($query); 70 my $mset = $enquire->get_mset(0, 10, \&mymatchdecider); 71 72 # Display the results. 73 printf "%i results found.\n", $mset->get_matches_estimated(); 74 printf "Results 1-%i:\n", $mset->size(); 75 76 foreach my $m ($mset->items()) { 77 printf "%i: %i%% docid=%i [%s]\n", $m->get_rank() + 1, $m->get_percent(), $m->get_docid(), $m->get_document()->get_data(); 78 } 79}; 80if ($@) { 81 print STDERR "Exception: $@\n"; 82 exit(1); 83} 84