Moritz Onken > ElasticSearchX-Model-0.1.6 > README.pod


Annotate this POD

View/Report Bugs
Source   Latest Release: ElasticSearchX-Model-0.2.2


 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;


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


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.

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, port 9200 with the httptiny transport class and a timeout of 30 seconds.


Connect to and with the same defaults as above.

{ %args }

Passes %args directly to the ElasticSearch constructor.


 my $bulk = $model->bulk( size => 100 );
 $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.

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

 $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).

syntax highlighting: