| 1 | package Catmandu::Searchable; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Catmandu::Util qw(:is); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | requires 'translate_sru_sortkeys'; | |||||||||
| 12 | 1 | requires 'translate_cql_query'; | |||||||||
| 13 | 1 | requires 'search'; | |||||||||
| 14 | 1 | requires 'searcher'; | |||||||||
| 15 | 1 | requires 'delete_by_query'; | |||||||||
| 16 | |||||||||||
| 17 | 1 | has default_limit => (is => 'ro', builder => 'default_default_limit'); | |||||||||
| 18 | 1 | has maximum_limit => (is => 'ro', builder => 'default_maximum_limit'); | |||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | sub default_default_limit {10} | ||||||||
|
Subroutine
default_default_limit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | 2 | ▶ | sub default_maximum_limit {1000} | ||||||||
|
Subroutine
default_maximum_limit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | sub normalize_query {$_[1]} | ||||||||
|
Subroutine
normalize_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | |||||||||||
| 25 | my $AROUND_SEARCH = sub { | ||||||||||
| 26 | 0 | ▶ | my ($orig, $self, %args) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||
| 27 | 0 | ▶ | $args{limit} = $self->default_limit unless is_natural($args{limit}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | ▶ | $args{start} = 0 unless is_natural($args{start}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | $args{start} += 0; | |||||||||
| 30 | 0 | $args{limit} += 0; | |||||||||
| 31 | 0 | ▶ | if ($args{limit} > $self->maximum_limit) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | $args{limit} = $self->maximum_limit; | |||||||||
| 33 | } | ||||||||||
| 34 | 0 | ▶ | if (is_positive(my $page = delete $args{page})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | $args{start} = ($page - 1) * $args{limit}; | |||||||||
| 36 | } | ||||||||||
| 37 | 0 | ▶ | if (my $sru_sortkeys = delete $args{sru_sortkeys}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | $args{sort} = $self->translate_sru_sortkeys($sru_sortkeys); | |||||||||
| 39 | } | ||||||||||
| 40 | 0 | ▶ | if (my $cql_query = delete $args{cql_query}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | $args{query} = $self->translate_cql_query($cql_query); | |||||||||
| 42 | } | ||||||||||
| 43 | 0 | $args{query} = $self->normalize_query($args{query}); | |||||||||
| 44 | 0 | $orig->($self, %args); | |||||||||
| 45 | 1 | }; | |||||||||
| 46 | |||||||||||
| 47 | 1 | around search => $AROUND_SEARCH; | |||||||||
| 48 | 1 | around searcher => $AROUND_SEARCH; | |||||||||
| 49 | |||||||||||
| 50 | around delete_by_query => sub { | ||||||||||
| 51 | 0 | ▶ | my ($orig, $self, %args) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 52 | 0 | ▶ | if (my $cql = delete $args{cql_query}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | $args{query} = $self->translate_cql_query($cql); | |||||||||
| 54 | } | ||||||||||
| 55 | 0 | $args{query} = $self->normalize_query($args{query}); | |||||||||
| 56 | 0 | $orig->($self, %args); | |||||||||
| 57 | 0 | return; | |||||||||
| 58 | 1 | }; | |||||||||
| 59 | |||||||||||
| 60 | 1 | 1; | |||||||||
| 61 | |||||||||||
| 62 | __END__ | ||||||||||
| 63 | |||||||||||
| 64 | =pod | ||||||||||
| 65 | |||||||||||
| 66 | =head1 NAME | ||||||||||
| 67 | |||||||||||
| 68 | Catmandu::Searchable - Optional role for searchable stores | ||||||||||
| 69 | |||||||||||
| 70 | =head1 SYNOPSIS | ||||||||||
| 71 | |||||||||||
| 72 | my $store = Catmandu::Store::Solr->new(); | ||||||||||
| 73 | |||||||||||
| 74 | # Return one page of search results (page size = 1000) | ||||||||||
| 75 | my $hits = $store->bag->search( | ||||||||||
| 76 | query => 'dna' , | ||||||||||
| 77 | start => 0 , | ||||||||||
| 78 | limit => 100 , | ||||||||||
| 79 | sort => 'title desc', | ||||||||||
| 80 | ); | ||||||||||
| 81 | |||||||||||
| 82 | # Return all the search results as iterator | ||||||||||
| 83 | my $it = $store->bag->searcher(query => 'dna'); | ||||||||||
| 84 | $it->each(sub { ...}); | ||||||||||
| 85 | |||||||||||
| 86 | $store->bag->delete_by_query(query => 'dna'); | ||||||||||
| 87 | |||||||||||
| 88 | =head1 METHODS | ||||||||||
| 89 | |||||||||||
| 90 | =head2 search(query => $query, start => $start, page => $page, limit => $num, sort => $sort, cql_query => $cql) | ||||||||||
| 91 | |||||||||||
| 92 | Search the database and returns a L<Catmandu::Hits> on success. The Hits represents one | ||||||||||
| 93 | result page of at most $num results. The $query and $sort should implement the | ||||||||||
| 94 | query and sort syntax of the underlying search engine. If the CQL language is supported | ||||||||||
| 95 | by the Store, then optionally a $cql_query search can be excuted on the Searchable. | ||||||||||
| 96 | |||||||||||
| 97 | Optionally provide the index of the first result using the C<start> option, or the starting page using | ||||||||||
| 98 | the C<page> option. The number of records in a result page can be set using the C<limit> option. Sorting | ||||||||||
| 99 | options are being sent verbatim to the underlying search engine. | ||||||||||
| 100 | |||||||||||
| 101 | =head2 searcher(query => $query, start => $start, limit => $num, sort => $sort, cql_query => $cql) | ||||||||||
| 102 | |||||||||||
| 103 | Search the database and return a L<Catmandu::Iterable> on success. This iterator can be | ||||||||||
| 104 | used to loop over the complete result set. The $query and $sort should implement the | ||||||||||
| 105 | query and sort syntax of the underlying search engine. If the CQL language is supported | ||||||||||
| 106 | by the Store, then optionally a $cql_query search can be excuted on the Searchable. | ||||||||||
| 107 | |||||||||||
| 108 | Optionally provide the index of the first result using the C<start> option. The number of records in | ||||||||||
| 109 | a page can be set using the C<limit> option. Sorting options are being sent verbatim to the underlying | ||||||||||
| 110 | search engine. | ||||||||||
| 111 | |||||||||||
| 112 | =head2 delete_by_query(query => $query) | ||||||||||
| 113 | |||||||||||
| 114 | Delete items from the database that match $query | ||||||||||
| 115 | |||||||||||
| 116 | =head1 SEE ALSO | ||||||||||
| 117 | |||||||||||
| 118 | L<Catmandu::Hits>, L<Catmandu::Paged> | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||