ADS/Search.pm

filter: Statementstmt 80.0% 96 / 120 Branchbran 52.7% 19 / 36 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 89.4% 17 / 19 Podpod 83.3% 5 / 6 totaltotal 74.3% 142 / 191 CC 21 SCAR 23.9
CC
21
Cov
73%
__ANON__
24.4
gather_search_terms
18.0
query
3.0
CRAP 10.9
↑ Summary ADS.pm »
1 package Astro::ADS::Search;
2 4 $Astro::ADS::Search::VERSION = '1.92';
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 extends 'Astro::ADS';
5 4 with 'Astro::ADS::Role::ResultMapper';
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Mojo::Base -strict; # do we want -signatures
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Mojo::File qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Mojo::Util qw( quote );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Types::Standard qw( Int Str ArrayRef HashRef ); # InstanceOf ConsumerOf
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 has [qw/q fq fl sort/] => (
18 is => 'rw',
19 );
20 4 has [qw/start rows/] => (
21 is => 'rw',
22 isa => Int->where( '$_ >= 0' ),
23 );
24 # TODO: add year and other fields
25 has [qw/authors objects bibcode/] => (
26 is => 'rw',
27 isa => ArrayRef[Str],
28 9 ▶ default => sub { return [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 4 );
30 has [qw/author_logic object_logic/] => (
31 is => 'rw',
32 isa => HashRef[],
33 6 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 4 );
35  
36 8 ▶before sort => sub {
Subroutine __ANON__: called CC=6 SCAR=32.0
37 8 my $orig = shift;
38 8 my $self = shift;
39 8 my ($field, $direction) = @_;
40  
41 8 ▶ return unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 8 0 8
42  
43 0 my $sort_field_re = qr/(?:id|author_count|bibcode|citation_count|citation_count_norm|classic_factor|first_author|date|entry_date|read_count|score)/;
44 0 ▶ if ($field =~ /^$sort_field_re\+(?:asc|desc)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /^$sort_field_re\+(?:asc|desc)$/u) 0 0 0
45 0 $orig->($self, $field);
46 }
47 0 ▶ if ($field !~ /^$sort_field_re$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^$sort_field_re$/u) 0 0 0
48 0 carp 'Invalid sort field: ', $field;
49 0 return;
50 }
51 0 ▶ if ($direction eq 'asc' || $direction eq 'desc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq "asc" or $direction eq "desc") 0 0 0
Condition0%0/3
$direction eq "asc" or $direction eq "desc"
l!l&&r!l&&!r
000
MC/DC0%0/2
$direction eq "asc" or $direction eq "desc"
$direction eq "asc" $direction eq "desc"
Truth table0%0/3
A or B
A= $direction eq "asc"B= $direction eq "desc"
ABresult
1X1
011
000
52 0 carp 'Invalid sort direction: ', $direction;
53 0 return;
54 }
55 0 $orig->($self, join('+', $field, $direction) );
56 4 };
57  
58 6 ▶sub query {
Subroutine query: called CC=3 SCAR=11.0
Pod documented
59 6 my ($self, $terms) = @_;
60 6 my $url = $self->base_url->clone->path('search/query');
61 6 ▶ my $search_terms = $self->gather_search_terms( $terms ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $search_terms = $self->gather_search_terms($terms) or return 6 0 6
62  
63 6 $url->query( $search_terms );
64 6 my $response = $self->get_response( $url );
65 6 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 1 5 6
66 1 carp $response->message;
67 1 my $error_obj = {
68 message => $response->message,
69 query => $search_terms,
70 url => $url,
71 };
72 1 return Astro::ADS::Result->new( {error => $error_obj} );
73 }
74  
75 5 my $json = $response->json;
76 5 return $self->parse_response( $json );
77 }
78  
79 1 ▶sub query_tree {
Subroutine query_tree: called CC=1 SCAR=0.0
Pod documented
80 1 my ($self, $terms) = @_;
81  
82 1 my $url = $self->base_url->clone->path('search/qtree');
83 1 $url->query( { q => $self->q, fl => $self->fl } );
84 1 my $response = $self->get_response( $url );
85  
86 1 my $qtree = $self->parse_qtree_response( $response );
87 1 return $qtree;
88 }
89  
90 1 ▶sub bigquery {
Subroutine bigquery: called CC=1 SCAR=0.0
Pod documented
91 1 my ($self, @bibcodes) = @_;
92  
93 1 my $bibcode_list = join "\n", 'bibcode', @bibcodes;
94  
95 1 my $url = $self->base_url->clone->path('search/bigquery');
96 #TODO why the parameters in the query?
97 1 $url->query( { q => $self->q, fl => $self->fl } );
98 1 my $response = $self->post_response( $url, $bibcode_list );
99  
100 1 my $json = $response->json;
101 1 return $self->parse_response( $json );
102 }
103  
104 8 ▶sub gather_search_terms {
Subroutine gather_search_terms: called CC=14 SCAR=28.9
Pod undocumented
105 8 my ($self, $terms) = @_;
106  
107 8 my @query = ();
108 8 ▶ if ( $terms && $terms->{q} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terms and $terms->{'q'}) { } 2 6 8
Condition100%3/3
$terms and $terms->{"q"}
!ll&&!rl&&r
512
MC/DC100%2/2
$terms and $terms->{"q"}
$terms $terms->{"q"}
Truth table100%3/3
A and B
A= $termsB= $terms->{"q"}
ABresult
0X0
100
111
109 2 push @query, delete $terms->{q};
110 }
111 else {
112 6 ▶ push @query, $self->q if $self->q;
Branch: 1 branch
BranchTrueFalseTotal
if $self->q 6 0 6
113 6 ▶ push @query, delete $terms->{'+q'} if exists $terms->{'+q'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $terms->{"+q"} 1 5 6
114  
115 6 ▶ if ( @{$self->authors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->authors;}) 1 5 6
116 1 my $tag = 'author:';
117 1 ▶ substr($tag, 0, 0) = '=' if $self->author_logic->{exact};
Branch: 1 branch
BranchTrueFalseTotal
if $self->author_logic->{"exact"} 0 1 1
118 1 ▶ if ( @{$self->authors} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->authors;} > 1) { } 1 0 1
119 1 ▶ my $logic = $self->author_logic->{OR} ? q{ OR } : q{ };
Branch: 1 branch
BranchTrueFalseTotal
$self->author_logic->{'OR'} ? : 0 1 1
120 1 push @query, "$tag(" . join( $logic, map { quote $_ } @{$self->authors}) . ')';
121 }
122 else {
123 0 push @query, $tag . quote $self->authors->[0];
124 }
125 }
126  
127 6 ▶ if ( @{$self->objects} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->objects;}) 1 5 6
128 1 my $tag = 'object:';
129 1 ▶ if ( @{$self->objects} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->objects;} > 1) { } 0 1 1
130 0 ▶ my $logic = $self->object_logic->{OR} ? q{ OR } : q{ };
Branch: 1 branch
BranchTrueFalseTotal
$self->object_logic->{'OR'} ? : 0 0 0
131 0 push @query, "$tag(" . join( $logic, map { quote $_ } @{$self->objects}) . ')';
132 }
133 else {
134 1 push @query, $tag . quote $self->objects->[0];
135 }
136 }
137  
138 # need to remember which attributes take multiple values
139 0 ▶ push @query, @{$self->bibcode} if @{$self->bibcode};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->bibcode;} 0 6 6
140 }
141
142 8 ▶ unless ( @query ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@query) 0 8 8
143 0 carp 'No search terms provided for query';
144 0 return;
145 }
146  
147 8 my $search_terms = {
148 q => join(q{ }, @query),
149 maybe fq => $self->fq,
150 maybe fl => $self->fl,
151 maybe start => $self->start,
152 maybe rows => $self->rows,
153 maybe sort => $self->sort,
154 %$terms
155 };
156  
157 8 return $search_terms;
158 }
159  
160 0 ▶sub add_authors {
Subroutine add_authors: not called CC=1 SCAR=6.9
Pod documented
161 0 my ($self, @authors) = @_;
162 0 push @{$self->authors}, @authors;
163 }
164  
165 0 ▶sub add_objects {
Subroutine add_objects: not called CC=1 SCAR=6.9
Pod documented
166 0 my ($self, @objects) = @_;
167 0 push @{$self->objects}, @objects;
168 }
169  
170 4 1;
171  
172 =pod
173  
174 =encoding UTF-8
175  
176 =head1 NAME
177  
178 Astro::ADS::Search - Queries the ADS Search endpoint and collects the results
179  
180 =head1 VERSION
181  
182 version 1.92
183  
184 =head1 SYNOPSIS
185  
186 my $search = Astro::ADS::Search->new({
187 q => '...', # initial search query
188 fl => '...', # return list of attributes
189 });
190  
191 my $result = $search->query();
192 my @papers = $result->papers();
193  
194 while ( my $t = $result->next_query() ) {
195 $result = $search->query( $t );
196 push @papers, $result->get_papers();
197 }
198  
199 while ( my $t = $result->next_query() ) {
200 push @papers, $result->more_papers( $t );
201 }
202  
203 while ( push @papers, $result->next_query()->more_papers() ) {
204 }
205  
206 =head1 DESCRIPTION
207  
208 Search for papers in the Harvard ADS
209  
210 You can put base terms in the creation of the object and use the
211 query method to add new terms to that query only
212  
213 =head1 Methods
214  
215 =head2 query
216  
217 Adding a field key C<+q> to the query method B<adds> the query
218 term to the existing query terms,
219 whereas specifying a value for C<q> in the query method
220 overwrites the query terms and neglects gathering other search attributes,
221 such as authors or objects.
222  
223 =head2 add_authors
224  
225 Add a list of authors to a search query. Authors added here will not be
226 deleted if the query attribute is updated.
227  
228 =head2 add_objects
229  
230 Add a list of objects to a search query. Objects added here will not be
231 deleted if the query attribute is updated.
232  
233 =head2 query_tree
234  
235 Will return the L<Abstract Syntax Tree|https://ui.adsabs.harvard.edu/help/api/api-docs.html#get-/search/qtree> for the query.
236  
237 =head2 bigquery
238  
239 Accepts a L<list of many IDs|https://ui.adsabs.harvard.edu/help/api/api-docs.html#post-/search/bigquery> and supports paging.
240  
241 =head2 Notes
242  
243 From the ADS API, the "=" sign turns off the synonym expansion feature
244 available with the author and title fields
245  
246 =head1 See Also
247  
248 =over 4
249  
250 =item * L<Astro::ADS>
251  
252 =item * L<Astro::ADS::Result>
253  
254 =item * L<ADS API|https://ui.adsabs.harvard.edu/help/api/>
255  
256 =item * L<Search Syntax|https://ui.adsabs.harvard.edu/help/search/search-syntax>
257  
258 =back
259  
260 =head1 COPYRIGHT AND LICENSE
261  
262 This software is Copyright (c) 2025 by Boyd Duffee.
263  
264 This is free software, licensed under:
265  
266 The MIT (X11) License
267  
268 =cut
↑ Summary ADS.pm »