ElasticSearchX::Model - Extensible and flexible model for Elasticsearch based on Moose


version 2.0.1


 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.


This module and distribution have now been deprecated.



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


Moritz Onken


This software is Copyright (c) 2019 by Moritz Onken.

This is free software, licensed under:

  The (three-clause) BSD License