Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 1 | package Korap::Controller::Search; |
| 2 | use Mojo::Base 'Mojolicious::Controller'; |
| 3 | |
| 4 | # Add X-Forwarded-For to user agent call everywhere |
| 5 | |
| 6 | |
| 7 | # Query the KorAP backends and render a template |
| 8 | sub query { |
| 9 | my $c = shift; |
| 10 | $c->layout('default'); |
| 11 | $c->title('KorAP'); |
| 12 | |
| 13 | my $query = $c->param('q'); |
| 14 | |
| 15 | # Base parameters for remote access |
| 16 | my %param = ( |
| 17 | query_language => scalar $c->param('ql'), |
| 18 | query => $query, |
| 19 | ); |
| 20 | |
| 21 | my $inspect = (scalar $c->param('action') // '') eq 'inspect' ? 1 : 0; |
| 22 | |
| 23 | # Just check the serialization non-blocking |
| 24 | if ($inspect) { |
| 25 | $c->search->trace( |
| 26 | %param => sub { |
| 27 | return $c->render(template => 'query_info'); |
| 28 | } |
| 29 | ); |
| 30 | return; |
| 31 | }; |
| 32 | |
| 33 | my $template = scalar $c->param('snippet') ? 'snippet' : 'search'; |
| 34 | |
| 35 | # Search non-blocking |
| 36 | $c->delay( |
| 37 | sub { |
| 38 | my $delay = shift; |
| 39 | $c->search( |
| 40 | cutoff => scalar $c->param('cutoff'), |
| 41 | count => scalar $c->param('count'), |
Nils Diewald | 034ea70 | 2015-01-16 19:41:52 +0000 | [diff] [blame] | 42 | start_page => scalar $c->param('p'), |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 43 | cb => $delay->begin, |
| 44 | %param |
| 45 | ) if $query; |
| 46 | |
| 47 | $c->search->resource( |
| 48 | type => 'collection', |
| 49 | $delay->begin |
| 50 | ); |
| 51 | }, |
| 52 | sub { |
| 53 | return $c->render(template => $template); |
| 54 | } |
| 55 | ); |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | # Get informations about a match |
| 60 | sub match_info { |
| 61 | my $c = shift; |
| 62 | |
| 63 | my $foundry = '*'; |
| 64 | my %query = (foundry => '*'); |
| 65 | if ($c->param('foundry')) { |
| 66 | $query{foundry} = $c->param('foundry'); |
| 67 | if ($c->param('layer')) { |
| 68 | $query{layer} = $c->param('layer'); |
| 69 | }; |
| 70 | if ($c->param('spans')) { |
| 71 | $query{spans} = 'true'; |
| 72 | }; |
| 73 | }; |
| 74 | |
| 75 | $c->render_later; |
| 76 | |
| 77 | # Use the API for fetching matching information non-blocking |
| 78 | $c->search->match( |
| 79 | corpus_id => $c->stash('corpus_id'), |
| 80 | doc_id => $c->stash('doc_id'), |
| 81 | match_id => $c->stash('match_id'), |
| 82 | %query, |
| 83 | |
| 84 | # Callback for async search |
| 85 | sub { |
| 86 | my $index = shift; |
Nils Diewald | 8f4b5da | 2014-12-03 22:13:39 +0000 | [diff] [blame] | 87 | return $c->respond_to( |
| 88 | |
| 89 | # Render json if requested |
| 90 | json => sub { |
| 91 | # Add notifications to the matching json |
| 92 | # TODO: There should be a special notification engine doing that! |
| 93 | my $notes = $c->notifications(json => $index->results->[0]); |
| 94 | $c->render(json => $notes); |
| 95 | }, |
| 96 | |
| 97 | # Render html if requested |
| 98 | html => sub { |
| 99 | return $c->render( |
| 100 | layout => 'default', |
| 101 | template => 'match_info' |
| 102 | ) |
| 103 | } |
| 104 | ); |
| 105 | } |
| 106 | ); |
| 107 | }; |
| 108 | |
| 109 | 1; |
| 110 | |
| 111 | |
| 112 | __END__ |