Debian Codegraph @

lib/Catmandu/Store/ElasticSearch/Bag.pm

raw symbols SCIP
1 package Catmandu::Store::ElasticSearch::Bagscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/;
2
3 use Catmandu::Sanescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Sane/;
4
5 our $VERSIONscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$VERSION. = '1.0204';
6
7 use Catmandu::Hitsscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Hits/;
8 use Cpanel::JSON::XSscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Cpanel/JSON/XS/ qw(encode_json decode_json);
9 use Catmandu::Store::ElasticSearch::Searcherscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Searcher/;
10 use Catmandu::Store::ElasticSearch::CQLscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/CQL/;
11 use Catmandu::Utilscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Util/ qw(is_code_ref is_string);
12 use Mooscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Moo/;
13 use namespace::cleanscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 namespace/clean/;
14
15 withscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/with(). 'Catmandu::Bag';
16 withscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/with(). 'Catmandu::Droppable';
17 withscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/with(). 'Catmandu::Flushable';
18 withscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/with(). 'Catmandu::CQLSearchable';
19
20 has index => (is => 'lazy');
21 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). settings => (is => 'lazy');
22 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). mapping => (is => 'lazy');
23 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). type => (is => 'lazy');
24 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). buffer_size => (is => 'lazy', builder => 'default_buffer_size');
25 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). _bulk => (is => 'lazy');
26 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). cql_mapping => (is => 'ro');
27 hasscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/has(). on_error => (is => 'lazy');
28
29 sub BUILDscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/BUILD(). {
30 $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.[0]->create_indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/create_index().;
31 }
32
33 sub create_indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/create_index(). {
34 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
35 my $es = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().;
36 unless ($es->indicesscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/indices().->existsscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/exists().(index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().)) {
37 $es->indicesscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/indices().->createscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/create().(
38 index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,
39 body => {
40 settings => $self->settingsscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/settings().,
41 mappings => {$self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type(). => $self->mappingscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/mapping().},
42 },
43 );
44 }
45 1;
46 }
47
48 sub default_buffer_sizescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/default_buffer_size(). {100}
49
50 sub _coerce_on_errorscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_coerce_on_error(). {
51 my ($self, $cb) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
52
53 if (is_code_refscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_code_ref().($cb)) {
54 return $cb;
55 }
56 if (is_stringscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_string().($cb) && $cb eq 'throw') {
57 return sub {
58 my ($action, $res, $i) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
59 Catmandu::Error->throwscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Error/throw().(encode_jsonscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/encode_json().($res));
60 };
61 }
62 if (is_stringscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_string().($cb) && $cb eq 'log') {
63 return sub {
64 my ($action, $res, $i) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
65 $self->logscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/log().->errorscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/error().(encode_jsonscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/encode_json().($res));
66 };
67 }
68 if (is_stringscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_string().($cb) && $cb eq 'ignore') {
69 return sub { };
70 }
71
72 Catmandu::BadArg->throwscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/BadArg/throw().(
73 "on_error should be code ref, 'throw', 'log', or 'ignore'");
74 }
75
76 sub _build_on_errorscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_build_on_error(). {
77 'log';
78 }
79
80 sub _build_settingsscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_build_settings(). {
81 +{};
82 }
83
84 sub _build_mappingscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_build_mapping(). {
85 +{};
86 }
87
88 sub _build_indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_build_index(). {
89 $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.[0]->namescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/name().;
90 }
91
92 sub _build_typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_build_type(). {
93 $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.[0]->namescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/name().;
94 }
95
96 sub _build__bulkscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_build__bulk(). {
97 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
98 my $on_error = $self->_coerce_on_errorscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_coerce_on_error().($self->on_errorscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/on_error().);
99 my %args = (
100 index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,
101 type => $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().,
102 max_count => $self->buffer_sizescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/buffer_size().,
103 on_error => $on_error,
104 );
105 if ($self->logscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/log().->is_debugscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_debug().) {
106 $args{on_success} = sub {
107 my ($action, $res, $i) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
108 $self->logscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/log().->debugscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/debug().(encode_jsonscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/encode_json().($res));
109 };
110 }
111 $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->bulk_helperscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/bulk_helper().(%args);
112 }
113
114 sub generatorscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/generator(). {
115 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
116 my $id_key = $self->id_keyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/id_key().;
117 sub {
118 state $scroll = do {
119 my %args = (
120 index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,
121 size => $self->buffer_sizescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/buffer_size()., # TODO divide by number of shards
122 body => {query => {match_all => {}},},
123 );
124 if ($self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->is_es_1_or_2scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_es_1_or_2().) {
125 $args{search_type} = 'scan';
126 $args{type} = $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().;
127 }
128 $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->scroll_helperscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/scroll_helper().(%args);
129 };
130 my $doc = $scroll->nextscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/next(). // do {
131 $scroll->finishscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/finish().;
132 return;
133 };
134 my $data = $doc->{_source};
135 $data->{$id_key} = $doc->{_id};
136 $data;
137 };
138 }
139
140 sub countscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/count(). {
141 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
142 my $store = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().;
143 my %args = (index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,);
144 if ($store->is_es_1_or_2scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_es_1_or_2().) {
145 $args{type} = $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().;
146 }
147 $store->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->countscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/count().(%args)->{count};
148 }
149
150 sub getscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/get(). {
151 my ($self, $id) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
152 try {
153 my $data = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->get_sourcescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/get_source().(
154 index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,
155 type => $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().,
156 id => $id,
157 );
158 $data->{$self->id_keyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/id_key().} = $id;
159 $data;
160 }
161 catch_case ['Search::Elasticsearch::Error::Missing' => sub {undefscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/undef().}];
162 }
163
164 sub addscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/add(). {
165 my ($self, $data) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
166 $data = {%$data};
167 my $id = deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete().($data->{$self->id_keyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/id_key().});
168 $self->_bulkscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_bulk().->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().({id => $id, source => $data,});
169 }
170
171 sub deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete(). {
172 my ($self, $id) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
173 $self->_bulkscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_bulk().->deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete().({id => $id});
174 }
175
176 sub delete_allscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete_all(). {
177 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
178 my $es = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().;
179 if ($es->canscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/can().('delete_by_query')) {
180 $es->delete_by_queryscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete_by_query().(
181 index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,
182 type => $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().,
183 body => {query => {match_all => {}},},
184 );
185 }
186 else { # TODO document plugin needed for es 2.x
187 $es->transportscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/transport().->perform_requestscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/perform_request().(
188 method => 'DELETE',
189 path => '/' . $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index(). . '/' . $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type(). . '/_query',
190 body => {query => {match_all => {}},}
191 );
192 }
193 }
194
195 sub delete_by_queryscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete_by_query(). {
196 my ($self, %args) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
197 my $es = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().;
198 if ($es->canscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/can().('delete_by_query')) {
199 $es->delete_by_queryscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete_by_query().(
200 index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().,
201 type => $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().,
202 body => {query => $args{query},},
203 );
204 }
205 else { # TODO document plugin needed for es 2.x
206 $es->transportscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/transport().->perform_requestscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/perform_request().(
207 method => 'DELETE',
208 path => '/' . $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index(). . '/' . $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type(). . '/_query',
209 body => {query => $args{query},}
210 );
211 }
212 }
213
214 sub flushscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/flush(). {
215 $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.[0]->_bulkscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_bulk().->flushscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/flush().;
216 }
217
218 sub commitscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/commit(). {
219 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
220 $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->transportscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/transport().->perform_requestscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/perform_request().(
221 method => 'POST',
222 path => '/' . $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index(). . '/_refresh',
223 );
224 }
225
226 sub searchscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/search(). {
227 my ($self, %args) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
228
229 my $id_key = $self->id_keyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/id_key().;
230
231 my $start = deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete(). $args{start};
232 my $limit = deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete(). $args{limit};
233 my $scroll_id = deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete(). $args{scroll_id};
234 my $scroll = deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete(). $args{scroll};
235 my $bag = deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete(). $args{reify};
236
237 my $res;
238 if (definedscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/defined(). $scroll_id) {
239 my %es_args = (body => {scroll_id => $scroll_id},);
240 if (definedscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/defined(). $scroll) {
241 $es_args{scroll} = $scroll;
242 }
243 $res = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->scrollscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/scroll().(%es_args);
244 }
245 else {
246 my %es_args
247 = (index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index()., body => {%args, size => $limit,},);
248 if ($self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->is_es_1_or_2scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/is_es_1_or_2().) {
249 $es_args{type} = $self->typescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/type().;
250 }
251 if ($bag) {
252 $es_args{body}{fields} = [];
253 }
254 if (definedscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/defined(). $scroll) {
255 $es_args{scroll} = $scroll;
256 }
257 else {
258 $es_args{body}{from} = $start;
259 }
260 $res = $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->searchscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/search().(%es_args);
261 }
262
263 my $docs = $res->{hits}{hits};
264
265 my $hits
266 = {start => $start, limit => $limit, total => $res->{hits}{total},};
267
268 if ($bag) {
269 $hits->{hits} = [mapscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/map(). {$bag->getscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/get().($_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$_.->{_id})} @$docs];
270 }
271 elsif ($args{fields}) {
272
273 # TODO check if fields includes id_key
274 $hits->{hits} = [mapscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/map(). {$_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$_.->{fields} || +{}} @$docs];
275 }
276 else {
277 $hits->{hits} = [
278 mapscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/map(). {
279 my $data = $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$_.->{_source};
280 $data->{$id_key} = $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$_.->{_id};
281 $data;
282 } @$docs
283 ];
284 }
285
286 $hits = Catmandu::Hits->newscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Hits/new().($hits);
287
288 $hits->{scroll_id} = $res->{_scroll_id} if existsscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/exists(). $res->{_scroll_id};
289 for my $key (qw(facets suggest aggregations)) {
290 $hits->{$key} = $res->{$key} if existsscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/exists(). $res->{$key};
291 }
292
293 if ($args{highlight}) {
294 for my $hit (@$docs) {
295 if (my $hl = $hit->{highlight}) {
296 $hits->{highlight}{$hit->{$id_key}} = $hl;
297 }
298 }
299 }
300
301 $hits;
302 }
303
304 sub searcherscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/searcher(). {
305 my ($self, %args) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
306 Catmandu::Store::ElasticSearch::Searcher->newscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Searcher/new().(%args, bag => $self);
307 }
308
309 sub translate_sru_sortkeysscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/translate_sru_sortkeys(). {
310 my ($self, $sortkeys) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
311 [
312 grepscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/grep(). {definedscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/defined(). $_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$_.} mapscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/map(). {$self->_translate_sru_sortkeyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_translate_sru_sortkey().($_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/$_.)} splitscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/split(). /\s+/,
313 $sortkeys
314 ];
315 }
316
317 sub _translate_sru_sortkeyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/_translate_sru_sortkey(). {
318 my ($self, $sortkey) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
319 my ($field, $schema, $asc) = splitscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/split(). /,/, $sortkey;
320 $field || return;
321 if (my $map = $self->cql_mappingscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/cql_mapping().) {
322 $field = lcscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/lc(). $field;
323 $field =~ s/(?<=[^_])_(?=[^_])//g
324 if $map->{strip_separating_underscores};
325 $map = $map->{indexes} || return;
326 $map = $map->{$field} || return;
327 $map->{sortscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/sort().} || return;
328 if (refscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/ref(). $map->{sortscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/sort().} && $map->{sortscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/sort().}{field}) {
329 $field = $map->{sortscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/sort().}{field};
330 }
331 elsif (refscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/ref(). $map->{field}) {
332 $field = $map->{field}->[0];
333 }
334 elsif ($map->{field}) {
335 $field = $map->{field};
336 }
337 }
338 $asc //= 1;
339 +{$field => $asc ? 'asc' : 'desc'};
340 }
341
342 sub translate_cql_queryscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/translate_cql_query(). {
343 my ($self, $query) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
344 Catmandu::Store::ElasticSearch::CQL->newscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/CQL/new().(
345 mapping => $self->cql_mappingscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/cql_mapping().,
346 id_key => $self->id_keyscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/id_key().
347 )->parsescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/parse().($query);
348 }
349
350 sub normalize_queryscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/normalize_query(). {
351 my ($self, $query) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
352 if (refscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/ref(). $query) {
353 $query;
354 }
355 elsif ($query) {
356 {query_string => {query => $query}};
357 }
358 else {
359 {match_all => {}};
360 }
361 }
362
363 # assume a sort string is JSON encoded
364 sub normalize_sortscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/normalize_sort(). {
365 my ($self, $sort) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
366 return $sort if refscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/ref(). $sort;
367 return if !$sort;
368 decode_jsonscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/decode_json().($sort);
369 }
370
371 sub dropscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/drop(). {
372 my ($self) = @_scip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/`@_`.;
373 $self->storescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/store().->esscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/es().->indicesscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/indices().->deletescip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/delete().(index => $self->indexscip-perl cpan Catmandu-Store-Elasticsearch 1.0204 Catmandu/Store/ElasticSearch/Bag/index().);
374 }
375
376 1;
377
378 __END__
379
380 =pod
381
382 =head1 NAME
383
384 Catmandu::Store::ElasticSearch::Bag - Catmandu::Bag implementation for Elasticsearch
385
386 =head1 DESCRIPTION
387
388 See the main documentation at L<Catmandu::Store::ElasticSearch>.
389
390 =head1 METHODS
391
392 This class inherits all the methods of L<Catmandu::Bag>,
393 L<Catmandu::CQLSearchable> and L<Catmandu::Droppable>.
394 It also provides the following methods:
395
396 =head2 create_index()
397
398 This method is called automatically when the bag is instantiated. You only need
399 to call it manually it after deleting the index with C<drop> or the
400 Elasticsearch API.
401
402 =head1 SEE ALSO
403
404 L<Catmandu::Bag>, L<Catmandu::Searchable>, L<Catmandu::CQLSearchable>, L<Catmandu::Droppable>
405
406 =cut