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