| 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::Tutorial; | ||
| 11 | 1 | $ElasticSearchX::Model::Tutorial::VERSION = '2.0.0'; | |
| 12 | # ABSTRACT: Tutorial for ElasticSearchX::Model | ||
| 13 | 1 | 1; | |
| 14 | |||
| 15 | __END__ | ||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =encoding UTF-8 | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | ElasticSearchX::Model::Tutorial - Tutorial for ElasticSearchX::Model | ||
| 24 | |||
| 25 | =head1 VERSION | ||
| 26 | |||
| 27 | version 2.0.0 | ||
| 28 | |||
| 29 | =head1 INTRODUCTION | ||
| 30 | |||
| 31 | In this tutorial we are going to walk through the Elasticsearch example | ||
| 32 | on L<http://www.elasticsearch.org/>. Go ahead and read it first, | ||
| 33 | this gives you a good insight into how Elasticsearch works and how | ||
| 34 | L<ElasticSearchX::Model> can help to make it even more elastic. | ||
| 35 | |||
| 36 | =head1 DOCUMENTS | ||
| 37 | |||
| 38 | Elasticsearch is a document-based storage system. Even though it states | ||
| 39 | that it is schema free, it is not recommended to use Elasticsearch | ||
| 40 | without defining a proper schema or mapping, as Elasticsearch calls it. | ||
| 41 | |||
| 42 | L<ElasticSearchX::Model::Document> takes care of that. The Elasticsearch example | ||
| 43 | consists of two types: C<tweet> and C<user>. The C<tweet> type | ||
| 44 | contains the properties C<user>, C<post_date> and C<message>. The C<user> | ||
| 45 | type contains only the C<name> property. Using L<ElasticSearchX::Model::Document> | ||
| 46 | this looks like: | ||
| 47 | |||
| 48 | package MyModel::Tweet; | ||
| 49 | use Moose; | ||
| 50 | use ElasticSearchX::Model::Document; | ||
| 51 | |||
| 52 | has id => ( is => 'ro', id => [qw(user post_date)] ); | ||
| 53 | has user => ( is => 'ro', isa => 'Str' ); | ||
| 54 | has post_date => ( is => 'ro', isa => 'DateTime', required => 1, default => sub { DateTime->now } ); | ||
| 55 | has message => ( is => 'rw', isa => 'Str', index => 'analyzed' ); | ||
| 56 | |||
| 57 | package MyModel::User; | ||
| 58 | use Moose; | ||
| 59 | use ElasticSearchX::Model::Document; | ||
| 60 | |||
| 61 | has nickname => ( is => 'ro', isa => 'Str', id => 1 ); | ||
| 62 | has name => ( is => 'ro', isa => 'Str' ); | ||
| 63 | |||
| 64 | You might be wondering why there is an additional C<id> attribute and a | ||
| 65 | C<nickname>. The C<id> attribute in the Tweet class is build dynamically | ||
| 66 | by concatenating the values of C<user> and C<post_date>. this value is | ||
| 67 | digested using SHA1 and used as id for the document. If you want to | ||
| 68 | change the message of the tweet, you don't have to delete the old record | ||
| 69 | and add a new one but simply change the message and reindex the document. | ||
| 70 | Since the id will stay the same, the new record will overwrite the old one. | ||
| 71 | Also, you don't have to keep track of incrementing numerical document ids. | ||
| 72 | |||
| 73 | In the C<User> class, the C<nickname> attribute acts as id. Since it does not | ||
| 74 | depend on the value of any other attribute, the id matches the nickname. | ||
| 75 | |||
| 76 | Elasticsearch will assign a random id to the document if there is | ||
| 77 | no id attribute. | ||
| 78 | |||
| 79 | =head1 MAPPING | ||
| 80 | |||
| 81 | Each document belongs to a type. Think of it as a table in a relational | ||
| 82 | database. And each type belongs to an index, which corresponds to a database. | ||
| 83 | |||
| 84 | Modeling indices and types with L<ElasticSearchX::Model> is pretty easy | ||
| 85 | and the types have actually already been built: the meta objects of the | ||
| 86 | document classes describe the types. They include all the necessary | ||
| 87 | information to build a type mapping. You can even use L<MooseX::Types::Structured> | ||
| 88 | to build deepy nested structures that will be translated to C<object> | ||
| 89 | properties in Elasticsearch. L<DateTime> attributes become a C<Date> type | ||
| 90 | and so on. | ||
| 91 | |||
| 92 | Indices are defined in a model class: | ||
| 93 | |||
| 94 | package MyModel; | ||
| 95 | use Moose; | ||
| 96 | use ElasticSearchX::Model; | ||
| 97 | |||
| 98 | index twitter => ( namespace => 'MyModel' ); | ||
| 99 | |||
| 100 | This is all you need to define the index and its types. The namespace option | ||
| 101 | of the index C<twitter> will load all classes in the C<MyModel> namespace | ||
| 102 | and add them to the twitter index. Actually, you don't even have to define | ||
| 103 | the namespace in this case, since the namespace defaults to the name of the | ||
| 104 | model class. You can also load types explicitly by defining a C<types> option: | ||
| 105 | |||
| 106 | index twitter => ( types => [qw(MyModel::Tweet MyModel::User)] ); | ||
| 107 | |||
| 108 | Make sure that the classes are loaded. See L<ElasticSearchX::Model::Index> for all | ||
| 109 | the available options. | ||
| 110 | |||
| 111 | To deploy the indices and mappings to Elasticsearch, simply call | ||
| 112 | |||
| 113 | my $model = MyModel->new; | ||
| 114 | $model->deploy; | ||
| 115 | |||
| 116 | This will try to connect to an Elasticsearch instance on 127.0.0.1:9200. | ||
| 117 | See L<ElasticSearchX::Model/es> for more information. | ||
| 118 | |||
| 119 | =head1 INDEXING | ||
| 120 | |||
| 121 | Indexing describes the process of adding documents to types. | ||
| 122 | |||
| 123 | use DateTime; | ||
| 124 | |||
| 125 | my $twitter = $model->index('twitter'); | ||
| 126 | my $timestamp = DateTime->now; | ||
| 127 | my $tweet = $twitter->type('tweet')->put({ | ||
| 128 | user => 'mo', | ||
| 129 | post_date => $timestamp, | ||
| 130 | message => 'Elastic baby!', | ||
| 131 | }, { refresh => 1 }); | ||
| 132 | |||
| 133 | $twitter->type('tweet')->count; # 1 | ||
| 134 | |||
| 135 | The first parameter contains the property/value pairs. The C<post_date> | ||
| 136 | property is special because it is a L<DateTime> object. Objects are | ||
| 137 | being deflated prior to insertion. This is handled by | ||
| 138 | L<MooseX::Attribute::Deflator> and is configured in | ||
| 139 | L<ElasticSearchX::Model::Document::Types>. You can easily add deflators | ||
| 140 | for other objects. | ||
| 141 | |||
| 142 | Since the C<post_date> property is required and has a C<default>, you | ||
| 143 | don't even have to it to C<put>. ElasticSearchX::Model will automatically | ||
| 144 | build values from required attributes. If there is no builder or default, | ||
| 145 | it will throw an exception. | ||
| 146 | |||
| 147 | The second parameter to L<ElasticSearchX::Model::Document::Set/put> tells | ||
| 148 | Elasticsearch to refresh the index immediately. Otherwise it can | ||
| 149 | take up to one second for the server to refresh and the subsequent | ||
| 150 | call to L<ElasticSearchX::Model::Document::Set/count> will return C<0>. | ||
| 151 | |||
| 152 | If you index large numbers of documents, it is advised to call | ||
| 153 | L<ElasticSearchX::Model::Index/refresh> once you are finished and not | ||
| 154 | on every put. | ||
| 155 | |||
| 156 | =head1 RETRIEVING | ||
| 157 | |||
| 158 | Documents can be retrieved either with their id or by providing | ||
| 159 | the properties that define the id: | ||
| 160 | |||
| 161 | my $tweet_copy = $twitter->type('tweet')->get($tweet->id); | ||
| 162 | # or | ||
| 163 | my $tweet_copy = $twitter->type('tweet')->get({ | ||
| 164 | user => 'mo', | ||
| 165 | post_date => $timestamp, | ||
| 166 | }); | ||
| 167 | |||
| 168 | Objects that have been deflated (i.e. C<post_date>) will be inflated | ||
| 169 | again. Thus, C<< $tweet_copy->post_date >> is a DateTime object | ||
| 170 | again. | ||
| 171 | |||
| 172 | If you don't really care about objects or need extra speed, you can set | ||
| 173 | L<ElasticSearchX::Model::Documents::Set/inflate> to C<0>. This will return | ||
| 174 | the raw response from Elasticsearch. | ||
| 175 | |||
| 176 | $twitter->type('tweet')->raw->get($tweet->id); | ||
| 177 | |||
| 178 | =head1 SEARCHING AND SCROLLING | ||
| 179 | |||
| 180 | Elasticsearch is I<You know, for Search>. L<ElasticSearchX::Model::Set> tries | ||
| 181 | to help you with its very verbose query syntax. | ||
| 182 | |||
| 183 | my @tweets = $twitter->type('tweet')->filter({ | ||
| 184 | term => { user => 'mo' } | ||
| 185 | })->query({ | ||
| 186 | field => { 'message.analyzed' => 'baby' } | ||
| 187 | })->size(100)->all; | ||
| 188 | |||
| 189 | If you need to retrieve large amounts of data, you probably want to scroll | ||
| 190 | through the results, which is much faster and safer than scrolling manually | ||
| 191 | using L<ElasticSearchX::Model::Set/from>. | ||
| 192 | |||
| 193 | my $iterator = $twitter->type('tweet')->scroll; | ||
| 194 | while(my $tweet = $iterator->next) { | ||
| 195 | # do something with $tweet | ||
| 196 | } | ||
| 197 | |||
| 198 | For extra speed use C<< $twitter->type('tweet')->raw->scroll >> which will | ||
| 199 | skip the object inflation and give you the raw HashRef. | ||
| 200 | |||
| 201 | =head1 REINDEXING | ||
| 202 | |||
| 203 | Elasticsearch allows you to create aliases for each index. This makes it easy | ||
| 204 | to reindex to a new index, and change the alias once the reindexing is done, | ||
| 205 | to the new index. This is how you do it with ElasticSearchX::Model. | ||
| 206 | |||
| 207 | package MyModel; | ||
| 208 | use Moose; | ||
| 209 | use ElasticSearchX::Model; | ||
| 210 | |||
| 211 | index twitter => ( namespace => 'MyModel', alias_for => 'twitter_v1' ); | ||
| 212 | |||
| 213 | This will create an index called C<twitter_v1> in Elasticsearch and an | ||
| 214 | alias C<twitter>. To reindex data, you simply add a second index with | ||
| 215 | a different name but the same document classes: | ||
| 216 | |||
| 217 | index twitter_v2 => ( namespace => 'MyModel' ); | ||
| 218 | |||
| 219 | Now deploy the new index and start reindexing your data to the new index: | ||
| 220 | |||
| 221 | $model->deploy; | ||
| 222 | |||
| 223 | my $old = $model->index('twitter'); | ||
| 224 | my $new = $model->index('twitter_v2'); | ||
| 225 | my $iterator = $old->type('tweet')->size(1000)->scroll; | ||
| 226 | while(my $tweet = $iterator->next) { | ||
| 227 | $tweet->message('something else'); | ||
| 228 | $tweet->index($new); | ||
| 229 | $tweet->put; | ||
| 230 | } | ||
| 231 | |||
| 232 | Afterwards, you simply remove the C<twitter_v2> index and set the C<alias_for> | ||
| 233 | attribute on index C<twitter> to C<twitter_v2>. You have to call | ||
| 234 | C<< $model->deploy >> again, which will automatically update the aliases. | ||
| 235 | |||
| 236 | =head1 AUTHOR | ||
| 237 | |||
| 238 | Moritz Onken | ||
| 239 | |||
| 240 | =head1 COPYRIGHT AND LICENSE | ||
| 241 | |||
| 242 | This software is Copyright (c) 2018 by Moritz Onken. | ||
| 243 | |||
| 244 | This is free software, licensed under: | ||
| 245 | |||
| 246 | The (three-clause) BSD License | ||
| 247 | |||
| 248 | =cut |