blob: a94f0de8946bd9fd54fa0dfe3399efaa6f0a64c9 [file] [log] [blame]
Nils Diewald8f4b5da2014-12-03 22:13:39 +00001package Korap::Controller::Search;
2use 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
8sub 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'),
42 start_page => scalar $c->param('page'),
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
60sub 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;
87
88 return $c->respond_to(
89
90 # Render json if requested
91 json => sub {
92 # Add notifications to the matching json
93 # TODO: There should be a special notification engine doing that!
94 my $notes = $c->notifications(json => $index->results->[0]);
95 $c->render(json => $notes);
96 },
97
98 # Render html if requested
99 html => sub {
100 return $c->render(
101 layout => 'default',
102 template => 'match_info'
103 )
104 }
105 );
106 }
107 );
108};
109
1101;
111
112
113__END__