| 1 | package Terse::Model::Es; | ||
| 2 | use 5.006; use strict; use warnings; | ||
| 3 | our $VERSION = '0.02'; | ||
| 4 | use base 'Terse::Model'; | ||
| 5 | |||
| 6 | sub columns { } | ||
| 7 | |||
| 8 | sub attributes { | ||
| 9 | $_[0]->{_attributes} ||= [ | ||
| 10 | qw/size page sorting group optional aggregation cols must_not fields/, | ||
| 11 | keys %{ $_[0]->columns() } | ||
| 12 | ]; | ||
| 13 | return $_[0]->{_attributes}; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub index { } | ||
| 17 | |||
| 18 | sub has_column { | ||
| 19 | my $columns = $_[0]->columns(); | ||
| 20 | return $columns->{$_[1]}; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub count { | ||
| 24 | my ($self, $t) = @_; | ||
| 25 | my $res = $t->plugin('es')->count($self->filter('count')); | ||
| 26 | return $res->{count}; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub grouped { | ||
| 30 | my ($self, $t, @g) = @_; | ||
| 31 | $self->group = \@g; | ||
| 32 | return $t->plugin('es')->search($self->filter('grouped')); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub search { | ||
| 36 | my ($self, $t, @g) = @_; | ||
| 37 | return $t->plugin('es')->search($self->filter('search')); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub filter { | ||
| 41 | my ($self, $type) = @_; | ||
| 42 | |||
| 43 | my $filter = {}; | ||
| 44 | my $optional = $self->optional; | ||
| 45 | my @should = (); | ||
| 46 | my %cols = %{ $self->columns() }; | ||
| 47 | if ($optional && scalar keys %{$optional}) { | ||
| 48 | for my $o (sort keys %{$optional}) { | ||
| 49 | if (ref $optional->{$o} && ref $optional->{$o} eq 'ARRAY') { | ||
| 50 | # itterate the values | ||
| 51 | for (@{$optional->{$o}}) { | ||
| 52 | # and push into @should a phrase prefix query | ||
| 53 | push @should, { prefix => { $o => $_ } }; | ||
| 54 | } | ||
| 55 | } else { | ||
| 56 | if ($cols{$o}->{alias}) { | ||
| 57 | # if so push wth the correct aliased column | ||
| 58 | push @should, { | ||
| 59 | wildcard => { | ||
| 60 | $cols{$o}->{alias} => '*' . quotemeta($optional->{$o}) . '*' | ||
| 61 | } | ||
| 62 | }; | ||
| 63 | } else { | ||
| 64 | # else just push the wildcard query | ||
| 65 | push @should, { wildcard => { $o => '*' . quotemeta($optional->{$o}) . '*' } }; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | my (@must, @mustnot); | ||
| 72 | for (keys %cols) { | ||
| 73 | my $val = $self->can($_) && $self->$_; | ||
| 74 | if (defined $val) { | ||
| 75 | my %spec = ( | ||
| 76 | SCALAR => 'term', | ||
| 77 | ARRAY => 'terms', | ||
| 78 | %{ $cols{$_} } | ||
| 79 | ); | ||
| 80 | my $ref = ref $val || 'SCALAR'; | ||
| 81 | my %must_not; | ||
| 82 | %must_not = map { (ref $_ ? ( $_->[0] => $_->[1] ) : ($_ => 1)) } @{ $self->must_not } | ||
| 83 | if $self->must_not; | ||
| 84 | if ($must_not{$_}) { | ||
| 85 | if (ref $must_not{$_}) { | ||
| 86 | push @mustnot, $must_not{$_}; | ||
| 87 | } else { | ||
| 88 | my $aa = $spec{alias} ? $spec{alias} : $_; | ||
| 89 | push @mustnot, { | ||
| 90 | ($spec{$ref} ? ($spec{$ref} => { | ||
| 91 | $aa => $val | ||
| 92 | }) : ($aa => $val)) | ||
| 93 | }; | ||
| 94 | } | ||
| 95 | } else { | ||
| 96 | if (((ref $val) || "") eq 'HASH' && $val->{not_exists}) { | ||
| 97 | push @must, { | ||
| 98 | "bool" => { | ||
| 99 | "should"=> [ | ||
| 100 | (($val->{val} && scalar @{$val->{val}}) ? ( | ||
| 101 | { | ||
| 102 | "bool"=> { | ||
| 103 | "must"=> [ | ||
| 104 | { | ||
| 105 | "terms" => { | ||
| 106 | $_ => $val->{val} | ||
| 107 | } | ||
| 108 | } | ||
| 109 | ] | ||
| 110 | } | ||
| 111 | }) :()), | ||
| 112 | { | ||
| 113 | "bool"=> { | ||
| 114 | "must_not"=> [ | ||
| 115 | { | ||
| 116 | "exists"=> { | ||
| 117 | "field"=> $_ | ||
| 118 | } | ||
| 119 | } | ||
| 120 | ] | ||
| 121 | } | ||
| 122 | } | ||
| 123 | ] | ||
| 124 | } | ||
| 125 | }; | ||
| 126 | } else { | ||
| 127 | my $aa = $spec{alias} ? $spec{alias} : $_; | ||
| 128 | push @must, { | ||
| 129 | ($spec{$ref} ? ($spec{$ref} => { | ||
| 130 | $aa => $val | ||
| 131 | }) : ($aa => $val)) | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | if (scalar @should) { | ||
| 139 | push @must, {bool => {should => \@should}}; | ||
| 140 | } | ||
| 141 | |||
| 142 | if (scalar @must || scalar @mustnot) { | ||
| 143 | $filter->{query} = { | ||
| 144 | bool => { | ||
| 145 | (scalar @must ? (must => \@must) : ()), | ||
| 146 | (scalar @mustnot ? (must_not => \@mustnot) : ()), | ||
| 147 | } | ||
| 148 | }; | ||
| 149 | } | ||
| 150 | |||
| 151 | my $aggs = $self->aggs; | ||
| 152 | my $group = $filter; | ||
| 153 | if ($type eq 'grouped') { | ||
| 154 | my ($groups) = $self->group; | ||
| 155 | if ($groups && scalar @{$groups}) { | ||
| 156 | for my $g (@{ $groups }) { | ||
| 157 | $group = ($group->{aggs} ||= {}); | ||
| 158 | $group = ($group->{'group_by_' . $g} = { | ||
| 159 | terms => { field => $g, size => 10000 }, | ||
| 160 | ($aggs && $aggs->{$g} ? ( aggs => $aggs->{$g} ) : ()) | ||
| 161 | }); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | }} | ||
| 165 | |||
| 166 | if ($type =~ m/search|grouped/) { | ||
| 167 | if (defined $self->size) { | ||
| 168 | $filter->{size} = $self->size; | ||
| 169 | $filter->{from} = (($self->page * $self->size) - $self->size) if defined $self->page; | ||
| 170 | } | ||
| 171 | $group->{aggs} = $aggs->{default} if (!$group->{aggs} && $aggs && $aggs->{default} && keys %{$aggs->{default}}); | ||
| 172 | |||
| 173 | if (defined $self->fields) { | ||
| 174 | $filter->{script_fields} = $self->fields; | ||
| 175 | $filter->{_source} = \1; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | if ($type eq 'search') { | ||
| 180 | my $sort = $self->sort; | ||
| 181 | if (scalar @{$sort}) { | ||
| 182 | $filter->{sort} = $self->sort; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | return { index => $self->index, body => $filter }; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub clone { | ||
| 190 | my ($self) = shift; | ||
| 191 | my $clone = ref($self)->new( | ||
| 192 | map +( $_ => $self->$_ ), @{ $self->attributes } | ||
| 193 | ); | ||
| 194 | return $clone; | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | __END__ |