« blib/lib/ElasticSearchX/Model/Document/Set.pm
↑ Summary
blib/lib/ElasticSearchX/Model/Document/EmbeddedRole.pm »
| 1 | # | ||
| 2 | # This file is part of ElasticSearchX-Model | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2018 by Moritz Onken. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The (three-clause) BSD License | ||
| 9 | # | ||
| 10 | package ElasticSearchX::Model::Document; | ||
| 11 | 8 | $ElasticSearchX::Model::Document::VERSION = '1.0.3'; | |
| 12 | 8 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 8 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 8 | ▶ | use Moose 1.15 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 8 | ▶ | use Moose::Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 8 | ▶ | use ElasticSearchX::Model::Document::Trait::Class; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 8 | ▶ | use ElasticSearchX::Model::Document::Trait::Attribute; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 8 | ▶ | use ElasticSearchX::Model::Document::Types (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | |||
| 21 | 8 | my ( undef, undef, $init_meta ) = Moose::Exporter->build_import_methods( | |
| 22 | install => [qw(import unimport)], | ||
| 23 | with_meta => [qw(has)], | ||
| 24 | class_metaroles => | ||
| 25 | { class => ['ElasticSearchX::Model::Document::Trait::Class'] }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | 15 | ▶ | sub has { shift->add_property(@_) } |
|
Subroutine
has: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 29 | |||
| 30 | sub init_meta { | ||
| 31 | 13 | ▶ | my $class = shift; |
|
Subroutine
init_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 32 | 13 | my %p = @_; | |
| 33 | Moose::Util::ensure_all_roles( $p{for_class}, | ||
| 34 | 13 | qw(ElasticSearchX::Model::Document::Role) ); | |
| 35 | 13 | $class->$init_meta(%p); | |
| 36 | } | ||
| 37 | |||
| 38 | 8 | 1; | |
| 39 | |||
| 40 | __END__ | ||
| 41 | |||
| 42 | =pod | ||
| 43 | |||
| 44 | =encoding UTF-8 | ||
| 45 | |||
| 46 | =head1 NAME | ||
| 47 | |||
| 48 | ElasticSearchX::Model::Document | ||
| 49 | |||
| 50 | =head1 VERSION | ||
| 51 | |||
| 52 | version 1.0.3 | ||
| 53 | |||
| 54 | =head1 SYNOPSIS | ||
| 55 | |||
| 56 | package MyClass; | ||
| 57 | use Moose; | ||
| 58 | use ElasticSearchX::Model::Document; | ||
| 59 | use ElasticSearchX::Model::Document::Types qw(Location); | ||
| 60 | use MooseX::Types -declare => ['Resources']; | ||
| 61 | use MooseX::Types::Structured qw(Dict Optional); | ||
| 62 | use MooseX::Types::Moose qw(Str ArrayRef); | ||
| 63 | |||
| 64 | subtype Resources, | ||
| 65 | as Dict [ license => Optional [ ArrayRef [Str] ], | ||
| 66 | homepage => Optional [Str], | ||
| 67 | bugtracker => Optional [ Dict [ web => Str, mailto => Str ] ] ]; | ||
| 68 | |||
| 69 | has default => ( is => 'ro' ); | ||
| 70 | has date => ( is => 'ro', isa => 'DateTime' ); | ||
| 71 | has location => ( is => 'ro', isa => Location ); | ||
| 72 | has res => ( is => 'ro', isa => Resources ); | ||
| 73 | has abstract => ( is => 'ro', analyzer => 'lowercase' ); | ||
| 74 | |||
| 75 | =head1 DESCRIPTION | ||
| 76 | |||
| 77 | This class extends Moose to include meta information for ElasticSearch. | ||
| 78 | By default, each attribute is treated as property of an ElasticSearch | ||
| 79 | type (i.e. the L<ElasticSearchX::Model::Document::Trait::Attribute> | ||
| 80 | trait is applied). The type name is derived from the class name. See | ||
| 81 | L<ElasticSearchX::Model::Document::Trait::Class>. | ||
| 82 | |||
| 83 | See L<ElasticSearchX::Model::Document::Trait::Attribute/ATTRIBUTES> for a full | ||
| 84 | list of attribute options available. | ||
| 85 | |||
| 86 | =head1 ATTRIBUTES | ||
| 87 | |||
| 88 | =head2 index | ||
| 89 | |||
| 90 | $document->index($model->index('index_v2')); | ||
| 91 | $document->put; | ||
| 92 | |||
| 93 | The L<ElasticSearchX::Model::Index> object this document belongs to. | ||
| 94 | This attribute is writeable, which allows you to reindex a document | ||
| 95 | to a different index. Make sure that the type is part of the new | ||
| 96 | index. Otherwise indexing is likely to fail. | ||
| 97 | |||
| 98 | =head2 _id | ||
| 99 | |||
| 100 | This is a read-only attribute that holds the value of the id of | ||
| 101 | the document. If you want to set the id by yourself, create an | ||
| 102 | attribute in your document class which has the C<id> attribute | ||
| 103 | set to C<1>. Otherwise the id will be generated by ElasticSearch. | ||
| 104 | |||
| 105 | has id => ( is => 'ro', id => 1 ); | ||
| 106 | |||
| 107 | Setting the C<id> property to an ArrayRef of property names will | ||
| 108 | build the id for you as a digest of these values. | ||
| 109 | |||
| 110 | has id => ( is => 'ro', id => [qw(firstname lastname)] ); | ||
| 111 | |||
| 112 | A document with the firstname set to John and the lastname to Doe | ||
| 113 | will be stored with an id of C<Gwc_dwDeSaQunLeq73JHz5k9jns>. The | ||
| 114 | ID is generated in L<ElasticSearchX::Model::Util/digest>. | ||
| 115 | |||
| 116 | Think of it as a multi-value primary key. Quite useful if you don't | ||
| 117 | want to worry about duplicate records since the document with the | ||
| 118 | same values for the id will always generate the same id. | ||
| 119 | |||
| 120 | =head1 METHODS | ||
| 121 | |||
| 122 | =head2 create | ||
| 123 | |||
| 124 | =head2 create( { %qs } ) | ||
| 125 | |||
| 126 | Create a new document. If the document already exists (based | ||
| 127 | on the id), an L<ElasticSearch::Error::Conflict> expection is thrown. | ||
| 128 | |||
| 129 | =head2 update | ||
| 130 | |||
| 131 | =head2 update( { %qs } ) | ||
| 132 | |||
| 133 | Update an existing document. Throws an L<ElasticSearch::Error::Conflict> | ||
| 134 | if there is a version mismatch. This happens if a new version of the | ||
| 135 | document was pushed by someone else. If you don't care about a version | ||
| 136 | mismatch, either pass C<< version => undef >> or use L</put>. You can | ||
| 137 | also set a version explicitly with C<< version => $version >>. | ||
| 138 | |||
| 139 | If the document was built from a query that did not include all fields | ||
| 140 | (i.e. L<ElasticSearchX::Model::Set/fields> was set), then L</update> will | ||
| 141 | fail since partial updates to a document are not yet supported in | ||
| 142 | ElasticSearch. You can either pull the full document from ElasticSearch | ||
| 143 | and update then or call L</put>. | ||
| 144 | |||
| 145 | =head2 put | ||
| 146 | |||
| 147 | =head2 put( { %qs } ) | ||
| 148 | |||
| 149 | B<< Consider using L</update> or L</create> instead. >> They provide sanity | ||
| 150 | checks and are generally what you want to use. | ||
| 151 | |||
| 152 | This puts a document to the ElasticSearch server. Use this | ||
| 153 | to create a document or force an update. It calls | ||
| 154 | L<ElasticSearchX::Model::Document::Trait::Class/get_data> to retrieve the | ||
| 155 | data from an L<ElasticSearchX::Model::Document> object. | ||
| 156 | |||
| 157 | C<%qs> are optional parameters that are passed on to L<index()/ElasticSearch>. | ||
| 158 | |||
| 159 | =head2 delete | ||
| 160 | |||
| 161 | =head2 delete( { %qs } ) | ||
| 162 | |||
| 163 | Delete the document from the index. | ||
| 164 | |||
| 165 | =head1 AUTHOR | ||
| 166 | |||
| 167 | Moritz Onken | ||
| 168 | |||
| 169 | =head1 COPYRIGHT AND LICENSE | ||
| 170 | |||
| 171 | This software is Copyright (c) 2018 by Moritz Onken. | ||
| 172 | |||
| 173 | This is free software, licensed under: | ||
| 174 | |||
| 175 | The (three-clause) BSD License | ||
| 176 | |||
| 177 | =cut | ||