File Coverage

blib/lib/ElasticSearchX/Model/Document.pm
Criterion Covered Total %
statement 26 26 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 35 37 94.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of ElasticSearchX-Model
3             #
4             # This software is Copyright (c) 2016 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             $ElasticSearchX::Model::Document::VERSION = '1.0.2';
12 7     7   1899000 use strict;
  7         12  
  7         193  
13 7     7   27 use warnings;
  7         8  
  7         190  
14              
15 7     7   26 use Moose 1.15 ();
  7         160  
  7         104  
16 7     7   23 use Moose::Exporter;
  7         8  
  7         41  
17 7     7   2882 use ElasticSearchX::Model::Document::Trait::Class;
  7         15  
  7         205  
18 7     7   2959 use ElasticSearchX::Model::Document::Trait::Attribute;
  7         21  
  7         260  
19 7     7   38 use ElasticSearchX::Model::Document::Types ();
  7         13  
  7         863  
20              
21             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 24     24 0 43126 sub has { shift->add_property(@_) }
29              
30             sub init_meta {
31 13     13 0 85229 my $class = shift;
32 13         40 my %p = @_;
33             Moose::Util::ensure_all_roles( $p{for_class},
34 13         51 qw(ElasticSearchX::Model::Document::Role) );
35 13         27786 $class->$init_meta(%p);
36             }
37              
38             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.2
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) 2016 by Moritz Onken.
172              
173             This is free software, licensed under:
174              
175             The (three-clause) BSD License
176              
177             =cut