App-ElasticSearch-Utilities

 view release on metacpan or  search on metacpan

lib/App/ElasticSearch/Utilities/QueryString/Ranges.pm  view on Meta::CPAN

package App::ElasticSearch::Utilities::QueryString::Ranges;
# ABSTRACT: Implement parsing comparison operators to Equivalent Lucene syntax

use v5.16;
use warnings;

our $VERSION = '8.8'; # VERSION

use CLI::Helpers qw(:output);
use namespace::autoclean;

use Moo;
with 'App::ElasticSearch::Utilities::QueryString::Plugin';

sub _build_priority { 20; }

my %Operators = (
    '<'  => { side => 'left',  op => 'lt' },
    '<=' => { side => 'left',  op => 'lte' },
    '>'  => { side => 'right', op => 'gt' },
    '>=' => { side => 'right', op => 'gte' },
);
my $op_match = join('|', map { quotemeta } sort { length $b <=> length $a } keys %Operators);


sub handle_token {
    my ($self,$token) = @_;

    debug(sprintf "%s - evaluating token '%s'", $self->name, $token);
    my ($k,$v) = split /:/, $token, 2;

    return unless $v;

    my %sides = ();
    my %range = ();
    foreach my $range (split /\,/, $v) {
        if( my($symbol,$value) = ( $range =~ /^($op_match)(.+)$/ ) ) {
            my $side = $Operators{$symbol}->{side};
            # Invalid query if two left or right operators
            die "attempted to set more than one $side-side operator in Range: $token"
                if $sides{$side};
            $sides{$side} = 1;
            $range{$Operators{$symbol}->{op}} = $value;
        }
    }

    return unless scalar keys %range;

    return { condition => { range => { $k => \%range } } };
}

# Return True;
1;

__END__

=pod

=head1 NAME

App::ElasticSearch::Utilities::QueryString::Ranges - Implement parsing comparison operators to Equivalent Lucene syntax

=head1 VERSION

version 8.8

=head1 SYNOPSIS

=head2 App::ElasticSearch::Utilities::QueryString::Ranges



( run in 0.755 second using v1.01-cache-2.11-cpan-39bf76dae61 )