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