BenchmarkAnything-Storage-Search-Elasticsearch

 view release on metacpan or  search on metacpan

lib/BenchmarkAnything/Storage/Search/Elasticsearch.pm  view on Meta::CPAN

    my $default_max_size = 10_000;

    my $ar_ba_select   = $hr_ba_query->{select};   # select:done; aggregators!
    my $ar_ba_where    = $hr_ba_query->{where};    # done
    my $i_ba_limit     = $hr_ba_query->{limit};    # done
    my $i_ba_offset    = $hr_ba_query->{offset};   # done
    my $ar_ba_order_by = $hr_ba_query->{order_by}; # done

    my @must_ranges;
    my @must_matches;
    my @must_not_matches;
    my @should_matches;
    my %source_fields = (
        NAME      => 1,
        UNIT      => 1,
        VALUE     => 1,
        VALUE_ID  => 1,
        CREATED   => 1,
    );

    # select fields

lib/BenchmarkAnything/Storage/Search/Elasticsearch.pm  view on Meta::CPAN

     (
      '<'  => 'lt',
      '>'  => 'gt',
      '<=' => 'lte',
      '>=' => 'gte',
     );
    my %match_operator =
     (
      '='  => 1,
     );
    my %not_match_operator =
     (
      '!=' => 1,
     );
    my %empty_match_operator =
     (
      'is_empty' => 1,
     );
    my %wildcard_match_operator =
     (
      'like' => 1,
     );
    my %wildcard_not_match_operator =
     (
      'not like' => 1, # deprecated
      'not_like' => 1,
     );
    foreach my $w (@{ $ar_ba_where || [] })
    {
        my $op = $w->[0];       # operator
        my $k  = $w->[1];       # key
        my @v  = map { encode_utf8($_) } @$w[2..@$w-1]; # value(s)

        my $es_op;
        if ($es_op = $range_operator{$op})
        {
            push @must_ranges,        { range => { $k => { $es_op => $v[0] } } };
        }
        elsif ($es_op = $match_operator{$op})
        {
            if (@v > 1) {
                push @should_matches, { match => { $k => $_ } } foreach @v;
            } else {
                push @must_matches,   { match => { $k => $v[0] } };
            }
        }
        elsif ($es_op = $not_match_operator{$op})
        {
            push @must_not_matches,   { match => { $k => $_ } } foreach @v;
        }
        elsif ($es_op = $wildcard_match_operator{$op})
        {
            my $es_v = $v[0];
            $es_v =~ s/%/*/g;
            push @must_matches,       { wildcard => { $k => $es_v } };
        }
        elsif ($es_op = $empty_match_operator{$op})
        {
            if (@v and $v[0] == 0) {
                # field is NOT EMPTY: [ "is_empty", "some_field_name", 0 ]
                push @must_matches, { wildcard => { $k => '*' } };

            } elsif (@v and $v[0] == 2) {
                # field is EMPTY but exists (ie. not undefined): [ "is_empty", "some_field_name", 2 ]
                push @must_not_matches, { wildcard => { $k => '*' } };
                push @must_matches,     { exists   => { field => $k } };

            } elsif (not @v or $v[0] == 1) {
                # field is EMPTY or UNDEFINED/NULL: [ "is_empty", "some_field_name", 1 ] or [ "is_empty", "some_field_name" ]
                push @must_not_matches, { wildcard => { $k => '*' } };

            } else {
                # we might invent other semantics so we better warn about
                # what could once become meaningful.
                warn "unclear 'is_empty' condition (".$v[0]."). Interpreting as 'is_empty' condition (1).";
                push @must_not_matches, { wildcard => { $k => '*' } };
            }
        }
        elsif ($es_op = $wildcard_not_match_operator{$op})
        {
            my $es_v = $v[0];
            $es_v =~ s/%/*/g;
            push @must_not_matches,   { wildcard => { $k => $es_v } };
        }
        else
        {
            warn "_get_elasticsearch_query: Unsupported operator: $op\n";
            return;
        }
    }

    $hr_es_query = { query => { bool => {
                                         (@must_matches||@must_ranges ? (must     => [ @must_ranges, @must_matches ]) : ()),
                                         (@must_not_matches           ? (must_not => [ @must_not_matches ]) : ()),
                                         (@should_matches             ? (should   => [ @should_matches ],minimum_should_match => 1) : ()),
                                        },
                              },
                     %from,
                     %size,
                     %sort,
                     %source,
                   };

    return $hr_es_query;



( run in 0.234 second using v1.01-cache-2.11-cpan-4d4bc49f3ae )