Author image Matthias Friedrich


Net::LuceneWS - Interface to the Lucene Web Service


  use Net::LuceneWS;

  my $ws = new Net::LuceneWS(
    host     => 'localhost',
    port     => 8080,
    index    => 'musicindex',
    context  => 'context',

  my $ret = $ws->Search('R.E.M.', max_hits=>5, default_field=>'artist')
    or die "Error: " . $ws->GetError() . "\n";

  foreach my $hit ( $ret->GetHits() ) {
        printf "%3d %s\n", $hit->GetScore(), $hit->GetField('artist');


Interface to the Lucene indexing and searching package via lucene-ws, the Lucene Web Service.


  new Net::LuceneWS(
      host           => 'localhost',
      port           => 8080,
      context        => 'lucene',
      index          => 'musicindex',
      max_hits       => 25,
      default_field  => 'artist',
      debug          => 1,

Create a new Net::LuceneWS object. This constructor expects a hash of configuration settings. Alternatively, you can pass the same arguments to each of the methods described above. In this case they take precedence over arguments passed to the constructor.

host, port, context and index are always required, either when calling the constructor or for each method call.

If you set up lucene-ws as suggested, context has to be set to "lucene". index is the name of the index directory. max_hits set the maximum number of hits returned by the Search() method. default_field is the default field that is used for the query. If debug is set to 1 the network dialog is printed to stderr.

  my $results = $ws->Search('Tori Amos', %args);

Search the web service. The first parameter is the query string, the other parameters are the same as those for the constructor. The arguments max_hits and default_field are required, additional to those listed in new().

This method returns a Net::LuceneWS::SearchResults object or undef on error. In this case an error message is available via GetError().

  my @docs = (
    { id => '1', artist => 'Tori Amos', track => 'Northern Lad' },
    { id => '2', artist => 'R.E.M.', track => 'Falls to Climb' },
  $ws->AddDocuments(\@docs, %args);

Add one or more documents to the index. Each document is a hashref

If the index doesn't exist yet it is created. In this case you have to pass an analyzer argument. Valid values are "SimpleAnalyzer", "StopAnalyzer", "StandardAnalyzer" and "WithStopAnalyzer". After the analyzer argument is no longer required.

  my %update = (
    default_field => 'id',
    query => '1',
    document => { id => 1, artist => 'Heather Nova', track => 'Storm' },

  $ws->UpdateDocuments([\%update], %args);

Update the matching documents. This is basically a combination of the delete and add methods.

  my @delete = (
    { default_field => 'artist', query => 'R.E.M.' },
  $ws->DeleteDocuments(\@delete, %args);

Delete all documents matching the queries.


Optimize the index which results in a speedup of queries. This method should be called after making larger changes to the index.


Get the error message returned by lucene-ws.


Diagnostics could be better but that is partly due to lucene-ws.



Matthias Friedrich, <>


Copyright (C) 2005 by Matthias Friedrich

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 460:

You forgot a '=back' before '=head1'