File Coverage

blib/lib/App/ElasticSearch/Utilities/QueryString/Plugin.pm
Criterion Covered Total %
statement 30 32 93.7
branch 4 8 50.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 43 50 86.0


line stmt bran cond sub pod time code
1             package App::ElasticSearch::Utilities::QueryString::Plugin;
2             # ABSTRACT: Moo::Role for implementing QueryString Plugins
3              
4 9     9   461064 use v5.16;
  9         47  
5 9     9   93 use warnings;
  9         33  
  9         801  
6              
7             our $VERSION = '8.8'; # VERSION
8              
9 9     9   4394 use Hash::Merge::Simple qw(clone_merge);
  9         4439  
  9         826  
10 9     9   651 use Moo::Role;
  9         20147  
  9         69  
11 9     9   6545 use Ref::Util qw(is_arrayref is_hashref);
  9         5920  
  9         812  
12 9     9   4830 use Types::Standard qw( HashRef Str Int );
  9         985411  
  9         146  
13              
14              
15             # Attributes
16             has name => (
17             is => 'lazy',
18             isa => Str,
19             );
20             sub _build_name {
21 7     7   132 my $self = shift;
22 7         17 my $class = ref $self;
23 7         182 return (split /::/, $class)[-1];
24             }
25              
26              
27             has priority => (
28             is => 'lazy',
29             isa => Int,
30             );
31 1     1   48 sub _build_priority { 50; }
32              
33              
34             has fields_meta => (
35             is => 'rw',
36             isa => HashRef,
37             default => sub { {} },
38             );
39              
40              
41             requires qw(handle_token);
42              
43             around 'handle_token' => sub {
44             my $orig = shift;
45             my $self = shift;
46             my $refs = $orig->($self,@_);
47             if( defined $refs ) {
48             if( is_arrayref($refs) ) {
49             foreach my $doc (@{ $refs }) {
50             $doc->{_by} = $self->name;
51             }
52             }
53             elsif( is_hashref($refs) ) {
54             $refs->{_by} = $self->name;
55             }
56             return $refs;
57             }
58             else {
59             return;
60             }
61             };
62              
63              
64             # Handle Build Args
65              
66              
67             sub BUILDARGS {
68 7     7 0 34826 my($class,%in) = @_;
69              
70 7         41 my @search = map { $_ => lc $_ } ($class,(split /::/, $class)[-1]);
  14         58  
71 7 50       32 my $options = exists $in{options} ? delete $in{options} : {};
72              
73 7         15 my @options = ();
74 7         22 foreach my $s (@search) {
75 28 50       81 if( exists $options->{$s} ) {
76 0         0 push @options, $options->{$s};
77 0         0 last;
78             }
79             }
80 7 50       27 push @options, \%in if keys %in;
81              
82 7 50       36 return scalar(@options) ? clone_merge(@options) : {};
83             }
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =head1 NAME
92              
93             App::ElasticSearch::Utilities::QueryString::Plugin - Moo::Role for implementing QueryString Plugins
94              
95             =head1 VERSION
96              
97             version 8.8
98              
99             =head1 ATTRIBUTES
100              
101             =head2 name
102              
103             Name of the plugin, used in debug reporting.
104              
105             =head2 priority
106              
107             Priority is an integer which determmines the order tokens are parsed in
108             low->high order.
109              
110             =head2 fields_meta
111              
112             A hash reference with the field data from L<App::ElasticSearch::Utilities::es_index_fields>.
113              
114             =head1 INTERFACE
115              
116             =head2 handle_token()
117              
118             The handle_token() routine receives a single token from the command line, often a single word
119             and returns a hash reference specifying
120              
121             The token expansion plugins can return undefined, which is basically a noop on the token.
122             The plugin can return a hash reference, which marks that token as handled and no other plugins
123             receive that token. The hash reference may contain:
124              
125             =head2 query_string
126              
127             This is the rewritten bits that will be reassembled in to the final query string.
128              
129             =head2 condition
130              
131             This is usually a hash reference representing the condition going into the bool query. For instance:
132              
133             { terms => { field => [qw(alice bob charlie)] } }
134              
135             Or
136              
137             { prefix => { user_agent => 'Go ' } }
138              
139             These conditions will wind up in the B<must> or B<must_not> section of the B<bool> query depending on the
140             state of the the invert flag.
141              
142             =head2 invert
143              
144             This is used by the bareword "not" to track whether the token invoked a flip from the B<must> to the B<must_not>
145             state. After each token is processed, if it didn't set this flag, the flag is reset.
146              
147             =head2 dangles
148              
149             This is used for bare words like "not", "or", and "and" to denote that these terms cannot dangle from the
150             beginning or end of the query_string. This allows the final pass of the query_string builder to strip these
151             words to prevent syntax errors.
152              
153             =for Pod::Coverage BUILDARGS
154              
155             =head1 AUTHOR
156              
157             Brad Lhotsky <brad@divisionbyzero.net>
158              
159             =head1 COPYRIGHT AND LICENSE
160              
161             This software is Copyright (c) 2024 by Brad Lhotsky.
162              
163             This is free software, licensed under:
164              
165             The (three-clause) BSD License
166              
167             =cut