Nicolas Steenlant > Catmandu-0.0402 > Catmandu::Store::ElasticSearch

Download:
Catmandu-0.0402.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Source  

NAME ^

Catmandu::Store::ElasticSearch - A Catmandu::Store plugin for ElasticSearch engines

SYNOPSIS ^

    use Catmandu::Store::ElasticSearch;

    my $store = Catmandu::Store::ElasticSearch->new(index_name => 'catmandu');

    my $obj1 = $store->bag->add({ name => 'Patrick' });

    printf "obj1 stored as %s\n" , $obj1->{_id};

    # Force an id in the store
    my $obj2 = $store->bag->add({ _id => 'test123' , name => 'Nicolas' });

    # Commit all changes
    $store->bag->commit;

    my $obj3 = $store->bag->get('test123');

    $store->bag->delete('test123');
    
    $store->bag->delete_all;

    # All bags are iterators
    $store->bag->each(sub { ... });
    $store->bag->take(10)->each(sub { ... });

    # Some stores can be searched
    my $hits = $store->bag->search(query => 'name:Patrick');

    # ElasticSearch supports CQL...
    my $hits = $store->bag->search(cql_query => 'name any "Patrick"');

DESCRIPTION ^

A Catmandu::Store::ElasticSearch is a Perl package that can index data into a ElasticSearch engine. The database as a whole is called a 'store'. Databases also have compartments (e.g. tables) called Catmandu::Bag-s. The Catmandu::Store::ElasticSearch can be searched using Catmandu::Searchable methods.

SUPPORT ^

This ElasticSearch interface is based on elasticsearch-0.17.6.

METHODS ^

new(index_name => $name, cql_mapping => \%mapping)

Create a new Catmandu::Store::ElasticSearch store connected to index $name. The ElasticSearch supports CQL searches when a cql_mapping is provided. This hash contains a translation of CQL fields into ElasticSearch searchable fields.

 # Example mapping
 $cql_mapping = {
      title => {
        op => {
          'any'   => 1 ,
          'all'   => 1 ,
          '='     => 1 ,
          '<>'    => 1 ,
          'exact' => {field => [qw(mytitle.exact myalttitle.exact)]}
        } ,
        sort  => 1,
        field => 'mytitle',
        cb    => ['Biblio::Search', 'normalize_title']
      }
 }

 The CQL mapping above will support for the 'title' field the CQL operators: any, all, =, <> and exact.

 For all the operators the 'title' field will be mapping into the ElasticSearch field 'mytitle', except
 for the 'exact' operator. In case of 'exact' we will search both the 'mytitle.exact' and 'myalttitle.exact'
 fields.

 The CQL mapping allows for sorting on the 'title' field. If, for instance, we would like to use a special
 ElasticSearch field for sorting we could have written "sort => { field => 'mytitle.sort' }".

 The CQL has an optional callback field 'cb' which contains a reference to subroutines to rewrite or
 augment the search query. In this case, in the Biblio::Search package there is a normalize_title 
 subroutine which returns a string or an ARRAY of string with augmented title(s). E.g.

    package Biblio::Search;

    sub normalize_title {
       my ($self,$title) = @_;
       my $new_title =~ s{[^A-Z0-9]+}{}g;
       $new_title;
    }

    1;

bag($name)

Create or retieve a bag with name $name. Returns a Catmandu::Bag.

SEE ALSO ^

Catmandu::Bag, Catmandu::Searchable

syntax highlighting: