File Coverage

blib/lib/Data/SearchEngine/Query.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::SearchEngine::Query;
2             {
3             $Data::SearchEngine::Query::VERSION = '0.33';
4             }
5 1     1   33839 use Moose;
  0            
  0            
6             use MooseX::Storage;
7              
8             # ABSTRACT: Query to pass to an engine.
9              
10             with 'MooseX::Storage::Deferred';
11              
12             use Data::SearchEngine::Meta::Attribute::Trait::Digestable;
13             use Digest::MD5;
14              
15              
16             has count => (
17             traits => [qw(Digestable)],
18             is => 'ro',
19             isa => 'Int',
20             default => 10
21             );
22              
23              
24             has debug => (
25             is => 'rw',
26             isa => 'Str',
27             predicate => 'has_debug'
28             );
29              
30              
31             has facets => (
32             traits => [ 'Hash', 'Digestable' ],
33             is => 'rw',
34             isa => 'HashRef[Any]',
35             default => sub { {} },
36             handles => {
37             facet_names => 'keys',
38             get_facet => 'get',
39             set_facet => 'set',
40             has_facets => 'count'
41             }
42             );
43              
44              
45             has fields => (
46             traits => [qw(Digestable)],
47             is => 'rw',
48             isa => 'Str|ArrayRef[Str]',
49             predicate => 'has_fields'
50             );
51              
52              
53             has filters => (
54             traits => [ 'Hash', 'Digestable' ],
55             is => 'rw',
56             isa => 'HashRef[Any]',
57             default => sub { {} },
58             handles => {
59             filter_names=> 'keys',
60             get_filter => 'get',
61             set_filter => 'set',
62             has_filters => 'count'
63             }
64             );
65              
66              
67             has index => (
68             traits => [qw(Digestable)],
69             is => 'rw',
70             isa => 'Str|ArrayRef[Str]',
71             predicate => 'has_index'
72             );
73              
74              
75             has order => (
76             traits => [qw(Digestable)],
77             is => 'rw',
78             isa => 'Str|ArrayRef|HashRef',
79             predicate => 'has_order'
80             );
81              
82              
83             has original_query => (
84             traits => [qw(Digestable)],
85             is => 'rw',
86             isa => 'Str|Undef',
87             lazy => 1,
88             default => sub { my $self = shift; return $self->query }
89             );
90              
91              
92             has page => (
93             traits => [qw(Digestable)],
94             is => 'ro',
95             isa => 'Int',
96             default => 1
97             );
98              
99              
100             has query => (
101             traits => [qw(Digestable)],
102             is => 'rw',
103             isa => 'Str|HashRef|ArrayRef',
104             predicate => 'has_query'
105             );
106              
107              
108             has type => (
109             traits => [qw(Digestable)],
110             is => 'rw',
111             isa => 'Str',
112             predicate => 'has_type'
113             );
114              
115              
116             sub digest {
117             my $self = shift;
118              
119             my $digester = Digest::MD5->new;
120              
121             my @attributes = $self->meta->get_attribute_list;
122             foreach my $aname (@attributes) {
123             my $attr = $self->meta->get_attribute($aname);
124              
125             next unless $attr->does('Digest::SearchEngine::Meta::Attribute::Trait::Digestable');
126              
127             if($attr->has_digest_value) {
128             $digester->add(lc($attr->digest_value));
129             } else {
130             my $reader = $attr->get_read_method;
131             my $val = $attr->$reader;
132             if(defined($val)) {
133             $digester->add(lc($val));
134             }
135             }
136              
137             }
138             return $digester->b64digest;
139             }
140              
141              
142             sub has_filter_like {
143             my ($self, $predicate) = @_;
144              
145             return grep { $predicate->() } $self->filter_names;
146             }
147              
148             no Moose;
149             __PACKAGE__->meta->make_immutable;
150              
151             1;
152              
153             __END__
154             =pod
155              
156             =head1 NAME
157              
158             Data::SearchEngine::Query - Query to pass to an engine.
159              
160             =head1 VERSION
161              
162             version 0.33
163              
164             =head1 DESCRIPTION
165              
166             The query object has some common attributes one would expect when performing
167             a search. It has the added benefit of producing a digest that can be used
168             with L<Data::SearchEngine::Results> ability to serialize to implement caching.
169              
170             If you add new attributes to a subclass, be sure and add the Digestable
171             trait to any attributes you want to be included in the digest as document in
172             L<Data::SearchEngine::Meta::Attribute::Trait::Digestable>.
173              
174             =head1 ATTRIBUTES
175              
176             =head2 count
177              
178             The number of results this query should return.
179              
180             =head2 debug
181              
182             A string value denoting that this query should include debugging information
183             in the response. Uses a string as the type because some search indexes
184             allow you to specify the type of debugging. For those that just use a flag,
185             the predicate should be checked so that any true value results in debugging.
186              
187             =head2 facets
188              
189             A HashRef of facets used with the query. The key should be the facet name and
190             the value is the facet's value. Consult the documentation for your backend to
191             see how this is used (if at all).
192              
193             =head2 fields
194              
195             The fields to return. Some search engine backends allow you to only return
196             part of a document.
197              
198             =head2 filters
199              
200             A HashRef of filters used with the query. The key should be the filter name
201             and the value is the filter's value. Consult the documentation for your
202             backend to see how this is used.
203              
204             =head2 index
205              
206             The index we will be querying. Some search engine backends allow multiple
207             indices and need this attribute.
208              
209             =head2 order
210              
211             The order in which the results should be sorted.
212              
213             =head2 original_query
214              
215             The "original" query that the user searched for. Provided because some
216             backends may need to modify the C<query> attribute to a more palatable form.
217             If no value is provided for this attribute then it will assume the same
218             value as C<query>.
219              
220             =head2 page
221              
222             Which page of results to show.
223              
224             =head2 query
225              
226             The query string to search for. This attribute may be a Str, ArrayRef or a
227             HashRef. Some backends (like ElasticSearch) require complex data structures
228             to perform searches and need a HashRef for their queries. B<NOTE:> It is
229             advised that you set C<original_query> to a string so that the results
230             object has a clean string to show end-users.
231              
232             =head2 type
233              
234             The type of query to use. Some backends (Solr and ElasticSearch) will use a
235             query type, if specified.
236              
237             =head1 METHODS
238              
239             =head2 has_debug
240              
241             Returns true if this Query has a value for debug.
242              
243             =head2 facet_names
244              
245             Get the names of all facets.
246              
247             =head2 get_facet
248              
249             Get the value for the specified facet.
250              
251             =head2 has_facets
252              
253             Predicate that returns true if this Query has any facets. Actually, it
254             returns the count but it does the same thing.
255              
256             =head2 set_facet
257              
258             Set the value for the specified facet.
259              
260             =head2 has_fields
261              
262             Returns true if this query has fields specified.
263              
264             =head2 filter_names
265              
266             Return an array of filter names that are present in this query.
267              
268             =head2 get_filter
269              
270             Gets the value for the specified filter.
271              
272             =head2 has_filters
273              
274             Predicate that returns true if this query has filters.
275              
276             =head2 set_filter
277              
278             Sets the value for the specified filter.
279              
280             =head2 has_index
281              
282             Returns true if this query has an index specified.
283              
284             =head2 has_query
285              
286             Returns true if this Query has a query string.
287              
288             =head2 has_type
289              
290             Returns true if this Query has a type set.
291              
292             =head2 digest
293              
294             Returns a unique digest identifying this Query. Useful as a key when caching.
295              
296             =head2 has_filter_like
297              
298             Returns true if any of the filter names match the provided subroutine:
299              
300             $query->set_filter('foo', 'bar');
301             $query->has_filter_like(sub { /^fo/ })s; # true!
302              
303             =head1 AUTHOR
304              
305             Cory G Watson <gphat@cpan.org>
306              
307             =head1 COPYRIGHT AND LICENSE
308              
309             This software is copyright (c) 2012 by Cold Hard Code, LLC.
310              
311             This is free software; you can redistribute it and/or modify it under
312             the same terms as the Perl 5 programming language system itself.
313              
314             =cut
315