File Coverage

blib/lib/App/ElasticSearch/Utilities/QueryString/AutoEscape.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 26 100.0


line stmt bran cond sub pod time code
1             package App::ElasticSearch::Utilities::QueryString::AutoEscape;
2             # ABSTRACT: Provides a prefix of '=' to use the term filter
3              
4 1     1   1587 use strict;
  1         3  
  1         31  
5 1     1   6 use warnings;
  1         2  
  1         45  
6              
7             our $VERSION = '8.5'; # VERSION
8              
9 1     1   7 use CLI::Helpers qw(:output);
  1         2  
  1         9  
10 1     1   181 use Const::Fast;
  1         2  
  1         18  
11 1     1   131 use namespace::autoclean;
  1         4  
  1         9  
12              
13 1     1   77 use Moo;
  1         2  
  1         7  
14             with 'App::ElasticSearch::Utilities::QueryString::Plugin';
15              
16 1     1   28 sub _build_priority { 5; }
17              
18              
19             sub handle_token {
20             my ($self,$token) = @_;
21              
22             debug(sprintf "%s - evaluating token '%s'", $self->name, $token);
23             if( $token =~ /^=(.*)$/ ) {
24             my ($f,$v) = split /:/, $1, 2;
25             return { condition => { term => { $f => $v } }};
26             }
27              
28             return;
29             }
30              
31             # Return True;
32             1;
33              
34             __END__
35              
36             =pod
37              
38             =head1 NAME
39              
40             App::ElasticSearch::Utilities::QueryString::AutoEscape - Provides a prefix of '=' to use the term filter
41              
42             =head1 VERSION
43              
44             version 8.5
45              
46             =head1 SYNOPSIS
47              
48             =head2 App::ElasticSearch::Utilities::QueryString::AutoEscape
49              
50             Provide an '=' prefix to a query string parameter to promote that parameter to a C<term> filter.
51              
52             This allows for exact matches of a field without worrying about escaping Lucene special character filters.
53              
54             E.g.:
55              
56             user_agent:"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"
57              
58             Is evaluated into a weird query that doesn't do what you want. However:
59              
60             =user_agent:"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"
61              
62             Is translated into:
63              
64             { term => { user_agent => "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1" } }
65              
66             Which provides an exact match to the term in the query.
67              
68             =for Pod::Coverage handle_token
69              
70             =head1 AUTHOR
71              
72             Brad Lhotsky <brad@divisionbyzero.net>
73              
74             =head1 COPYRIGHT AND LICENSE
75              
76             This software is Copyright (c) 2023 by Brad Lhotsky.
77              
78             This is free software, licensed under:
79              
80             The (three-clause) BSD License
81              
82             =cut