Debian Codegraph @

README

raw
1 NAME
2
3 Catmandu::Store::ElasticSearch - A searchable store backed by
4 Elasticsearch
5
6 SYNOPSIS
7
8 # From the command line
9
10 # Import data into ElasticSearch
11 $ catmandu import JSON to ElasticSearch --bag catmandu < data.json
12
13 # Export data from ElasticSearch
14 $ catmandu export ElasticSearch --bag catmandu to JSON > data.json
15
16 # Export only one record
17 $ catmandu export ElasticSearch --bag catmandu --id 1234
18
19 # Export using an ElasticSearch query
20 $ catmandu export ElasticSearch --bag catmandu --query "name:Recruitment OR name:college"
21
22 # Export using a CQL query (needs a CQL mapping)
23 $ catmandu export ElasticSearch --bag catmandu --cql-query "name any college"
24
25 # You need to specify the client version if your Elasticsearch server version is
26 # not the same as your default Search::Elasticsearch client version
27 $ catmandu import JSON to ElasticSearch --bag catmandu --client '5_0::Direct' < data.json
28
29 # From Perl
30
31 use Catmandu;
32
33 my $store = Catmandu->store('ElasticSearch');
34 # options will be passed to the underlying Search::Elasticsearch client
35 my $store = Catmandu->store('ElasticSearch', nodes => ['server.example.com:9200']);
36
37 my $obj1 = $store->bag('catmandu')->add({ name => 'Patrick' });
38
39 printf "obj1 stored as %s\n" , $obj1->{_id};
40
41 # Force an id in the store
42 my $obj2 = $store->bag('catmandu')->add({ _id => 'test123' , name => 'Nicolas' });
43
44 # Commit all changes
45 $store->bag('catmandu')->commit;
46
47 $store->bag('catmandu')->delete('test123');
48
49 $store->bag('catmandu')->delete_all;
50
51 # All bags are iterators
52 $store->bag->each(sub { ... });
53 $store->bag->take(10)->each(sub { ... });
54
55 # Query the store using a simple ElasticSearch query
56 my $hits = $store->bag->search(query => '(content:this OR name:this) AND (content:that OR name:that)');
57
58 # Native queries are also supported by providing a hash of terms
59 # See the ElasticSearch manual for more examples
60 my $hits = $store->bag->search(
61 query => {
62 # All name.exact fields that start with 'test'
63 prefix => {
64 'name.exact' => 'test'
65 }
66 } ,
67 limit => 1000);
68
69 # Catmandu::Store::ElasticSearch supports CQL...
70 my $hits = $store->bag->search(cql_query => 'name any "Patrick"');
71
72 METHODS
73
74 new(%params)
75
76 new(%params, bags => { mybag => { index => 'myindex', mapping => \%map
77 cql_mapping => \%map } })
78
79 Create a new Catmandu::Store::ElasticSearch store. ElasticSearch
80 connection parameters will be passed on to the underlying client.
81
82 Optionally provide for each bag a index to indicate which index to use.
83 This defaults to the bag's name.
84
85 Optionally provide for each bag a type to indicate the name of the
86 mapping. This defaults to the bag's name.
87
88 Optionally provide for each bag a mapping which contains a
89 ElasticSearch schema for each field in the index (See below).
90
91 Optionally provide for each bag a cql_mapping to map fields to CQL
92 indexes.
93
94 Optionally provide for each bag an on_error error handler (See below).
95
96 INHERITED METHODS
97
98 This Catmandu::Store implements:
99
100 Catmandu::Store
101
102 Each Catmandu::Bag in this Catmandu::Store implements:
103
104 Catmandu::Bag
105
106 Catmandu::Droppable
107
108 Catmandu::Searchable
109
110 Catmandu::CQLSearchable
111
112 INDEX MAPPING
113
114 The mapping contains a Elasticsearch schema mappings for each bag
115 defined in the index. E.g.
116
117 {
118 properties => {
119 title => {
120 type => 'text'
121 }
122 }
123 }
124
125 See
126 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
127 for more information on mappings.
128
129 These mappings can be passed inside a Perl program, or be written into
130 a Catmandu 'catmandu.yml' configuration file. E.g.
131
132 # catmandu.yml
133 store:
134 search:
135 package: ElasticSearch
136 options:
137 bags:
138 mybag:
139 mapping:
140 properties:
141 title:
142 type: text
143
144 Via the command line these configuration parameters can be read in by
145 using the name of the store, search in this case:
146
147 $ catmandu import JSON to search --bag mybag < data.json
148 $ catmandu export search --bag mybag to JSON > data.json
149
150 CQL MAPPING
151
152 Catmandu::Store::ElasticSearch supports CQL searches when a cql_mapping
153 is provided for each bag. This hash contains a translation of CQL
154 fields into Elasticsearch searchable fields.
155
156 # Example mapping
157 {
158 indexes => {
159 title => {
160 op => {
161 'any' => 1 ,
162 'all' => 1 ,
163 '=' => 1 ,
164 '<>' => 1 ,
165 'exact' => {field => [qw(mytitle.exact myalttitle.exact)]}
166 } ,
167 field => 'mytitle',
168 sort => 1,
169 cb => ['Biblio::Search', 'normalize_title']
170 }
171 }
172 }
173
174 The CQL mapping above will support for the 'title' field the CQL
175 operators: any, all, =, <> and exact.
176
177 The 'title' field will be mapping into the Elasticsearch field
178 'mytitle', except for the 'exact' operator. In case of 'exact' we will
179 search both the 'mytitle.exact' and 'myalttitle.exact' fields.
180
181 The CQL mapping allows for sorting on the 'title' field. If, for
182 instance, we would like to use a special ElasticSearch field for
183 sorting we could have written "sort => { field => 'mytitle.sort' }".
184
185 The callback field cb contains a reference to subroutines to rewrite or
186 augment a search query. In this case, the Biblio::Search package
187 contains a normalize_title subroutine which returns a string or an
188 ARRAY of strings with augmented title(s). E.g.
189
190 package Biblio::Search;
191
192 sub normalize_title {
193 my ($self,$title) = @_;
194 my $new_title =~ s{[^A-Z0-9]+}{}g;
195 $new_title;
196 }
197
198 1;
199
200 Also this configuration can be added to a catmandu.yml configuration
201 file like:
202
203 # catmandu.yml
204 store:
205 search:
206 package: ElasticSearch
207 options:
208 client: 6_0::Direct
209 bags:
210 book:
211 mapping:
212 properties:
213 title:
214 type: text
215 cql_mapping:
216 indexes:
217 title:
218 op:
219 'any': true
220 'all': true
221 '=': true
222 '<>': true
223 'exact':
224 field: [ 'mytitle.exact' , 'myalttitle.exact' ]
225 field: mytitle
226 sort: true
227 cb: [ 'Biblio::Search' , 'normalize_title' ]
228
229 Via the command line these configuration parameters can be read in by
230 using the name of the store, search in this case:
231
232 $ catmandu export search --bag book -q 'title any blablabla' to JSON > data.json
233
234 COMPATIBILITY
235
236 The appropriate client should be installed:
237
238 # Elasticsearch 6.x
239 cpanm Search::Elasticsearch::Client::6_0::Direct
240 # Elasticsearch 1.x
241 cpanm Search::Elasticsearch::Client::1_0::Direct
242
243 And specified in the options:
244
245 Catmandu::Store::ElasticSearch->new(client => '1_0::Direct')
246
247 If you want to use the delete_by_query method with Elasticsearch 2.0
248 you have to install the delete by query plugin
249 <https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html>.
250
251 ERROR HANDLING
252
253 Error handling can be activated by specifying an error handling
254 callback for index when creating a store. E.g. to create an error
255 handler for the bag 'data' index use:
256
257 my $error_handler = sub {
258 my ($action, $response, $i) = @_;
259 do_something_with_error($response);
260 };
261
262 my $store = Catmandu::Store::ElasticSearch->new(
263 bags => { data => { on_error => $error_handler } }
264 });
265
266 Instead of a callback, the following shortcuts are also accepted for
267 on_error:
268
269 log: log the response
270
271 throw: throw the response as an error
272
273 ignore: do nothing
274
275 my $store = Catmandu::Store::ElasticSearch->new(
276 bags => { data => { on_error => 'log' } }
277 });
278
279 UPGRADING FROM A PRE 1.0 VERSION
280
281 Versions of this store < 1.0 used Elasticsearch types to map bags to a
282 single index. Support for multiple types in one index has since been
283 removed from Elasticsearch and since 1.0 each bag is mapped to an
284 index.
285
286 You need to export you data before upgrading, update the configuration
287 and then import you data again.
288
289 SEE ALSO
290
291 Catmandu::Store
292
293 AUTHOR
294
295 Nicolas Steenlant, <nicolas.steenlant at ugent.be>
296
297 CONTRIBUTORS
298
299 Dave Sherohman, dave.sherohman at ub.lu.se
300
301 Robin Sheat, robin at kallisti.net.nz
302
303 Patrick Hochstenbach, patrick.hochstenbach at ugent.be
304
305 LICENSE AND COPYRIGHT
306
307 This program is free software; you can redistribute it and/or modify it
308 under the terms of either: the GNU General Public License as published
309 by the Free Software Foundation; or the Artistic License.
310
311 See http://dev.perl.org/licenses/ for more information.
312