| 1 | package App::ElasticSearch::Utilities::QueryString; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: CLI query string fixer | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use v5.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '9.1'; # VERSION | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use App::ElasticSearch::Utilities qw(:config); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use App::ElasticSearch::Utilities::Query; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use CLI::Helpers qw(:output); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Module::Pluggable::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Ref::Util qw(is_arrayref); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Types::Standard qw(ArrayRef Enum HashRef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | my %JOINING = map { $_ => 1 } qw( AND OR ); | |||||||||||||||||||||||||||
| 21 | 3 | my %TRAILING = map { $_ => 1 } qw( AND OR NOT ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has 'context' => ( | ||||||||||||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||||||||||
| 26 | isa => Enum[qw(query filter)], | ||||||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | default => sub { 'query' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 3 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | has search_path => ( | ||||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||
| 34 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 35 | 2 | ▶ | default => sub {[]}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 3 | ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | has default_join => ( | ||||||||||||||||||||||||||||
| 40 | is => 'rw', | ||||||||||||||||||||||||||||
| 41 | isa => Enum[qw(AND OR)], | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | default => sub { 'AND' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | has plugins => ( | |||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 49 | builder => '_build_plugins', | ||||||||||||||||||||||||||||
| 50 | lazy => 1, | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | has fields_meta => ( | ||||||||||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||||||||||
| 56 | isa => HashRef, | ||||||||||||||||||||||||||||
| 57 | 1 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 3 | ); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 14 | ▶ | sub expand_query_string { | ||||||||||||||||||||||||||
|
Subroutine
expand_query_string: called CC=24 SCAR=32.6
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 14 | my $query = App::ElasticSearch::Utilities::Query->new( | |||||||||||||||||||||||||||
| 65 | fields_meta => $self->fields_meta, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | 14 | my @processed = (); | |||||||||||||||||||||||||||
| 68 | 14 | TOKEN: foreach my $token (@_) { | |||||||||||||||||||||||||||
| 69 | 22 | foreach my $p (@{ $self->plugins }) { | |||||||||||||||||||||||||||
| 70 | 70 | my $res = $p->handle_token($token); | |||||||||||||||||||||||||||
| 71 | 70 | ▶ | if( defined $res ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 18 | ▶ | push @processed, is_arrayref($res) ? @{$res} : $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 18 | next TOKEN; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 4 | push @processed, { query_string => $token }; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 14 | debug({color=>"magenta"}, "Processed parts"); | |||||||||||||||||||||||||||
| 80 | 14 | debug_var({color=>"magenta"},\@processed); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 14 | ▶ | my $context = $self->context eq 'query' ? 'must' : 'filter'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 14 | my $invert=0; | |||||||||||||||||||||||||||
| 84 | 14 | my @dangling=(); | |||||||||||||||||||||||||||
| 85 | 14 | my @qs=(); | |||||||||||||||||||||||||||
| 86 | 14 | foreach my $part (@processed) { | |||||||||||||||||||||||||||
| 87 | 22 | ▶ | if( exists $part->{dangles} ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 88 | 6 | push @dangling, $part->{query_string}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif( exists $part->{query_string} ) { | ||||||||||||||||||||||||||||
| 91 | 4 | push @qs, @dangling, $part->{query_string}; | |||||||||||||||||||||||||||
| 92 | 4 | @dangling=(), | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif( exists $part->{condition} ) { | ||||||||||||||||||||||||||||
| 95 | 12 | ▶ | my $target = $invert ? 'must_not' : $context; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 12 | $query->add_bool( $target => $part->{condition} ); | |||||||||||||||||||||||||||
| 97 | 12 | @dangling=(); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | elsif( exists $part->{nested} ) { | ||||||||||||||||||||||||||||
| 100 | 0 | $query->nested($part->{nested}{query}); | |||||||||||||||||||||||||||
| 101 | 0 | $query->nested_path($part->{nested}{path}); | |||||||||||||||||||||||||||
| 102 | 0 | @dangling=(); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | # Carry over the Inversion for instance where we jump out of the QS | ||||||||||||||||||||||||||||
| 105 | 22 | ▶ | $invert = exists $part->{invert} && $part->{invert}; | ||||||||||||||||||||||||||
|
Condition100%3/3
exists $part->{'invert'} && $part->{'invert'}
MC/DC100%2/2
exists $part->{'invert'} && $part->{'invert'}
exists $part->{'invert'}
$part->{'invert'}
Truth table100%3/3
A && B
A= exists $part->{'invert'}B= $part->{'invert'}
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 14 | ▶ | if(@qs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | pop @qs while @qs && exists $TRAILING{$qs[-1]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@qs and exists $TRAILING{$qs[-1]}
MC/DC0%0/2
@qs and exists $TRAILING{$qs[-1]}
@qs
exists $TRAILING{$qs[-1]}
Truth table33%1/3
A and B
A= @qsB= exists $TRAILING{$qs[-1]}
| |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | shift @qs while @qs && exists $JOINING{$qs[0]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@qs and exists $JOINING{$qs[0]}
MC/DC50%1/2
@qs and exists $JOINING{$qs[0]}
@qs
exists $JOINING{$qs[0]}
Truth table66%2/3
A and B
A= @qsB= exists $JOINING{$qs[0]}
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Ensure there's a joining token, otherwise use our default | ||||||||||||||||||||||||||||
| 112 | 3 | ▶ | if( @qs > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 2 | my $prev_query = 0; | |||||||||||||||||||||||||||
| 114 | 2 | my @joined = (); | |||||||||||||||||||||||||||
| 115 | 2 | foreach my $part ( @qs ) { | |||||||||||||||||||||||||||
| 116 | 6 | ▶ | if( $prev_query ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | push @joined, $self->default_join() unless exists $JOINING{$part}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 6 | push @joined, $part; | |||||||||||||||||||||||||||
| 120 | # Here we include AND, NOT, OR | ||||||||||||||||||||||||||||
| 121 | 6 | ▶ | $prev_query = exists $TRAILING{$part} ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 2 | @qs = @joined; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 14 | ▶ | $query->add_bool($context => { query_string => { query => join(' ', @qs) } }) if @qs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 14 | return $query; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Builder Routines for QS Objects | ||||||||||||||||||||||||||||
| 132 | 1 | ▶ | sub _build_plugins { | ||||||||||||||||||||||||||
|
Subroutine
_build_plugins: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 133 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 1 | my $globals = es_globals('plugins'); | |||||||||||||||||||||||||||
| 135 | my $finder = Module::Pluggable::Object->new( | ||||||||||||||||||||||||||||
| 136 | 1 | search_path => ['App::ElasticSearch::Utilities::QueryString',@{ $self->search_path }], | |||||||||||||||||||||||||||
| 137 | except => [qw( | ||||||||||||||||||||||||||||
| 138 | App::ElasticSearch::Utilities::QueryString::AutoEscape | ||||||||||||||||||||||||||||
| 139 | App::ElasticSearch::Utilities::QueryString::Plugin | ||||||||||||||||||||||||||||
| 140 | )], | ||||||||||||||||||||||||||||
| 141 | instantiate => 'new', | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | 1 | my @plugins; | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | foreach my $p ( sort { $a->priority <=> $b->priority || $a->name cmp $b->name } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$a->priority <=> $b->priority || $a->name cmp $b->name
MC/DC0%0/2
$a->priority <=> $b->priority || $a->name cmp $b->name
$a->priority <=> $b->priority
$a->name cmp $b->name
Truth table66%2/3
A || B
A= $a->priority <=> $b->priorityB= $a->name cmp $b->name
| |||||||||||||||||||||||||||||
| 145 | $finder->plugins( | ||||||||||||||||||||||||||||
| 146 | fields_meta => $self->fields_meta, | ||||||||||||||||||||||||||||
| 147 | options => defined $globals ? $globals : {}, | ||||||||||||||||||||||||||||
| 148 | ) | ||||||||||||||||||||||||||||
| 149 | ) { | ||||||||||||||||||||||||||||
| 150 | 7 | debug(sprintf "Loaded %s with priority:%d", $p->name, $p->priority); | |||||||||||||||||||||||||||
| 151 | 7 | push @plugins, $p; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 1 | return \@plugins; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Return true | ||||||||||||||||||||||||||||
| 157 | 3 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||