The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Elastic::Doc - Adds Elastic::Model functionality to your object classes

VERSION

version 0.52

SYNOPSIS

Simple class definition

package MyApp::User;

use Elastic::Doc;

has 'name' => (
    is  => 'rw',
    isa => 'Str'
);

no Elastic::Doc;

More complex class definition

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;

DESCRIPTION

Elastic::Doc prepares your object classes (eg MyApp::User) for storage in Elasticsearch, by:

INTRODUCTION TO Elastic::Model

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.

EXPORTED FUNCTIONS

has_mapping

has_mapping can be used to customize the special "meta-fields" (ie not attr/field-specific) in the type mapping. For instance:

has_mapping {
    _source => {
        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.

apply_field_settings

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

If you DO have access to the role, then the preferred way to configure attributes is with the ElasticField trait:

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.

SEE ALSO

AUTHOR

Clinton Gormley <drtech@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 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.