[Xapian-discuss] Fwd: Perl Search::Xapian

Jon Bradley weatchu at gmail.com
Tue Jan 28 18:53:52 GMT 2014


(\ /)
( . .)   Jon's website is here:
c(")(")   http://www.securityrabbit.com




---------- Forwarded message ----------
From: Jon Bradley <weatchu at gmail.com>
Date: Tue, Jan 28, 2014 at 9:52 AM
Subject: Re: [Xapian-discuss] Perl Search::Xapian
To: peter at peknet.com


This script now seems to work, thanks all!
now soon have to write the search.......

#!/usr/bin/perl

use strict;
use warnings;
unless ($#ARGV eq 1) { die "Usage: <cvsfile> <dbpath>\n" };

use Search::Xapian (':db');
use Tie::Handle::CSV;

my $csvfile = $ARGV[0];
my $dbpath = $ARGV[1];
my $db = Search::Xapian::WritableDatabase->new($dbpath,DB_CREATE_OR_OPEN);

# setup TermGenerator that'll be used in indexing.
my $tg = Search::Xapian::TermGenerator->new();
$tg->set_stemmer(Search::Xapian::Stem->new('en'));

# here is a for to loop thru all the csv? file.
my $fh = Tie::Handle::CSV->new($csvfile, header => 1);
while (my $csvline = <$fh>) {
    my $description = $csvline->{DESCRIPTION};
    my $title = $csvline->{TITLE};
    my $identifier = $csvline->{id_NUMBER};

    # We make a doc and tell the term generator to use this.
    my $doc = Search::Xapian::Document->new();
    $tg->set_document($doc);

    $tg->index_text($title, 1, 'S');
    $tg->index_text($description, 1, 'XD');

    # index fields without prefixes for general search.
    $tg->index_text($title);
    $tg->increase_termpos();
    $tg->index_text($description);

    # Store all the feilds for display purposes.
    # this is a TODO

    #my $idterm = "Q".$identifier;
    my $idterm = join('',"Q",$identifier);
    $doc->add_boolean_term($idterm);
    $db->replace_document_by_term($idterm, $doc);
}

close $fh;

(\ /)
( . .)   Jon's website is here:
c(")(")   http://www.securityrabbit.com



On Mon, Jan 27, 2014 at 6:19 PM, Peter Karman <peter at peknet.com> wrote:
> On 1/27/14 5:05 PM, Jon Bradley wrote:
>>
>> Hi,
>>
>> Trying to learn Search::Xapian and be better at perl at the same time,
>> I'm stuck, at the DB_CREATE_OR_OPEN error.  Perl says this:
>>
>> ~/dev/sandbox/Xapian-perl$ ./Index1-Xap.pl 100-objects-v1.csv db
>> "db" is not exported by the Search::Xapian module
>> Can't continue after import errors at ./Index1-Xap.pl line 7.
>> BEGIN failed--compilation aborted at ./Index1-Xap.pl line 7.
>>
>> What I did was try to convert the python example code in to Perl.
>> There's probably other errors, but my question is how to I deal with
>> the :db DB_CREATE_OR_OPEN in perl?  Been reading a lot and
>> my understanding of perl I guess is not the best it seems, any
>> help would be great, Thanks.
>>
>> ----------------snip---------------
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>> unless ($#ARGV eq 1) { die "Usage: <cvsfile> <dbpath>\n" };
>>
>> use Search::Xapian;
>
>
> use Search::Xapian ':db';
>
> will import the constants.
>
>
>
>> use Tie::Handle::CSV;
>>
>> my $csvfile = $ARGV[0];
>> my $dbpath = $ARGV[1];
>> my $db = Search::Xapian::WritableDatabase->new(
>>          path => $dbpath,
>>          mode => "DB_CREATE_OR_OPEN",
>>      );
>
>
>
> DB_CREATE_OR_OPEN is a constant so it should not be quoted.
>
>
> --
> Peter Karman  .  http://peknet.com/  .  peter at peknet.com
>
>
> _______________________________________________
> Xapian-discuss mailing list
> Xapian-discuss at lists.xapian.org
> http://lists.xapian.org/mailman/listinfo/xapian-discuss



More information about the Xapian-discuss mailing list