Elastic::Doc - Adds Elastic::Model functionality to your object classes
version 0.28
package MyApp::User; use Elastic::Doc; has 'name' => ( is => 'rw', isa => 'Str' ); no Elastic::Doc;
package MyApp::User; use Elastic::Doc; has_mapping { _ttl => { # delete documents/object after 2 hours enabled => 1, default => '2h' } }; has 'user' => ( is => 'ro', isa => 'MyApp::User' ); has 'title' => ( is => 'rw', isa => 'Str', analyzer => 'edge_ngrams' # use custom analyzer ); has 'body' => ( is => 'rw', isa => 'Str', analyzer => 'english', # use builtin analyzer ); has 'created' => ( is => 'ro', isa => 'DateTime', default => sub { DateTime->new } ); has 'tag' => ( is => 'ro', isa => 'Str', index => 'not_analyzed' # index exact value ); no Elastic::Doc;
Elastic::Doc prepares your object classes (eg MyApp::User) for storage in Elasticsearch, by:
MyApp::User
applying Elastic::Model::Role::Doc to your class and Elastic::Model::Meta::Doc to its metaclass
adding keywords to your attribute declarations, to give you control over how they are indexed (see Elastic::Manual::Attributes)
wrapping your accessors to allow auto-inflation of embedded objects (see Elastic::Model::Meta::Instance).
exporting the "has_mapping" function to allow you to customize the special "meta-fields" in the type mapping in Elasticsearch
If you are not familiar with Elastic::Model, you should start by reading Elastic::Manual::Intro.
The rest of the documentation on this page explains how to use the Elastic::Doc module itself.
has_mapping can be used to customize the special "meta-fields" (ie not attr/field-specific) in the type mapping. For instance:
has_mapping
has_mapping { _source => { compress => 1, includes => ['path1.*','path2.*'], excludes => ['path3.*'] }, _ttl => { enabled => 1, default => '2h' }, numeric_detection => 1, date_detection => 0, };
Warning: Use has_mapping with caution. Elastic::Model requires certain settings to be active to work correctly.
See the "Fields" section in Mapping and Root object type for more information about what options can be configured.
package MyApp::User; use Elastic::Doc; with 'MyApp::Role::Foo'; apply_field_settings { field_1 => { type => 'string' }, field_2 => { exclude => 1 } };
When you apply a role to your Elastic::Doc class, you may not be able to configure the attributes directly in the role (eg if the role comes from CPAN).
You can use apply_field_settings in your doc class to add any of the settings specified in Elastic::Manual::Attributes. Alternatively, if you don't want any of the imported attributes to be persisted to Elasticsearch, then you can specify:
apply_field_settings
apply_field_settings '-exclude';
Note: the -exclude is applied to all attributes applied thus far, which don't already do Elastic::Model::Trait::Field. So you can then apply other roles and have another apply_field_settings statement later in your module.
-exclude
If you DO have access to the role, then the preferred way to configure attributes is with the ElasticField trait:
ElasticField
package MyApp::Role::Foo; use Moose::Role; has 'name' => ( traits => ['ElasticField'], is => 'rw', index => 'not_analyzed' );
ElasticField is the short name for Elastic::Model::Trait::Field.
Elastic::Model::Role::Doc
Elastic::Model
Elastic::Meta::Trait::Field
Elastic::Model::TypeMap::Default
Elastic::Model::TypeMap::Moose
Elastic::Model::TypeMap::Objects
Elastic::Model::TypeMap::Structured
Elastic::Model::TypeMap::ES
Elastic::Model::TypeMap::Common
Clinton Gormley <drtech@cpan.org>
This software is copyright (c) 2014 by Clinton Gormley.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Elastic::Model, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Elastic::Model
CPAN shell
perl -MCPAN -e shell install Elastic::Model
For more information on module installation, please visit the detailed CPAN module installation guide.