Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 1 | package Kalamar::API; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 2 | use Mojo::Base 'Mojolicious::Plugin'; |
Nils Diewald | 791b590 | 2014-12-04 04:47:24 +0000 | [diff] [blame] | 3 | use Scalar::Util qw/blessed weaken/; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 4 | use strict; |
| 5 | use warnings; |
| 6 | |
| 7 | # KorAP Search engine for Mojolicious::Plugin::Search |
| 8 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 9 | # TODO: Add fixtures |
| 10 | # TODO: Support search in corpus and virtualcollection |
| 11 | # TODO: Support caching everywhere! |
| 12 | # TODO: Correct use of stash info everywhere! |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 13 | |
| 14 | # Register the plugin |
| 15 | sub register { |
| 16 | my ($plugin, $mojo, $index_class, $param) = @_; |
| 17 | $param ||= {}; |
| 18 | |
| 19 | # Add attributes to the index class |
| 20 | $index_class->attr(api => $param->{api}); |
| 21 | $index_class->attr([qw/cutoff |
| 22 | query_language |
| 23 | time_exceeded |
| 24 | api_request |
| 25 | _api_cache |
| 26 | api_response |
| 27 | benchmark |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 28 | query_jsonld |
| 29 | collection_jsonld/]); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 30 | $index_class->attr(no_cache => 0); |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | # Search the index |
| 35 | sub search { |
| 36 | my $self = shift; |
| 37 | my $index = shift; |
| 38 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 39 | # Get controller |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 40 | my $c = $index->controller; |
| 41 | |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 42 | # If there is a callback, do async |
| 43 | my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE'; |
| 44 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 45 | # No query defined |
| 46 | unless ($index->query) { |
| 47 | return $cb->($index) if $cb; |
| 48 | return; |
| 49 | }; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 50 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 51 | # Get query url |
| 52 | my $url = _query_url($index, @_); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 53 | |
| 54 | # Cache based on URL |
| 55 | $index->_api_cache('total-' . $url->to_string); |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 56 | my %param = @_; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 57 | |
| 58 | # Set context based on parameter |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 59 | $url->query({ context => $param{'context'} // 'paragraph' }); |
| 60 | |
| 61 | # Set path to search |
| 62 | $url->path('search'); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 63 | |
| 64 | # Check cache for total results |
| 65 | my $total_results; |
| 66 | |
| 67 | if (!$index->no_cache && |
| 68 | defined ($total_results = $c->chi->get($index->_api_cache))) { |
| 69 | |
| 70 | # Set total results from cache |
| 71 | $index->total_results($total_results); |
| 72 | $c->app->log->debug('Get total result from cache'); |
| 73 | |
| 74 | # Set cutoff unless already set |
| 75 | $url->query({cutoff => 'true'}) unless defined $index->cutoff; |
| 76 | }; |
| 77 | |
| 78 | # Set api request for debugging |
| 79 | $index->api_request($url->to_string); |
| 80 | |
| 81 | # Create new user agent and set timeout to 2 minutes |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 82 | my $ua = $c->ua; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 83 | $ua->inactivity_timeout(120); |
| 84 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 85 | # Debugging |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 86 | $c->app->log->debug('Search for ' . $index->api_request); |
| 87 | |
| 88 | # Search non-blocking |
| 89 | if ($cb) { |
| 90 | |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 91 | $ua->get( |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 92 | $url => sub { |
| 93 | my $tx = pop; |
| 94 | $self->_process_response('matches', $index, $tx); |
Nils Diewald | 791b590 | 2014-12-04 04:47:24 +0000 | [diff] [blame] | 95 | weaken $index; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 96 | return $cb->($index); |
| 97 | }); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 98 | } |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 99 | |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 100 | # Search blocking |
| 101 | else { |
| 102 | my $tx = $ua->get($url); |
Nils Diewald | 034ea70 | 2015-01-16 19:41:52 +0000 | [diff] [blame] | 103 | $self->_process_response('matches', $index, $tx); |
| 104 | return $index; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 105 | }; |
| 106 | }; |
| 107 | |
| 108 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 109 | # Trace query serialization |
| 110 | sub trace { |
| 111 | my $self = shift; |
| 112 | my $index = shift; |
| 113 | |
| 114 | # Get controller |
| 115 | my $c = $index->controller; |
| 116 | |
| 117 | # If there is a callback, do async |
| 118 | my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE'; |
| 119 | |
| 120 | my %param = @_; |
| 121 | |
| 122 | # No query defined |
| 123 | unless ($index->query(delete $param{query})) { |
| 124 | return $cb->($index) if $cb; |
| 125 | return; |
| 126 | }; |
| 127 | |
| 128 | # Get query url |
| 129 | my $url = _query_url($index, @_); |
| 130 | |
| 131 | $url->path('search'); |
| 132 | |
| 133 | # Create new user agent and set timeout to 30 seconds |
| 134 | my $ua = $c->ua; # Mojo::UserAgent->new; |
| 135 | $ua->inactivity_timeout(30); |
| 136 | |
| 137 | # Build transaction |
| 138 | my $tx = $ua->build_tx(TRACE => $url); |
| 139 | |
| 140 | # non-blocking |
| 141 | if ($cb) { |
Nils Diewald | 791b590 | 2014-12-04 04:47:24 +0000 | [diff] [blame] | 142 | weaken $index; |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 143 | |
| 144 | # Trace non-blocking |
| 145 | $ua->start( |
| 146 | $tx => sub { |
| 147 | $self->_process_response('trace', $index, pop); |
| 148 | return $cb->($index); |
| 149 | }); |
| 150 | } |
| 151 | # Trace blocking |
| 152 | else { |
| 153 | my $tx = $ua->start($url); |
| 154 | return $self->_process_response('trace', $index, $tx); |
| 155 | }; |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | # Get match info |
| 160 | sub match { |
| 161 | my $self = shift; |
| 162 | my $index = shift; |
| 163 | |
| 164 | # Get controller |
| 165 | my $c = $index->controller; |
| 166 | |
| 167 | # If there is a callback, do async |
| 168 | my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE'; |
| 169 | |
| 170 | my %param = @_; |
| 171 | |
| 172 | my $url = Mojo::URL->new($index->api); |
| 173 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 174 | # Legacy: In old versions, doc_id contained text_id |
| 175 | $param{doc_id} .= '.' . $param{text_id} if $param{text_id}; |
| 176 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 177 | # Use hash slice to create path |
| 178 | $url->path(join('/', 'corpus', @param{qw/corpus_id doc_id match_id/}, 'matchInfo')); |
| 179 | |
| 180 | # Build match id |
| 181 | # $match = 'match-' . $corpus . '!' . $corpus . '_' . $doc . '-' . $match; |
| 182 | |
| 183 | my %query; |
| 184 | $query{foundry} = $param{foundry}; |
| 185 | $query{layer} = $param{layer} if defined $param{layer}; |
| 186 | $query{spans} = $param{spans} ? 'true' : 'false'; |
| 187 | |
| 188 | # Add query |
| 189 | $url->query(\%query); |
| 190 | |
| 191 | $c->app->log->debug('Match info: ' . $url); |
| 192 | |
| 193 | # Create new user agent and set timeout to 30 seconds |
| 194 | my $ua = $c->ua; # Mojo::UserAgent->new; |
| 195 | $ua->inactivity_timeout(30); |
| 196 | |
| 197 | # non-blocking |
| 198 | if ($cb) { |
Nils Diewald | 791b590 | 2014-12-04 04:47:24 +0000 | [diff] [blame] | 199 | weaken $index; |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 200 | $ua->get( |
| 201 | $url => sub { |
| 202 | my $tx = pop; |
| 203 | $self->_process_response('match', $index, $tx); |
| 204 | return $cb->($index); |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | # Match info blocking |
| 209 | else { |
| 210 | my $tx = $ua->get($url); |
| 211 | return $self->_process_response('match', $index, $tx); |
| 212 | }; |
| 213 | }; |
| 214 | |
| 215 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 216 | # Get resource information |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 217 | sub resource { |
| 218 | my $self = shift; |
| 219 | my $index = shift; |
| 220 | |
| 221 | # Get controller |
| 222 | my $c = $index->controller; |
| 223 | |
| 224 | # If there is a callback, do async |
| 225 | my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE'; |
| 226 | |
| 227 | my %param = @_; |
| 228 | |
| 229 | # Rename info endpoints regarding resource |
| 230 | my $type = $param{type} // 'collection'; |
| 231 | $type = 'virtualcollection' if $type eq 'collection'; |
| 232 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 233 | # Create resource URL |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 234 | my $url = Mojo::URL->new($index->api)->path($type); |
| 235 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 236 | # Debugging |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 237 | $c->app->log->debug('Get resource info on '. $url); |
| 238 | |
| 239 | # Check for cached information |
| 240 | if (my $json = $c->chi->get($url->to_string)) { |
| 241 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 242 | # TODO: That's unfortunate, as it prohibits caching of multiple resources |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 243 | $c->app->log->debug('Get resource info from cache'); |
| 244 | $c->stash('search.resource' => $json); |
| 245 | return $cb->($index) if $cb; |
| 246 | return $json; |
| 247 | }; |
| 248 | |
| 249 | $c->stash('search._resource_cache' => $url->to_string); |
| 250 | |
| 251 | # Create new user agent and set timeout to 30 seconds |
| 252 | my $ua = $c->ua; # Mojo::UserAgent->new; |
| 253 | $ua->inactivity_timeout(30); |
| 254 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 255 | # Get resource information async |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 256 | if ($cb) { |
Nils Diewald | 791b590 | 2014-12-04 04:47:24 +0000 | [diff] [blame] | 257 | weaken $index; |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 258 | $ua->get( |
| 259 | $url => sub { |
| 260 | $self->_process_response('resource', $index, pop); |
| 261 | return $cb->($index); |
| 262 | }) |
| 263 | } |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 264 | |
| 265 | # Get resource information blocking |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 266 | else { |
| 267 | my $tx = $ua->get($url); |
| 268 | $self->_process_response('resource', $index, $tx); |
| 269 | }; |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | # Process response - especially error messages etc. |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 274 | sub _process_response { |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 275 | my ($self, $type, $index, $tx) = @_; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 276 | my $c = $index->controller; |
| 277 | |
| 278 | # An error has occurded |
| 279 | if (my $e = $tx->error) { |
| 280 | $c->notify( |
| 281 | error => |
| 282 | ($e->{code} ? $e->{code} . ': ' : '') . |
| 283 | $e->{message} . ' (remote)' |
| 284 | ); |
| 285 | return; |
| 286 | }; |
| 287 | |
| 288 | # Response was fine |
| 289 | if (my $res = $tx->success) { |
| 290 | |
| 291 | # Set api response for debugging |
Nils Diewald | 89d8801 | 2015-03-10 21:03:36 +0000 | [diff] [blame] | 292 | $index->api_response($res->body) if $c->kalamar_test_port; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 293 | |
| 294 | # Json failure |
| 295 | my $json; |
| 296 | unless ($json = $res->json) { |
| 297 | $c->notify(error => 'JSON response is invalid'); |
| 298 | return; |
| 299 | }; |
| 300 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 301 | # expected response for matches |
| 302 | if ($type eq 'matches') { |
| 303 | $self->_process_response_matches($index, $json); |
| 304 | } |
| 305 | elsif ($type eq 'trace') { |
| 306 | $self->_process_response_trace($index, $json); |
| 307 | } |
| 308 | elsif ($type eq 'match') { |
| 309 | $self->_process_response_match($index, $json); |
| 310 | } |
| 311 | elsif ($type eq 'resource') { |
| 312 | $self->_process_response_resource($index, $json); |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 315 | return 1 if ref $json ne 'HASH'; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 316 | |
| 317 | # Add warnings (Legacy) |
| 318 | if ($json->{warning}) { |
| 319 | $json->{warning} =~ s/;\s+null$//; |
| 320 | $c->notify(warn => $json->{warning}); |
| 321 | }; |
| 322 | |
| 323 | $self->_notify_on_error($c, 0, $json); |
| 324 | } |
| 325 | |
| 326 | # Request failed |
| 327 | else { |
| 328 | $self->_notify_on_error($c, 1, $tx->res); |
| 329 | }; |
| 330 | return 1; |
| 331 | }; |
| 332 | |
| 333 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 334 | # Handle match results |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 335 | sub _process_response_matches { |
| 336 | my ($self, $index, $json) = @_; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 337 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 338 | # Reformat benchmark counter |
| 339 | my $benchmark = $json->{benchmark}; |
| 340 | if ($benchmark && $benchmark =~ s/\s+(m)?s$//) { |
| 341 | $benchmark = sprintf("%.2f", $benchmark) . ($1 ? $1 : '') . 's'; |
| 342 | }; |
| 343 | |
| 344 | # Set benchmark |
| 345 | $index->benchmark($benchmark); |
| 346 | |
| 347 | # Set time exceeded |
| 348 | if ($json->{timeExceeded} && $json->{timeExceeded} eq Mojo::JSON::true) { |
| 349 | $index->time_exceeded(1); |
| 350 | }; |
| 351 | |
| 352 | # Set result values |
| 353 | $index->items_per_page($json->{itemsPerPage}); |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 354 | |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame^] | 355 | # Bouncing query |
| 356 | if ($json->{query}) { |
| 357 | $index->query_jsonld($json->{query}); |
| 358 | } |
| 359 | # Legacy |
| 360 | elsif ($json->{request}->{query}) { |
| 361 | $index->query_jsonld($json->{request}->{query}); |
| 362 | }; |
| 363 | |
| 364 | # Temporary: |
| 365 | my $collection_query = { |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 366 | '@type' => "koral:docGroup", |
| 367 | "operation" => "operation:or", |
| 368 | "operands" => [ |
| 369 | { |
| 370 | '@type' => "koral:docGroup", |
| 371 | "operation" => "operation:and", |
| 372 | "operands" => [ |
| 373 | { |
| 374 | '@type' => "koral:doc", |
| 375 | "key" => "title", |
| 376 | "match" => "match:eq", |
| 377 | "value" => "Der Birnbaum", |
| 378 | "type" => "type:string" |
| 379 | }, |
| 380 | { |
| 381 | '@type' => "koral:doc", |
| 382 | "key" => "pubPlace", |
| 383 | "match" => "match:eq", |
| 384 | "value" => "Mannheim", |
| 385 | "type" => "type:string" |
| 386 | }, |
| 387 | { |
| 388 | '@type' => "koral:docGroup", |
| 389 | "operation" => "operation:or", |
| 390 | "operands" => [ |
| 391 | { |
| 392 | '@type' => "koral:doc", |
| 393 | "key" => "subTitle", |
| 394 | "match" => "match:eq", |
| 395 | "value" => "Aufzucht oder Pflege", |
| 396 | "type" => "type:string" |
| 397 | }, |
| 398 | { |
| 399 | '@type' => "koral:doc", |
| 400 | "key" => "subTitle", |
| 401 | "match" => "match:eq", |
| 402 | "value" => "Gedichte", |
| 403 | "type" => "type:string" |
| 404 | } |
| 405 | ] |
| 406 | } |
| 407 | ] |
| 408 | }, |
| 409 | { |
| 410 | '@type' => "koral:doc", |
| 411 | "key" => "pubDate", |
| 412 | "match" => "match:geq", |
| 413 | "value" => "2015-03-05", |
| 414 | "type" => "type:date" |
| 415 | } |
| 416 | ] |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame^] | 417 | }; |
| 418 | |
| 419 | |
| 420 | # Bouncing collection query |
| 421 | if ($json->{collection}) { |
| 422 | $index->collection_jsonld($json->{collection}); |
| 423 | } |
| 424 | |
| 425 | # Legacy |
| 426 | elsif ($json->{request}->{collection}) { |
| 427 | $index->collection_jsonld($json->{request}->{collection}); |
| 428 | }; |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 429 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 430 | $index->results(_map_matches($json->{matches})); |
| 431 | |
| 432 | # Total results not set by stash |
| 433 | if ($index->total_results == -1) { |
| 434 | |
| 435 | if ($json->{totalResults} && $json->{totalResults} > -1) { |
| 436 | my $c = $index->controller; |
| 437 | |
| 438 | $c->app->log->debug('Cache total result'); |
| 439 | $c->chi->set($index->_api_cache => $json->{totalResults}, '120min'); |
| 440 | $index->total_results($json->{totalResults}); |
| 441 | }; |
| 442 | }; |
| 443 | }; |
| 444 | |
| 445 | |
| 446 | # Process query serialization response |
| 447 | sub _process_response_match { |
| 448 | my ($self, $index, $json) = @_; |
| 449 | $index->results(_map_match($json)); |
| 450 | }; |
| 451 | |
| 452 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 453 | # Process trace response |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 454 | sub _process_response_trace { |
| 455 | my ($self, $index, $json) = @_; |
| 456 | $index->query_jsonld($json); |
| 457 | }; |
| 458 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 459 | |
| 460 | # Process resource response |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 461 | sub _process_response_resource { |
| 462 | my ($self, $index, $json) = @_; |
| 463 | my $c = $index->controller; |
| 464 | |
| 465 | # TODO: That's unfortunate, as it prohibits multiple resources |
| 466 | $c->stash('search.resource' => $json); |
| 467 | $c->app->log->debug('Cache resource info'); |
| 468 | $c->chi->set($c->stash('search._resource_cache') => $json, '24 hours'); |
| 469 | }; |
| 470 | |
| 471 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 472 | # Parse error messages and forward them to the user |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 473 | sub _notify_on_error { |
| 474 | my ($self, $c, $failure, $res) = @_; |
| 475 | my $json = $res; |
| 476 | |
| 477 | my $log = $c->app->log; |
| 478 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 479 | # Check if the response is already json |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 480 | if (blessed $res) { |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 481 | $json = $res->json if blessed $res ne 'Mojo::JSON'; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 482 | }; |
| 483 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 484 | # Chec json response error message |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 485 | if ($json) { |
| 486 | if ($json->{error}) { |
| 487 | # Temp |
| 488 | $json->{error} =~ s/;\s+null$//; |
| 489 | $c->notify(error => $json->{error}); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | # New error messages |
| 494 | elsif ($json->{errstr}) { |
| 495 | # Temp |
| 496 | $json->{errstr} =~ s/;\s+null$//; |
| 497 | $c->notify(error => $json->{errstr}); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | # policy service error messages |
| 502 | elsif ($json->{status}) { |
| 503 | $c->notify(error => 'Middleware error ' . $json->{status}); |
| 504 | return; |
| 505 | }; |
| 506 | }; |
| 507 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 508 | # Doesn't matter what - there is a failure! |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 509 | if ($failure) { |
| 510 | $c->notify(error => ( |
| 511 | ($res->{code} ? $res->{code} . ': ' : '') . |
| 512 | ($res->{message} ? $res->{message} : 'Unknown error') . |
| 513 | ' (remote)' |
| 514 | )); |
| 515 | }; |
| 516 | }; |
| 517 | |
| 518 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 519 | # Cleanup array of matches |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 520 | sub _map_matches { |
| 521 | return () unless $_[0]; |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 522 | map { _map_match($_) } @{ shift() }; |
| 523 | }; |
| 524 | |
| 525 | |
| 526 | # Cleanup single match |
| 527 | sub _map_match { |
| 528 | my $x = shift or return; |
| 529 | $x->{ID} =~ s/^match\-[^!]+![^-]+-//; |
| 530 | $x->{docID} =~ s/^[^_]+_//; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 531 | |
| 532 | # Legacy: In old versions the text_id was part of the doc_id |
| 533 | unless ($x->{textID}) { |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 534 | ($x->{docID}, $x->{textID}) = split '\.', $x->{docID}; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 535 | }; |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 536 | $x; |
| 537 | }; |
| 538 | |
| 539 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 540 | # Build query url |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 541 | sub _query_url { |
| 542 | my ($index, %param) = @_; |
| 543 | |
| 544 | # Set cutoff from param |
| 545 | $index->cutoff(delete $param{cutoff}); |
| 546 | |
| 547 | # Set query language |
| 548 | $index->query_language(delete $param{query_language} // 'poliqarp'); |
| 549 | |
| 550 | # Should results be cached? Defaults to "yes" |
| 551 | $index->no_cache(1) if $param{no_cache}; |
| 552 | |
| 553 | # Init the query with stuff coming from the index |
| 554 | my %query; |
| 555 | $query{q} = $index->query; |
| 556 | $query{ql} = $index->query_language; |
| 557 | $query{page} = $index->start_page if $index->start_page; |
| 558 | $query{count} = $index->items_per_page if $index->items_per_page; |
| 559 | $query{cutoff} = 'true' if $index->cutoff; |
| 560 | |
| 561 | # Todo: support corpus and collection |
| 562 | # Create query url |
| 563 | my $url = Mojo::URL->new($index->api); |
| 564 | $url->query(\%query); |
| 565 | return $url; |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | |
| 569 | 1; |
| 570 | |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 571 | |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 572 | __END__ |
| 573 | |
| 574 | =pod |
| 575 | |
Nils Diewald | 9dfe010 | 2015-05-19 16:14:06 +0000 | [diff] [blame] | 576 | =encoding utf8 |
Nils Diewald | 996aa55 | 2014-12-02 03:26:44 +0000 | [diff] [blame] | 577 | |
Nils Diewald | 9dfe010 | 2015-05-19 16:14:06 +0000 | [diff] [blame] | 578 | =head1 NAME |
| 579 | |
| 580 | Kalamar::API |
| 581 | |
| 582 | =head1 DESCRIPTION |
| 583 | |
| 584 | L<Kalamar::API> is a search engine class for L<Mojolicious::Plugin::Search> |
| 585 | that uses the KorAP Web API. |
| 586 | |
| 587 | B<The Web API as well as L<Mojolicious::Plugin::Search> are not stable yet, |
| 588 | so this class is expected to change in the near future. Do not rely on its API!> |
| 589 | |
| 590 | |
| 591 | =head1 METHODS |
| 592 | |
| 593 | L<Kalamar::API> inherits all methods from L<Mojolicious::Plugin> and |
| 594 | implements the following new ones. |
| 595 | |
| 596 | |
| 597 | =head2 register |
| 598 | |
| 599 | See L<Mojolicious::Plugin::Search> for registering search engines. |
| 600 | In addition to the mentioned query parameters, the following parameters are supported: |
| 601 | |
| 602 | |
| 603 | =over 2 |
| 604 | |
| 605 | =item B<query_language> |
| 606 | |
| 607 | One of the supported query languages, like C<poliqarp> or C<annis>. |
| 608 | |
| 609 | |
| 610 | =item B<cutoff> |
| 611 | |
| 612 | Cut off results following the current page (i.e. don't count the number of results). |
| 613 | |
| 614 | |
| 615 | =item B<no_cache> |
| 616 | |
| 617 | Do not cache search results. Defaults to C<0>. |
| 618 | |
| 619 | |
| 620 | =back |
| 621 | |
| 622 | In addition to the mentioned index attributes, the following attributes are supported: |
| 623 | |
Akron | 456abd9 | 2015-06-02 15:07:21 +0200 | [diff] [blame] | 624 | =over 2 |
Nils Diewald | 9dfe010 | 2015-05-19 16:14:06 +0000 | [diff] [blame] | 625 | |
| 626 | =item B<api> |
| 627 | |
| 628 | The API address. |
| 629 | |
| 630 | |
| 631 | =item B<time_exceeded> |
| 632 | |
| 633 | Report on time outs, that may mean, not all results were retrieved. |
| 634 | |
| 635 | |
| 636 | =item B<api_request> |
| 637 | |
| 638 | Report the whole API request. |
| 639 | |
| 640 | |
| 641 | =item B<api_response> |
| 642 | |
| 643 | Report the whole API response (a KoralQuery object). |
| 644 | |
| 645 | |
| 646 | =item B<benchmarks> |
| 647 | |
| 648 | Report on processing time for benchmarking. |
| 649 | |
| 650 | |
| 651 | =item B<query_jsonld> |
| 652 | |
| 653 | The KoralQuery realization of the C<query> object. |
| 654 | |
| 655 | =back |
| 656 | |
| 657 | =head2 search |
| 658 | |
| 659 | Search the index. |
| 660 | |
| 661 | =head2 trace |
| 662 | |
| 663 | Trace query serializations. |
| 664 | |
| 665 | =head2 match |
| 666 | |
| 667 | Get match information. |
| 668 | |
| 669 | =head2 resource |
| 670 | |
| 671 | Get resource information. |
| 672 | |
| 673 | |
| 674 | =head1 COPYRIGHT AND LICENSE |
| 675 | |
| 676 | Copyright (C) 2015, L<IDS Mannheim|http://www.ids-mannheim.de/> |
| 677 | Author: L<Nils Diewald|http://nils-diewald.de/> |
| 678 | |
| 679 | Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/> |
| 680 | Corpus Analysis Platform at the |
| 681 | L<Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 682 | member of the |
| 683 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/> |
| 684 | and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project, |
| 685 | funded by the |
| 686 | L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>. |
| 687 | |
| 688 | Kalamar is free software published under the |
Akron | 456abd9 | 2015-06-02 15:07:21 +0200 | [diff] [blame] | 689 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>. |
Nils Diewald | 9dfe010 | 2015-05-19 16:14:06 +0000 | [diff] [blame] | 690 | |
| 691 | =cut |