Skip to content

Commit

Permalink
Support fields object in search responses
Browse files Browse the repository at this point in the history
Change-Id: I4c80d06182852fb56f77072789f04bd0f6b94f94
  • Loading branch information
Akron committed Jul 18, 2024
1 parent 20903f3 commit 95dfb25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Add pages to references in snippet view. (diewald)
- Added category to Piwik calls. (diewald)
- Add SpaCy with STTS to annotation assistant. (diewald)
- Support field objects in search responses. (diewald)

0.54 2024-06-10
- Remove deprecated 'matchInfo' API path. (diewald, margaretha)
Expand Down
18 changes: 18 additions & 0 deletions lib/Kalamar/Controller/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ sub _map_matches {
sub _map_match {
my $match = shift or return;

if ($match->{fields}) {
_flatten_fields($match->{fields}, $match);
};

# Legacy match id
if ($match->{matchID}) {
$match->{matchID} =~ s/^match\-(?:[^!]+!|[^_]+_)[^\.]+?\..+?-([pc]\d)/$1/ or
Expand All @@ -564,6 +568,20 @@ sub _map_match {
};


# Flatten requested field objects
sub _flatten_fields {
my $fields_obj = shift;
my $flat_fields = shift // {};

return $flat_fields if ref $fields_obj ne 'ARRAY';

foreach (@{$fields_obj}) {
next unless ref $_ ne 'HASH' || $_->{'key'};
$flat_fields->{$_->{'key'}} //= $_->{value};
};
return $flat_fields;
};

1;


Expand Down
30 changes: 30 additions & 0 deletions t/query.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use Mojo::Base -strict;
use Test::Mojo;
use Test::More;
use Mojo::File qw/path/;
use Mojo::JSON qw'decode_json';
use Kalamar::Controller::Search;


Expand Down Expand Up @@ -350,5 +351,34 @@ $err = $t->get_ok('/?q=baum&pipe=glemm')
is(defined $err ? $err->text : '', '');


my $base_fixtures = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
my $text_info = $base_fixtures->child('response_textinfo_goe_agi_00000.json')->slurp;
my $fields = decode_json($text_info)->{json}->{document}->{fields};

my $f = Kalamar::Controller::Search::_flatten_fields($fields);

is($f->{textSigle}, 'GOE/AGI/00000');
is($f->{author}, 'Goethe, Johann Wolfgang von');
is($f->{docSigle}, 'GOE/AGI');
is($f->{docTitle}, 'Goethe: Autobiographische Schriften III, (1813-1816, 1819-1829)');
is($f->{textType}, 'Autobiographie');
is($f->{language}, 'de');
is($f->{availability}, 'ACA-NC');
is($f->{title}, 'Italienische Reise');
is($f->{creationDate}, '1813');
is($f->{pubDate}, '1982');
is($f->{reference}, 'Goethe, Johann Wolfgang von: Italienische Reise. Auch ich in Arkadien!, (Geschrieben: 1813-1816), In: Goethe, Johann Wolfgang von: Goethes Werke, Bd. 11, Autobiographische Schriften III, Hrsg.: Trunz, Erich. München: Verlag C. H. Beck, 1982, S. 9-349');
is($f->{subTitle}, 'Auch ich in Arkadien!');
is($f->{tokenSource}, 'base#tokens');
is($f->{foundries}, 'corenlp corenlp/constituency corenlp/morpho corenlp/sentences dereko dereko/structure dereko/structure/base-sentences-paragraphs-pagebreaks malt malt/dependency marmot marmot/morpho opennlp opennlp/morpho opennlp/sentences treetagger treetagger/morpho');
is($f->{publisher}, 'Verlag C. H. Beck');
is($f->{corpusAuthor}, 'Goethe, Johann Wolfgang von');
is($f->{layerInfos}, 'corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens');
is($f->{pubPlace}, 'München');
is($f->{corpusTitle}, 'Goethes Werke');
is($f->{corpusSigle}, 'GOE');
is($f->{corpusEditor}, 'Trunz, Erich');


done_testing;
__END__

0 comments on commit 95dfb25

Please sign in to comment.