This module and distribution have now been deprecated.
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;
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.
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.
default
See "ATTRIBUTES" in ElasticSearchX::Model::Index for available options.
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.
Builds and holds the ElasticSearch object. Valid values are:
Connect to a server on 127.0.0.1, port 9200 with the httptiny transport class and a timeout of 30 seconds.
127.0.0.1
9200
httptiny
Connect to 127.0.0.1:9200 and 12.12.12.12:9200 with the same defaults as above.
127.0.0.1:9200
12.12.12.12:9200
Passes %args directly to the ElasticSearch constructor.
%args
my $bulk = $model->bulk( size => 100 ); $bulk->put($tweet); $bulk->commit; # optional
Returns an instance of ElasticSearchX::Model::Bulk.
my $index = $model->index('twitter');
Returns an ElasticSearchX::Model::Index object.
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.
deploy
To create the indices from scratch, pass delete => 1. This will delete all the data in your indices.
delete => 1
$model->deploy( delete => 1 );
if($model->es_version > 0.02) { ... }
Returns the version number of the Elasticsearch server you are currently connected to. Elasticsearch uses Semantic Versioning. However, release candidates have a special syntax. For example, the version 0.20.0.RC1 would be parsed as 0.020_000_001.
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::Document::Set).
To install ElasticSearchX::Model, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ElasticSearchX::Model
CPAN shell
perl -MCPAN -e shell install ElasticSearchX::Model
For more information on module installation, please visit the detailed CPAN module installation guide.