| 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; | ||||||||||||||
| 11 | 12 | $ElasticSearchX::Model::VERSION = '2.0.0'; | |||||||||||||
| 12 | # ABSTRACT: Extensible and flexible model for Elasticsearch based on Moose | ||||||||||||||
| 13 | 12 | ▶ | use Moose 2.02 (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 12 | ▶ | use Moose::Exporter (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 12 | ▶ | use ElasticSearchX::Model::Index; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 12 | ▶ | use ElasticSearchX::Model::Bulk; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 12 | Moose::Exporter->setup_import_methods( | |||||||||||||
| 19 | with_meta => [qw(index analyzer tokenizer filter)], | ||||||||||||||
| 20 | class_metaroles => { class => ['ElasticSearchX::Model::Trait::Class'] }, | ||||||||||||||
| 21 | base_class_roles => [qw(ElasticSearchX::Model::Role)], | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | sub index { | ||||||||||||||
| 25 | 21 | ▶ | my ( $self, $name, @rest ) = @_; | ||||||||||||
|
Subroutine
index: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||
| 26 | 21 | ▶ | if ( !ref $name ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | # DSL call, where $self is the meta object | ||||||||||||||
| 29 | 11 | return $self->add_index( $name, {@rest} ); | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | elsif ( ref $name eq 'ARRAY' ) { | ||||||||||||||
| 32 | 0 | $self->add_index( $_, {@rest} ) for (@$name); | |||||||||||||
| 33 | 0 | return; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | else { | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # method call, i.e. $model->index() | ||||||||||||||
| 38 | 10 | my $options = $name->meta->get_index( $rest[0] ); | |||||||||||||
| 39 | 10 | my $index = ElasticSearchX::Model::Index->new( | |||||||||||||
| 40 | name => $rest[0], | ||||||||||||||
| 41 | %$options, model => $name | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | 10 | $options->{types} = $index->types; | |||||||||||||
| 44 | 10 | return $index; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | sub analyzer { | ||||||||||||||
| 49 | 2 | ▶ | shift->add_analyzer( shift, {@_} ); | ||||||||||||
|
Subroutine
analyzer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | sub tokenizer { | ||||||||||||||
| 53 | 0 | ▶ | shift->add_tokenizer( shift, {@_} ); | ||||||||||||
|
Subroutine
tokenizer: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | sub filter { | ||||||||||||||
| 57 | 0 | ▶ | shift->add_filter( shift, {@_} ); | ||||||||||||
|
Subroutine
filter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 12 | 1; | |||||||||||||
| 61 | |||||||||||||||
| 62 | __END__ | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =pod | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =encoding UTF-8 | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =head1 NAME | ||||||||||||||
| 69 | |||||||||||||||
| 70 | ElasticSearchX::Model - Extensible and flexible model for Elasticsearch based on Moose | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =head1 VERSION | ||||||||||||||
| 73 | |||||||||||||||
| 74 | version 2.0.0 | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||
| 77 | |||||||||||||||
| 78 | package MyModel::Tweet; | ||||||||||||||
| 79 | use Moose; | ||||||||||||||
| 80 | use ElasticSearchX::Model::Document; | ||||||||||||||
| 81 | |||||||||||||||
| 82 | has message => ( is => 'ro', isa => 'Str' ); | ||||||||||||||
| 83 | has date => ( | ||||||||||||||
| 84 | is => 'ro', | ||||||||||||||
| 85 | required => 1, | ||||||||||||||
| 86 | isa => 'DateTime', | ||||||||||||||
| 87 | default => sub { DateTime->now } | ||||||||||||||
| 88 | ); | ||||||||||||||
| 89 | |||||||||||||||
| 90 | package MyModel; | ||||||||||||||
| 91 | use Moose; | ||||||||||||||
| 92 | use ElasticSearchX::Model; | ||||||||||||||
| 93 | |||||||||||||||
| 94 | __PACKAGE__->meta->make_immutable; | ||||||||||||||
| 95 | |||||||||||||||
| 96 | my $model = MyModel->new; | ||||||||||||||
| 97 | $model->deploy; | ||||||||||||||
| 98 | my $tweet = $model->index('default')->type('tweet')->put({ | ||||||||||||||
| 99 | message => 'Hello there!' | ||||||||||||||
| 100 | }); | ||||||||||||||
| 101 | print $tweet->_id; | ||||||||||||||
| 102 | $tweet->delete; | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =head1 DESCRIPTION | ||||||||||||||
| 105 | |||||||||||||||
| 106 | This is an Elasticsearch to Moose mapper which hides the REST api | ||||||||||||||
| 107 | behind object-oriented api calls. Elasticsearch types and indices | ||||||||||||||
| 108 | are defined using Moose classes and a flexible DSL. | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Deployment statements for Elasticsearch can be build dynamically | ||||||||||||||
| 111 | using these classes. Results from Elasticsearch inflate automatically | ||||||||||||||
| 112 | to the corresponding Moose classes. Furthermore, it provides | ||||||||||||||
| 113 | sensible defaults. | ||||||||||||||
| 114 | |||||||||||||||
| 115 | The search API makes the tedious task of building Elasticsearch queries | ||||||||||||||
| 116 | a lot easier. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | B<< The L<ElasticSearchX::Model::Tutorial> is probably the best place | ||||||||||||||
| 119 | to get started! >> | ||||||||||||||
| 120 | |||||||||||||||
| 121 | B<< WARNING: This module is being used in production already but I don't | ||||||||||||||
| 122 | consider it being stable in terms of the API and implementation details. >> | ||||||||||||||
| 123 | |||||||||||||||
| 124 | =head1 DEPRECATED | ||||||||||||||
| 125 | |||||||||||||||
| 126 | This module and distribution have now been deprecated. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head1 DSL | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =head2 index | ||||||||||||||
| 131 | |||||||||||||||
| 132 | index twitter => ( namespace => 'MyNamespace', traits => ['MyTrait'] ); | ||||||||||||||
| 133 | |||||||||||||||
| 134 | index facebook => ( types => [qw(FB::User FB::Friends)] ); | ||||||||||||||
| 135 | |||||||||||||||
| 136 | Adds an index to the model. By default there is a C<default> | ||||||||||||||
| 137 | index, which will be removed once you add custom indices. | ||||||||||||||
| 138 | |||||||||||||||
| 139 | See L<ElasticSearchX::Model::Index/ATTRIBUTES> for available options. | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =head2 analyzer | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =head2 tokenizer | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =head2 filter | ||||||||||||||
| 146 | |||||||||||||||
| 147 | analyzer lowercase => ( tokenizer => 'keyword', filter => 'lowercase' ); | ||||||||||||||
| 148 | |||||||||||||||
| 149 | tokenizer camelcase => ( | ||||||||||||||
| 150 | type => 'pattern', | ||||||||||||||
| 151 | pattern => "([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])" | ||||||||||||||
| 152 | ); | ||||||||||||||
| 153 | analyzer camelcase => ( | ||||||||||||||
| 154 | type => 'custom', | ||||||||||||||
| 155 | tokenizer => 'camelcase', | ||||||||||||||
| 156 | filter => ['lowercase', 'unique'] | ||||||||||||||
| 157 | ); | ||||||||||||||
| 158 | |||||||||||||||
| 159 | Adds analyzers, tokenizers or filters to all indices. They can | ||||||||||||||
| 160 | then be used in attributes of L<ElasticSearchX::Model::Document> classes. | ||||||||||||||
| 161 | |||||||||||||||
| 162 | =head1 ATTRIBUTES | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =head2 es | ||||||||||||||
| 165 | |||||||||||||||
| 166 | Builds and holds the L<ElasticSearch> object. Valid values are: | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =over | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =item B<:9200> | ||||||||||||||
| 171 | |||||||||||||||
| 172 | Connect to a server on C<127.0.0.1>, port C<9200> with the C<httptiny> | ||||||||||||||
| 173 | transport class and a timeout of 30 seconds. | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =item B<[qw(:9200 12.12.12.12:9200)]> | ||||||||||||||
| 176 | |||||||||||||||
| 177 | Connect to C<127.0.0.1:9200> and C<12.12.12.12:9200> with the same | ||||||||||||||
| 178 | defaults as above. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =item B<{ %args }> | ||||||||||||||
| 181 | |||||||||||||||
| 182 | Passes C<%args> directly to the L<ElasticSearch> constructor. | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =back | ||||||||||||||
| 185 | |||||||||||||||
| 186 | =head2 bulk | ||||||||||||||
| 187 | |||||||||||||||
| 188 | my $bulk = $model->bulk( size => 100 ); | ||||||||||||||
| 189 | $bulk->put($tweet); | ||||||||||||||
| 190 | $bulk->commit; # optional | ||||||||||||||
| 191 | |||||||||||||||
| 192 | Returns an instance of L<ElasticSearchX::Model::Bulk>. | ||||||||||||||
| 193 | |||||||||||||||
| 194 | =head1 METHODS | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =head2 index | ||||||||||||||
| 197 | |||||||||||||||
| 198 | my $index = $model->index('twitter'); | ||||||||||||||
| 199 | |||||||||||||||
| 200 | Returns an L<ElasticSearchX::Model::Index> object. | ||||||||||||||
| 201 | |||||||||||||||
| 202 | =head2 deploy | ||||||||||||||
| 203 | |||||||||||||||
| 204 | C<deploy> pushes the mapping to the Elasticsearch server. It will | ||||||||||||||
| 205 | automatically try to upgrade your mapping if the types already | ||||||||||||||
| 206 | exists. However, this might not be possible in case you changes | ||||||||||||||
| 207 | a field from one data type to another and Elasticsearch cannot | ||||||||||||||
| 208 | figure out how to translate it. In this case C<deploy> will | ||||||||||||||
| 209 | throw an error message. | ||||||||||||||
| 210 | |||||||||||||||
| 211 | To create the indices from scratch, pass C<< delete => 1 >>. | ||||||||||||||
| 212 | B<< This will delete all the data in your indices. >> | ||||||||||||||
| 213 | |||||||||||||||
| 214 | $model->deploy( delete => 1 ); | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head2 es_version | ||||||||||||||
| 217 | |||||||||||||||
| 218 | if($model->es_version > 0.02) { ... } | ||||||||||||||
| 219 | |||||||||||||||
| 220 | Returns the L<version> number of the Elasticsearch server you are currently | ||||||||||||||
| 221 | connected to. Elasticsearch uses Semantic Versioning. However, release candidates | ||||||||||||||
| 222 | have a special syntax. For example, the version 0.20.0.RC1 would be parsed | ||||||||||||||
| 223 | as 0.020_000_001. | ||||||||||||||
| 224 | |||||||||||||||
| 225 | =head1 PERFORMANCE CONSIDERATIONS | ||||||||||||||
| 226 | |||||||||||||||
| 227 | Creating objects is a quite expensive operation. If you are | ||||||||||||||
| 228 | crawling through large amounts of data, you will gain a huge | ||||||||||||||
| 229 | speed improvement by not inflating the results to their | ||||||||||||||
| 230 | document classes (see L<ElasticSearchX::Model::Document::Set/raw>). | ||||||||||||||
| 231 | |||||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||||
| 233 | |||||||||||||||
| 234 | Moritz Onken | ||||||||||||||
| 235 | |||||||||||||||
| 236 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 237 | |||||||||||||||
| 238 | This software is Copyright (c) 2018 by Moritz Onken. | ||||||||||||||
| 239 | |||||||||||||||
| 240 | This is free software, licensed under: | ||||||||||||||
| 241 | |||||||||||||||
| 242 | The (three-clause) BSD License | ||||||||||||||
| 243 | |||||||||||||||
| 244 | =cut | ||||||||||||||