Permalink
Browse files

Handle empty queryb/fiterb/post_filterb clauses gracefully

Closes #20
  • Loading branch information...
1 parent e3a2a20 commit 5dff8aa47b4c04d4e72ac813979712e6a425e79e @clintongormley committed Nov 9, 2012
Showing with 42 additions and 10 deletions.
  1. +1 −1 lib/Elastic/Model/Role/Model.pm
  2. +22 −9 lib/Elastic/Model/View.pm
  3. +19 −0 t/40_view/01_view_attrs.t
@@ -3,7 +3,7 @@ package Elastic::Model::Role::Model;
use Moose::Role;
use Carp;
use Elastic::Model::Types qw(ES ES_UniqueKey);
-use ElasticSearch 0.59 ();
+use ElasticSearch 0.61 ();
use ElasticSearchX::UniqueKey 0.03 ();
use Class::Load qw(load_class);
use Moose::Util qw(does_role);
@@ -252,25 +252,31 @@ sub _build_search_builder { Elastic::Model::SearchBuilder->new }
#===================================
sub queryb {
#===================================
- my $self = shift;
- my @args = @_ > 1 ? {@_} : shift();
- $self->query( $self->search_builder->query(@args)->{query} );
+ my $self = shift;
+ my @args = @_ > 1 ? {@_} : shift();
+ my $query = $self->search_builder->query(@args)
+ or return $self->_clone_self;
+ $self->query( $query->{query} );
}
#===================================
sub filterb {
#===================================
- my $self = shift;
- my @args = @_ > 1 ? {@_} : shift();
- $self->filter( $self->search_builder->filter(@args)->{filter} );
+ my $self = shift;
+ my @args = @_ > 1 ? {@_} : shift();
+ my $filter = $self->search_builder->filter(@args)
+ or return $self->_clone_self;
+ $self->filter( $filter->{filter} );
}
#===================================
sub post_filterb {
#===================================
- my $self = shift;
- my @args = @_ > 1 ? {@_} : shift();
- $self->post_filter( $self->search_builder->filter(@args)->{filter} );
+ my $self = shift;
+ my @args = @_ > 1 ? {@_} : shift();
+ my $filter = $self->search_builder->filter(@args)
+ or return $self->_clone_self;
+ $self->post_filter( $filter->{filter} );
}
#===================================
@@ -340,6 +346,13 @@ sub _clone_args {
}
#===================================
+sub _clone_self {
+#===================================
+ my $self = shift;
+ return bless {%$self}, ref $self;
+}
+
+#===================================
sub _check_no_fields {
#===================================
my ( $self, $val ) = @_;
@@ -148,6 +148,25 @@ test_view(
}
);
+test_view(
+ 'Set-queryb-empty-list',
+ $view->queryb(),
+ { query => { match_all => {} } }
+);
+
+test_view(
+ 'Set-queryb-empty-hashref',
+ $view->queryb({}),
+ { query => { match_all => {} } }
+);
+
+test_view(
+ 'Set-queryb-empty-arrayref',
+ $view->queryb([]),
+ { query => { match_all => {} } }
+);
+
+
## filter ##
test_view(
'New-filter',

0 comments on commit 5dff8aa

Please sign in to comment.