File Coverage

blib/lib/App/ElasticSearch/Utilities/QueryString/Text.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 25 25 100.0


line stmt bran cond sub pod time code
1             package App::ElasticSearch::Utilities::QueryString::Text;
2             # ABSTRACT: Provides a better interface for text and keyword queries
3              
4 2     2   302493 use v5.16;
  2         9  
5 2     2   11 use warnings;
  2         4  
  2         233  
6              
7             our $VERSION = '8.8'; # VERSION
8              
9 2     2   435 use CLI::Helpers qw(:output);
  2         111661  
  2         23  
10 2     2   1186 use Const::Fast;
  2         3578  
  2         17  
11 2     2   787 use namespace::autoclean;
  2         16374  
  2         16  
12              
13 2     2   597 use Moo;
  2         6318  
  2         16  
14             with 'App::ElasticSearch::Utilities::QueryString::Plugin';
15              
16 1     1   70 sub _build_priority { 4; }
17              
18              
19             sub handle_token {
20             my ($self,$token) = @_;
21              
22             my $meta = $self->fields_meta;
23              
24             debug(sprintf "%s - evaluating token '%s'", $self->name, $token);
25             if ( $token =~ /[^:]+:/ ) {
26             my ($f,$v) = split /:/, $token, 2;
27              
28             my $matcher = '';
29              
30             # Grab the prefix symbol
31             $f =~ s/^(?<op>[^a-zA-Z])//;
32             if( $+{op} ) {
33             $matcher = $+{op} eq '*' ? 'wildcard'
34             : $+{op} eq '=' ? 'term'
35             : $+{op} eq '/' ? 'regexp'
36             : $+{op} eq '~' ? 'fuzzy'
37             : $+{op} eq '+' ? 'match_phrase'
38             : '';
39             }
40              
41             # Check metadata for text type
42             if ( exists $meta->{$f}
43             && exists $meta->{$f}{type}
44             && $meta->{$f}{type} eq 'text'
45             ) {
46             # We can't use term filters on text fields
47             $matcher = 'match' if !$matcher or $matcher eq 'term';
48             }
49              
50             if( $matcher ) {
51             return { condition => { $matcher => { $f => $v } } };
52             }
53             }
54              
55             return;
56             }
57              
58             # Return True;
59             1;
60              
61             __END__
62              
63             =pod
64              
65             =head1 NAME
66              
67             App::ElasticSearch::Utilities::QueryString::Text - Provides a better interface for text and keyword queries
68              
69             =head1 VERSION
70              
71             version 8.8
72              
73             =head1 SYNOPSIS
74              
75             =head2 App::ElasticSearch::Utilities::QueryString::Text
76              
77             Provides field prefixes to manipulate the text search capabilities.
78              
79             =head3 Terms Query via '='
80              
81             Provide an '=' prefix to a query string parameter to promote that parameter to a C<term> filter.
82              
83             This allows for exact matches of a field without worrying about escaping Lucene special character filters.
84              
85             E.g.:
86              
87             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"
88              
89             Is evaluated into a weird query that doesn't do what you want. However:
90              
91             =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"
92              
93             Is translated into:
94              
95             { 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" } }
96              
97             =head3 Wildcard Query via '*'
98              
99             Provide an '*' prefix to a query string parameter to promote that parameter to a C<wildcard> filter.
100              
101             This uses the wild card match for text fields to making matching more intuitive.
102              
103             E.g.:
104              
105             *user_agent:"Mozilla*"
106              
107             Is translated into:
108              
109             { wildcard => { user_agent => "Mozilla* } }
110              
111             =head3 Regexp Query via '/'
112              
113             Provide an '/' prefix to a query string parameter to promote that parameter to a C<regexp> filter.
114              
115             If you want to use regexp matching for finding data, you can use:
116              
117             /message:'\\bden(ial|ied|y)'
118              
119             Is translated into:
120              
121             { regexp => { message => "\\bden(ial|ied|y)" } }
122              
123             =head3 Fuzzy Matching via '~'
124              
125             Provide an '~' prefix to a query string parameter to promote that parameter to a C<fuzzy> filter.
126              
127             ~message:deny
128              
129             Is translated into:
130              
131             { fuzzy => { message => "deny" } }
132              
133             =head3 Phrase Matching via '+'
134              
135             Provide an '+' prefix to a query string parameter to promote that parameter to a C<match_phrase> filter.
136              
137             +message:"login denied"
138              
139             Is translated into:
140              
141             { match_phrase => { message => "login denied" } }
142              
143             =head3 Automatic Match Queries for Text Fields
144              
145             If the field meta data is provided and the field is a C<text> type, the query
146             will automatically be mapped to a C<match> query.
147              
148             # message field is text
149             message:"foo"
150              
151             Is translated into:
152              
153             { match => { message => "foo" } }
154              
155             =for Pod::Coverage handle_token
156              
157             =head1 AUTHOR
158              
159             Brad Lhotsky <brad@divisionbyzero.net>
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is Copyright (c) 2024 by Brad Lhotsky.
164              
165             This is free software, licensed under:
166              
167             The (three-clause) BSD License
168              
169             =cut