SYNOPSIS

package MyModel::Tweet;
use Moose;
use ElasticSearchX::Model::Document;

has message => ( is => 'ro', isa => 'Str' );
has date => (
   is       => 'ro',
   required => 1,
   isa      => 'DateTime',
   default  => sub { DateTime->now }
);

package MyModel;
use Moose;
use ElasticSearchX::Model;

__PACKAGE__->meta->make_immutable;

 my $model = MyModel->new;
 $model->deploy;
 my $tweet = $model->index('default')->type('tweet')->put({
     message => 'Hello there!'
 });
 print $tweet->_id;
 $tweet->delete;

DESCRIPTION

This is an ElasticSearch to Moose mapper which hides the REST api behind object-oriented api calls. ElasticSearch types and indices are defined using Moose classes and a flexible DSL.

Deployment statements for ElasticSearch can be build dynamically using these classes. Results from ElasticSearch inflate automatically to the corresponding Moose classes. Furthermore, it provides sensible defaults.

The search API makes the tedious task of building ElasticSearch queries a lot easier.

The ElasticSearchX::Model::Tutorial is probably the best place to get started!

WARNING: This module is being used in production already but I don't consider it being stable in terms of the API and implementation details.

DSL

index

index twitter => ( namespace => 'MyNamespace', traits => ['MyTrait'] );

index facebook => ( types => [qw(FB::User FB::Friends)] );

Adds an index to the model. By default there is a default index, which will be removed once you add custom indices.

See "ATTRIBUTES" in ElasticSearchX::Model::Index for available options.

analyzer

tokenizer

filter

analyzer lowercase => ( tokenizer => 'keyword',  filter   => 'lowercase' );

tokenizer camelcase => (
    type => 'pattern',
    pattern => "([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])"
);
analyzer camelcase => (
    type => 'custom',
    tokenizer => 'camelcase',
    filter => ['lowercase', 'unique']
);

Adds analyzers, tokenizers or filters to all indices. They can then be used in attributes of ElasticSearchX::Model::Document classes.

ATTRIBUTES

es

Builds and holds the ElasticSearch object. Valid values are:

:9200

Connect to a server on 127.0.0.1, port 9200 with the httptiny transport class and a timeout of 30 seconds.

[qw(:9200 12.12.12.12:9200)]

Connect to 127.0.0.1:9200 and 12.12.12.12:9200 with the same defaults as above.

{ %args }

Passes %args directly to the ElasticSearch constructor.

bulk

my $bulk = $model->bulk( size => 100 );
$bulk->put($tweet);
$bulk->commit; # optional

Returns an instance of ElasticSearchX::Model::Bulk.

METHODS

index

my $index = $model->index('twitter');

Returns an ElasticSearchX::Model::Index object.

deploy

deploy pushes the mapping to the ElasticSearch server. It will automatically try to upgrade your mapping if the types already exists. However, this might not be possible in case you changes a field from one data type to another and ElasticSearch cannot figure out how to translate it. In this case deploy will throw an error message.

To create the indices from scratch, pass delete => 1. This will delete all the data in your indices.

$model->deploy( delete => 1 );

PERFORMANCE CONSIDERATIONS

Creating objects is a quite expensive operation. If you are crawling through large amounts of data, you will gain a huge speed improvement by not inflating the results to their document classes (see "raw" in ElasticSearchX::Model::Set).