| 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::Index; | ||||||||||
| 11 | 14 | $ElasticSearchX::Model::Index::VERSION = '2.0.0'; | |||||||||
| 12 | 14 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 14 | ▶ | use Module::Find (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 14 | ▶ | use Class::Load (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 14 | ▶ | use ElasticSearchX::Model::Document::Types qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 14 | has name => ( is => 'ro' ); | |||||||||
| 18 | |||||||||||
| 19 | 14 | has path => ( is => 'ro' ); | |||||||||
| 20 | |||||||||||
| 21 | 14 | has namespace => ( is => 'ro', lazy_build => 1 ); | |||||||||
| 22 | |||||||||||
| 23 | 14 | has [qw(shards replicas)] => ( is => 'ro', default => 1 ); | |||||||||
| 24 | |||||||||||
| 25 | 14 | has model => ( is => 'ro', required => 1, handles => [qw(es bulk)] ); | |||||||||
| 26 | |||||||||||
| 27 | 9 | ▶ | has traits => ( isa => 'ArrayRef', is => 'ro', default => sub { [] } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 14 | has refresh_interval => ( is => 'ro', default => '1s' ); | |||||||||
| 30 | |||||||||||
| 31 | 14 | has dynamic => ( is => 'ro', isa => 'Bool', default => 0 ); | |||||||||
| 32 | |||||||||||
| 33 | 14 | has alias_for => ( is => 'ro', isa => 'Str' ); | |||||||||
| 34 | |||||||||||
| 35 | 14 | has types => ( | |||||||||
| 36 | isa => Types, | ||||||||||
| 37 | coerce => 1, | ||||||||||
| 38 | traits => ['Hash'], | ||||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | lazy_build => 1, | ||||||||||
| 41 | handles => { | ||||||||||
| 42 | get_types => 'values', | ||||||||||
| 43 | get_type_list => 'keys', | ||||||||||
| 44 | add_type => 'set', | ||||||||||
| 45 | remove_type => 'delete', | ||||||||||
| 46 | get_type => 'get', | ||||||||||
| 47 | } | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | sub _build_types { | ||||||||||
| 51 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_types: called CC=2 SCAR=6.9
| |||||||||||
| 52 | 5 | my $namespace = $self->namespace; | |||||||||
| 53 | 5 | my %stash = Class::MOP::get_all_metaclasses; | |||||||||
| 54 | my @found = ( | ||||||||||
| 55 | Module::Find::findallmod($namespace), | ||||||||||
| 56 | 5 | grep {/^\Q$namespace\E::/} keys %stash | |||||||||
| 57 | ); | ||||||||||
| 58 | 5 | map { Class::Load::load_class($_) } @found; | |||||||||
| 59 | @found = grep { | ||||||||||
| 60 | 5 | ▶ | $_->isa('Moose::Object') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | && $_->does('ElasticSearchX::Model::Document::Role') | ||||||||||
| 62 | } @found; | ||||||||||
| 63 | 5 | return { map { $_->meta->short_name => $_->meta } @found }; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub BUILD { | ||||||||||
| 67 | 10 | ▶ | my $self = shift; | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 68 | 10 | foreach my $trait ( @{ $self->traits } ) { | |||||||||
| 69 | 1 | Moose::Util::ensure_all_roles( $self, $trait ); | |||||||||
| 70 | } | ||||||||||
| 71 | 10 | return $self; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | sub _build_namespace { | ||||||||||
| 75 | 3 | ▶ | ref shift->model; | ||||||||
|
Subroutine
_build_namespace: called CC=1 SCAR=0.0
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub type { | ||||||||||
| 79 | 3 | ▶ | my ( $self, $type ) = @_; | ||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 80 | 3 | my $class = $self->get_type($type)->set_class; | |||||||||
| 81 | 3 | Class::Load::load_class($class); | |||||||||
| 82 | 3 | return $class->new( | |||||||||
| 83 | index => $self, | ||||||||||
| 84 | type => $self->get_type($type), | ||||||||||
| 85 | ); | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub deployment_statement { | ||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
deployment_statement: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 90 | 0 | my $deploy = {}; | |||||||||
| 91 | 0 | foreach my $type ( $self->get_types ) { | |||||||||
| 92 | next | ||||||||||
| 93 | 0 | ▶ | if $type->does_role( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 'ElasticSearchX::Model::Document::EmbeddedRole'); | ||||||||||
| 95 | 0 | $deploy->{mappings}->{ $type->short_name } = $type->mapping; | |||||||||
| 96 | } | ||||||||||
| 97 | 0 | my $model = $self->model->meta; | |||||||||
| 98 | 0 | for (qw(filter analyzer tokenizer)) { | |||||||||
| 99 | 0 | my $method = "get_${_}_list"; | |||||||||
| 100 | 0 | foreach my $name ( $model->$method ) { | |||||||||
| 101 | 0 | my $get = "get_$_"; | |||||||||
| 102 | 0 | $deploy->{settings}->{analysis}->{$_}->{$name} | |||||||||
| 103 | = $model->$get($name); | ||||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | $deploy->{settings}->{index} = { | ||||||||||
| 107 | 0 | number_of_shards => $self->shards, | |||||||||
| 108 | number_of_replicas => $self->replicas, | ||||||||||
| 109 | refresh_interval => $self->refresh_interval | ||||||||||
| 110 | }; | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | $deploy->{settings}->{index}->{mapper}->{dynamic} = \0 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | unless ( $self->dynamic ); | ||||||||||
| 114 | |||||||||||
| 115 | 0 | return $deploy; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | sub refresh { | ||||||||||
| 119 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
refresh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 120 | 0 | $self->es->indices->refresh( index => $self->name ); | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | sub delete { | ||||||||||
| 124 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 125 | 0 | $self->es->indices->delete( index => $self->name ); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 14 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||
| 131 | |||||||||||
| 132 | =pod | ||||||||||
| 133 | |||||||||||
| 134 | =encoding UTF-8 | ||||||||||
| 135 | |||||||||||
| 136 | =head1 NAME | ||||||||||
| 137 | |||||||||||
| 138 | ElasticSearchX::Model::Index | ||||||||||
| 139 | |||||||||||
| 140 | =head1 VERSION | ||||||||||
| 141 | |||||||||||
| 142 | version 2.0.0 | ||||||||||
| 143 | |||||||||||
| 144 | =head1 ATTRIBUTES | ||||||||||
| 145 | |||||||||||
| 146 | =head2 name | ||||||||||
| 147 | |||||||||||
| 148 | The name of the index. | ||||||||||
| 149 | |||||||||||
| 150 | =head2 alias_for | ||||||||||
| 151 | |||||||||||
| 152 | If given, this name is used as the index name in ElasticSearch. The | ||||||||||
| 153 | L</name> is added as alias to L</alias_for>. | ||||||||||
| 154 | |||||||||||
| 155 | =head2 dynamic | ||||||||||
| 156 | |||||||||||
| 157 | By default, indices are not dynamic. That means that fields that | ||||||||||
| 158 | are unknown to the ElasticSearch mapping will not be indexed and | ||||||||||
| 159 | no dynamic mapping will be auto generated. Since the mapping is | ||||||||||
| 160 | generated by this module, there is no need to have the dynamic | ||||||||||
| 161 | mapping enabled. If you want to enable it anyway, set this attribute | ||||||||||
| 162 | to C<1> value. | ||||||||||
| 163 | |||||||||||
| 164 | =head2 namespace | ||||||||||
| 165 | |||||||||||
| 166 | Types are loaded from this namespace if they are not explicitly | ||||||||||
| 167 | defined using L</types>. The namespace defaults to the package | ||||||||||
| 168 | name of the model. | ||||||||||
| 169 | |||||||||||
| 170 | =head2 refresh_interval | ||||||||||
| 171 | |||||||||||
| 172 | This string defines the interval in which the index is refreshed | ||||||||||
| 173 | and new documents are made available to the search. It defaults | ||||||||||
| 174 | to C<1s> and can be set to C<-1> to disable refreshing completely. | ||||||||||
| 175 | |||||||||||
| 176 | Indices can be refreshed manually by calling L</refresh>. | ||||||||||
| 177 | |||||||||||
| 178 | =head2 types | ||||||||||
| 179 | |||||||||||
| 180 | A HashRef where the key is an indentifier for the value, | ||||||||||
| 181 | the L<ElasticSearchX::Model::Document> meta object. This attribute | ||||||||||
| 182 | is automatically built if it isn't provided. It uses L</namespace> | ||||||||||
| 183 | to look for L<ElasticSearchX::Model::Document> classes. The name is | ||||||||||
| 184 | derived from the class name by lowercasing the last name segment | ||||||||||
| 185 | (i.e. C<MyModel::Tweet> becomes C<tweet>). | ||||||||||
| 186 | |||||||||||
| 187 | =head2 traits | ||||||||||
| 188 | |||||||||||
| 189 | An ArrayRef of traits which are applied to the index object. | ||||||||||
| 190 | This is useful if you want to alter the behaviour of methods | ||||||||||
| 191 | like L</deploy>. | ||||||||||
| 192 | |||||||||||
| 193 | =head2 model | ||||||||||
| 194 | |||||||||||
| 195 | This attribute is set automatically by the model class when | ||||||||||
| 196 | you add an index. It can be used to access the model through | ||||||||||
| 197 | the index object. | ||||||||||
| 198 | |||||||||||
| 199 | =head2 shards | ||||||||||
| 200 | |||||||||||
| 201 | =head2 replicas | ||||||||||
| 202 | |||||||||||
| 203 | Sets the number of shards and replicas. Both default to C<1>. | ||||||||||
| 204 | |||||||||||
| 205 | =head2 refresh_interval | ||||||||||
| 206 | |||||||||||
| 207 | Sets the refresh interval. Defaults to C<1s>. | ||||||||||
| 208 | |||||||||||
| 209 | =head2 es | ||||||||||
| 210 | |||||||||||
| 211 | The L<ElasticSearch> object. | ||||||||||
| 212 | |||||||||||
| 213 | =head2 bulk | ||||||||||
| 214 | |||||||||||
| 215 | Returns an instance of L<ElasticSearchX::Model::Bulk>. | ||||||||||
| 216 | |||||||||||
| 217 | =head1 METHODS | ||||||||||
| 218 | |||||||||||
| 219 | =head2 refresh | ||||||||||
| 220 | |||||||||||
| 221 | Refresh index manually. | ||||||||||
| 222 | |||||||||||
| 223 | =head2 delete | ||||||||||
| 224 | |||||||||||
| 225 | Delete an index from ElasticSearch. | ||||||||||
| 226 | |||||||||||
| 227 | =head1 AUTHOR | ||||||||||
| 228 | |||||||||||
| 229 | Moritz Onken | ||||||||||
| 230 | |||||||||||
| 231 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 232 | |||||||||||
| 233 | This software is Copyright (c) 2018 by Moritz Onken. | ||||||||||
| 234 | |||||||||||
| 235 | This is free software, licensed under: | ||||||||||
| 236 | |||||||||||
| 237 | The (three-clause) BSD License | ||||||||||
| 238 | |||||||||||
| 239 | =cut | ||||||||||