version 2.0.1


 package MyClass;
 use Moose;
 use ElasticSearchX::Model::Document;
 use ElasticSearchX::Model::Document::Types qw(Location);
 use MooseX::Types -declare => ['Resources'];
 use MooseX::Types::Structured qw(Dict Optional);
 use MooseX::Types::Moose qw(Str ArrayRef);

 subtype Resources,
  as Dict [ license => Optional [ ArrayRef [Str] ],
            homepage => Optional [Str],
            bugtracker => Optional [ Dict [ web => Str, mailto => Str ] ] ];

 has default  => ( is => 'ro' );
 has date     => ( is => 'ro', isa => 'DateTime' );
 has location => ( is => 'ro', isa => Location );
 has res      => ( is => 'ro', isa => Resources );
 has abstract => ( is => 'ro', analyzer => 'lowercase' );


This class extends Moose to include meta information for ElasticSearch. By default, each attribute is treated as property of an ElasticSearch type (i.e. the ElasticSearchX::Model::Document::Trait::Attribute trait is applied). The type name is derived from the class name. See ElasticSearchX::Model::Document::Trait::Class.

See "ATTRIBUTES" in ElasticSearchX::Model::Document::Trait::Attribute for a full list of attribute options available.




The ElasticSearchX::Model::Index object this document belongs to. This attribute is writeable, which allows you to reindex a document to a different index. Make sure that the type is part of the new index. Otherwise indexing is likely to fail.


This is a read-only attribute that holds the value of the id of the document. If you want to set the id by yourself, create an attribute in your document class which has the id attribute set to 1. Otherwise the id will be generated by ElasticSearch.

 has id => ( is => 'ro', id => 1 );

Setting the id property to an ArrayRef of property names will build the id for you as a digest of these values.

 has id => ( is => 'ro', id => [qw(firstname lastname)] );

A document with the firstname set to John and the lastname to Doe will be stored with an id of Gwc_dwDeSaQunLeq73JHz5k9jns. The ID is generated in "digest" in ElasticSearchX::Model::Util.

Think of it as a multi-value primary key. Quite useful if you don't want to worry about duplicate records since the document with the same values for the id will always generate the same id.



create( { %qs } )

Create a new document. If the document already exists (based on the id), an ElasticSearch::Error::Conflict expection is thrown.


update( { %qs } )

Update an existing document. Throws an ElasticSearch::Error::Conflict if there is a version mismatch. This happens if a new version of the document was pushed by someone else. If you don't care about a version mismatch, either pass version => undef or use "put". You can also set a version explicitly with version => $version.

If the document was built from a query that did not include all fields (i.e. "fields" in ElasticSearchX::Model::Set was set), then "update" will fail since partial updates to a document are not yet supported in ElasticSearch. You can either pull the full document from ElasticSearch and update then or call "put".


put( { %qs } )

Consider using "update" or "create" instead. They provide sanity checks and are generally what you want to use.

This puts a document to the ElasticSearch server. Use this to create a document or force an update. It calls "get_data" in ElasticSearchX::Model::Document::Trait::Class to retrieve the data from an ElasticSearchX::Model::Document object.

%qs are optional parameters that are passed on to "ElasticSearch" in index().


delete( { %qs } )

Delete the document from the index.


Moritz Onken


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

This is free software, licensed under:

  The (three-clause) BSD License