[Xapian-discuss] Seek help with simple writer/search using Perl

Ralf Mattes rm at seid-online.de
Mon Apr 9 20:48:31 BST 2007


On Mon, 2007-04-09 at 15:32 -0400, Jeff Anderson wrote:
> Hello all. I just started using Xapian and am already having troubles
> storing and searching very simple data.
> 
> Here is the relevant code i use to store the simple values: foo, bar,
> baz and qux:
> 
> -------------------------------------------------------------------------------
> my $db = Search::Xapian::WritableDatabase->new(
>     '/tmp/xapian/test',
>     Search::Xapian::DB_CREATE
> ) or die "can't create: $!\n";
> 
> print "Opened indexer for write with ", $db->get_doccount, " docs\n";
> 
> for my $data (qw(foo bar baz qux)) {
> 
>     my $doc = Search::Xapian::Document->new();
> 
>     $doc->set_data( $data );

I think you want to add your terms as postings and not as data - at
least iff you want to search for them later on ...

      $doc->add_posting( $data, 1);


HTH Ralf Mattes

> 
>     $doc->add_posting( 'ONE', 1 );
>     $doc->add_posting( 'TWO', 1 );
> 
>     $doc->add_value( 1, $data );
>     $doc->add_value( 2, $data );
> 
>     $db->add_document( $doc );
> }
> 
> $db->flush;
> 
> print "There are now ", $db->get_doccount, " docs in index\n";
> -------------------------------------------------------------------------------
> 
> As you can see, i am storing each value via set_data(), add_posting(),
> and add_value() (i really have no idea what the differences are or why
> i need to use one instead of the other). There seems to be no problems
> running this code, and after running this code it produces the
> following output for me:
> 
> -------------------------------------------------------------------------------
> Opened indexer for write with 0 docs
> There are now 4 docs in index
> -------------------------------------------------------------------------------
> 
> 
> 
> Moving on ... searching my database. Here is the relevant code i am using:
> 
> -------------------------------------------------------------------------------
> my $db = Search::Xapian::Database->new( '/tmp/xapian/test' ) or die $!;
> 
> for my $term (qw(foo bar baz qux)) {
> 
>     print "found ", $db->get_termfreq( $term ), " docs for term $term";
> }
> 
> for my $id (1..4) {
> 
>     my $doc = $db->get_document( $id );
> 
>     print "doc $id has data: ", $doc->get_data;
> }
> 
> my $enq = $db->enquire( OP_OR, 'bar' );
> warn "here's what we got: ", $enq->get_query;
> 
> my @match = $enq->matches( 0, 10 );
> print scalar( @match ) . " results found";
> -------------------------------------------------------------------------------
> 
> In the first for loop i check to see if there are any docs with the
> same simple values (foo, bar, baz and qux) that i used when i created
> the database and populated it with documents. The second for loop
> assumes that there are 4 documents in the database 1-4 (as there
> should be after running my first code snippet) and simply fetches them
> and prints the value stored as 'data.'
> 
> Finally, i search for a hard coded value 'bar'. Here is the result of
> me running this code (immediately after running my first snippet i
> should add)
> 
> -------------------------------------------------------------------------------
> found 0 docs for term foo
> found 0 docs for term bar
> found 0 docs for term baz
> found 0 docs for term qux
> doc 1 has data: foo
> doc 2 has data: bar
> doc 3 has data: baz
> doc 4 has data: qux
> here's what we got: Xapian::Query(bar) at bin/xapian_search.pl line 48.
> 
> 0 results found
> -------------------------------------------------------------------------------
> 
> As you can see, there are no docs with any of the terms i thought i
> assigned, but there are 4 documents in the database and the data was
> stored. However, my search results always turn up empty. :(
> 
> Can anyone here at this list offer any help, insight or general
> suggestions? I've been reading the site docs all day long and have
> some pretty good success, but this problem is proving to be a real
> show stopper for me.
> 
> Thanks in advance,




More information about the Xapian-discuss mailing list