| 1 | package Catmandu::Fix::search_in_store; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Catmandu::Util::Path qw(as_path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Catmandu; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | with 'Catmandu::Fix::Builder'; | |||||||||
| 10 | |||||||||||
| 11 | #options/arguments | ||||||||||
| 12 | 1 | has path => (fix_arg => 1); | |||||||||
| 13 | 1 | has store_name => (fix_opt => 1, init_arg => 'store'); | |||||||||
| 14 | 1 | has bag_name => (fix_opt => 1, init_arg => 'bag'); | |||||||||
| 15 | 3 | ▶ | has limit => (fix_opt => 1, init_arg => undef, default => sub {20}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 4 | ▶ | has start => (fix_opt => 1, init_arg => undef, default => sub {0}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | has sort => (fix_opt => 1, init_arg => undef, default => sub {''}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | has store_args => (fix_opt => 'collect'); | |||||||||
| 19 | |||||||||||
| 20 | #internal | ||||||||||
| 21 | 1 | has store => (is => 'lazy', init_arg => undef, builder => '_build_store'); | |||||||||
| 22 | 1 | has bag => (is => 'lazy', init_arg => undef, builder => '_build_bag'); | |||||||||
| 23 | |||||||||||
| 24 | 5 | ▶ | sub _build_store { | ||||||||
|
Subroutine
_build_store: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 5 | my $self = $_[0]; | |||||||||
| 26 | 5 | Catmandu->store($self->store_name, %{$self->store_args}); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 5 | ▶ | sub _build_bag { | ||||||||
|
Subroutine
_build_bag: called CC=2 SCAR=6.9
| |||||||||||
| 30 | 5 | my ($self) = @_; | |||||||||
| 31 | 5 | ▶ | defined $self->bag_name | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | ? $self->store->bag($self->bag_name) | ||||||||||
| 33 | : $self->store->bag; | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 5 | ▶ | sub _build_fixer { | ||||||||
|
Subroutine
_build_fixer: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 5 | my ($self) = @_; | |||||||||
| 38 | 5 | my $bag = $self->bag; | |||||||||
| 39 | 5 | my $limit = $self->limit; | |||||||||
| 40 | 5 | my $start = $self->start; | |||||||||
| 41 | 5 | my $sort = $self->sort; | |||||||||
| 42 | as_path($self->path)->updater( | ||||||||||
| 43 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 5 | my $val = $_[0]; | |||||||||
| 45 | 5 | my $hits = $bag->search( | |||||||||
| 46 | query => $val, | ||||||||||
| 47 | start => $start, | ||||||||||
| 48 | limit => $limit, | ||||||||||
| 49 | sort => $sort | ||||||||||
| 50 | ); | ||||||||||
| 51 | +{ | ||||||||||
| 52 | 5 | start => $start, | |||||||||
| 53 | limit => $limit, | ||||||||||
| 54 | total => $hits->total, | ||||||||||
| 55 | hits => $hits->to_array | ||||||||||
| 56 | }; | ||||||||||
| 57 | } | ||||||||||
| 58 | 5 | ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | =head1 NAME | ||||||||||
| 62 | |||||||||||
| 63 | Catmandu::Fix::search_in_store - use the value as query, and replace it by a search object | ||||||||||
| 64 | |||||||||||
| 65 | =head1 SYNTAX | ||||||||||
| 66 | |||||||||||
| 67 | search_in_store(path) | ||||||||||
| 68 | |||||||||||
| 69 | search_in_store(path,store: 'store', bag: 'bag', limit: 0, start: 0, sort: 'title desc') | ||||||||||
| 70 | |||||||||||
| 71 | =head1 RETURN VALUE | ||||||||||
| 72 | |||||||||||
| 73 | { | ||||||||||
| 74 | start: 0, | ||||||||||
| 75 | limit: 0, | ||||||||||
| 76 | hits: [], | ||||||||||
| 77 | total: 1000 | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | cf. L<Catmandu::Hits> | ||||||||||
| 81 | |||||||||||
| 82 | =head1 PARAMETERS | ||||||||||
| 83 | |||||||||||
| 84 | =head2 path | ||||||||||
| 85 | |||||||||||
| 86 | The location in the perl hash where the query is stored. | ||||||||||
| 87 | |||||||||||
| 88 | See L<Catmandu::Fix/"PATHS"> for more information about paths. | ||||||||||
| 89 | |||||||||||
| 90 | =head2 store | ||||||||||
| 91 | |||||||||||
| 92 | The name of the store. | ||||||||||
| 93 | |||||||||||
| 94 | This store MUST be an implementation of L<Catmandu::Searchable>. | ||||||||||
| 95 | |||||||||||
| 96 | There are several ways to refer to a store: | ||||||||||
| 97 | |||||||||||
| 98 | * by full package name ( e.g. 'Catmandu::Store::Solr' ) | ||||||||||
| 99 | * by short package name ( e.g. 'Solr' ) | ||||||||||
| 100 | * by name defined in the Catmandu configuration | ||||||||||
| 101 | |||||||||||
| 102 | See L<Catmandu/store-NAME> for more information. | ||||||||||
| 103 | |||||||||||
| 104 | Default is 'default'. | ||||||||||
| 105 | |||||||||||
| 106 | =head2 bag | ||||||||||
| 107 | |||||||||||
| 108 | Name of bag. | ||||||||||
| 109 | |||||||||||
| 110 | Default is 'data'. | ||||||||||
| 111 | |||||||||||
| 112 | =head2 limit | ||||||||||
| 113 | |||||||||||
| 114 | only return $limit number of records. | ||||||||||
| 115 | |||||||||||
| 116 | =head2 start | ||||||||||
| 117 | |||||||||||
| 118 | offset of records to return | ||||||||||
| 119 | |||||||||||
| 120 | =head2 sort | ||||||||||
| 121 | |||||||||||
| 122 | sort records before slicing them. | ||||||||||
| 123 | |||||||||||
| 124 | This parameter is store specific. | ||||||||||
| 125 | |||||||||||
| 126 | =head1 OTHER PARAMETERS | ||||||||||
| 127 | |||||||||||
| 128 | other parameters are given to the contructor of the L<Catmandu::Store> | ||||||||||
| 129 | |||||||||||
| 130 | e.g. catmandu.yml: | ||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | store: | ||||||||||
| 134 | catalog: | ||||||||||
| 135 | package: "Catmandu::Store::Solr" | ||||||||||
| 136 | |||||||||||
| 137 | e.g. fix: | ||||||||||
| 138 | |||||||||||
| 139 | search_in_store('foo.query', store:'catalog', bag: 'data', url: 'http://localhost:8983/solr/catalog') | ||||||||||
| 140 | |||||||||||
| 141 | =head1 EXAMPLES | ||||||||||
| 142 | |||||||||||
| 143 | #search in Catmandu->store->bag, and store first 20 results in the foo.query.hits | ||||||||||
| 144 | search_in_store('foo.query') | ||||||||||
| 145 | |||||||||||
| 146 | #search in Catmandu->store->bag, and store first 20 results in the foo.query.hits | ||||||||||
| 147 | search_in_store('foo.query', store:'default') | ||||||||||
| 148 | |||||||||||
| 149 | #search in Catmandu->store->bag; limit number of results to 10 | ||||||||||
| 150 | search_in_store('foo.query', store:'default', limit: 10) | ||||||||||
| 151 | |||||||||||
| 152 | #search in Catmandu->store->bag; limit number of result to 10, starting from 15 | ||||||||||
| 153 | search_in_store('foo.query', store:'default', limit: 10, start: 15) | ||||||||||
| 154 | |||||||||||
| 155 | #search in Catmandu->store->bag('persons'); sort by year descending, and by title ascending | ||||||||||
| 156 | search_in_store('foo.query', store:'default', bag:'persons', sort: 'year desc,title asc') | ||||||||||
| 157 | |||||||||||
| 158 | =head1 AUTHORS | ||||||||||
| 159 | |||||||||||
| 160 | Nicolas Franck C<< <nicolas.franck at ugent.be> >> | ||||||||||
| 161 | |||||||||||
| 162 | =head1 SEE ALSO | ||||||||||
| 163 | |||||||||||
| 164 | L<Catmandu::Fix> | ||||||||||
| 165 | |||||||||||
| 166 | L<Catmandu::Store> | ||||||||||
| 167 | |||||||||||
| 168 | =cut | ||||||||||
| 169 | |||||||||||
| 170 | 1 | 1; | |||||||||