Skip to content

Commit

Permalink
Fixed the interplay between fields, include/exclude_partial and _source
Browse files Browse the repository at this point in the history
  • Loading branch information
clintongormley committed Aug 16, 2014
1 parent 68a9526 commit 784e2fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
5 changes: 2 additions & 3 deletions lib/Elastic/Model/Role/Results.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ no Moose;
#===================================
sub _build_is_partial {
#===================================
my $self = shift;
my $source = $self->search->{_source};
return !!( !$source || ref $source );
my $self = shift;
return exists $self->search->{_source};
}

#===================================
Expand Down
8 changes: 5 additions & 3 deletions lib/Elastic/Model/View.pm
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ sub _build_search {
if $self->_has_include_paths;
$source->{exclude} = $self->exclude_paths
if $self->_has_exclude_paths;
$source ||= @$fields ? 0 : 1;

$fields = ['_source'] unless $source || @$fields;

my %args = _strip_undef(
( map { $_ => $self->$_ }
Expand All @@ -475,9 +476,10 @@ sub _build_search {
indices_boost => $self->index_boosts,
@_,
version => 1,
fields => [ '_parent', '_routing', @$fields ],
_source => $source,
fields => [ '_parent', '_routing', @$fields ]
);
$args{_source} = $source
if defined $source;
return \%args;
}

Expand Down
68 changes: 22 additions & 46 deletions t/40_view/01_view_attrs.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ test_view(
{ type => [ 'foo', 'bar' ] }
);

test_view(
'New-type-str',
$domain->view( type => 'foo' ),
{ type => ['foo'] }
);
test_view( 'New-type-str', $domain->view( type => 'foo' ),
{ type => ['foo'] } );

test_view(
'Set-type-array',
Expand Down Expand Up @@ -281,10 +278,8 @@ test_view(
'New-post_filterb-array',
$domain->view( post_filterb => [ 'foo', 'bar', 'foo', 'baz' ] ),
{ post_filter => {
or => [
{ term => { foo => "bar" } },
{ term => { foo => "baz" } },
],
or =>
[ { term => { foo => "bar" } }, { term => { foo => "baz" } }, ],
}
}
);
Expand All @@ -311,10 +306,8 @@ test_view(
'Set-post_filterb-array',
$view->post_filterb( [ foo => 'bar', foo => 'baz' ] ),
{ post_filter => {
or => [
{ term => { foo => "bar" } },
{ term => { foo => "baz" } },
],
or =>
[ { term => { foo => "bar" } }, { term => { foo => "baz" } }, ],
}
}
);
Expand Down Expand Up @@ -372,31 +365,31 @@ test_view(
test_view(
'New-fields-array',
$domain->view( fields => [ 'foo', 'bar' ] ),
{ fields => [ "_parent", "_routing", 'foo', 'bar' ], _source => 0 }
{ fields => [ "_parent", "_routing", 'foo', 'bar' ] }
);

test_view(
'New-fields-str',
$domain->view( fields => 'foo' ),
{ fields => [ "_parent", "_routing", 'foo' ], _source => 0 }
{ fields => [ "_parent", "_routing", 'foo' ] }
);

test_view(
'Set-fields-array',
$view->fields( [ 'foo', 'bar' ] ),
{ fields => [ "_parent", "_routing", 'foo', 'bar' ], _source => 0 }
{ fields => [ "_parent", "_routing", 'foo', 'bar' ] }
);

test_view(
'Set-fields-list',
$view->fields( 'foo', 'baz' ),
{ fields => [ "_parent", "_routing", 'foo', 'baz' ], _source => 0 }
{ fields => [ "_parent", "_routing", 'foo', 'baz' ] }
);

test_view(
'Set-fields-str',
$view->fields('foo'),
{ fields => [ "_parent", "_routing", 'foo' ], _source => 0 }
{ fields => [ "_parent", "_routing", 'foo' ] }
);

## from ##
Expand All @@ -416,11 +409,8 @@ test_view(
{ sort => [ 'foo', { bar => 'asc' } ] }
);

test_view(
'New-sort-str',
$domain->view( sort => 'foo' ),
{ sort => ['foo'] }
);
test_view( 'New-sort-str', $domain->view( sort => 'foo' ),
{ sort => ['foo'] } );

test_view(
'New-sort-hash',
Expand All @@ -447,9 +437,7 @@ test_view(
highlighting => { x => 'y' },
highlight => { 'foo' => {}, 'bar' => { x => 'z' } }
),
{ highlight =>
{ x => 'y', fields => { foo => {}, bar => { x => 'z' } } }
}
{ highlight => { x => 'y', fields => { foo => {}, bar => { x => 'z' } } } }
);

test_view(
Expand All @@ -458,9 +446,7 @@ test_view(
highlighting => { x => 'y' },
highlight => [ 'foo', 'bar' => { x => 'z' } ]
),
{ highlight =>
{ x => 'y', fields => { foo => {}, bar => { x => 'z' } } }
}
{ highlight => { x => 'y', fields => { foo => {}, bar => { x => 'z' } } } }
);

test_view(
Expand All @@ -476,17 +462,13 @@ test_view(
'Set-highlight-hash-hash',
$view->highlighting( { x => 'y' } )
->highlight( { foo => {}, bar => { x => 'z' } } ),
{ highlight =>
{ x => 'y', fields => { foo => {}, bar => { x => 'z' } } }
}
{ highlight => { x => 'y', fields => { foo => {}, bar => { x => 'z' } } } }
);

test_view(
'Set-highlight-list-list',
$view->highlighting( p => 'q' )->highlight( 'foo', bar => { p => 'r' } ),
{ highlight =>
{ p => 'q', fields => { foo => {}, bar => { p => 'r' } } }
}
{ highlight => { p => 'q', fields => { foo => {}, bar => { p => 'r' } } } }
);

test_view(
Expand Down Expand Up @@ -563,11 +545,8 @@ test_view(
{ preference => 'foo' }
);

test_view(
'Set-preference',
$view->preference('bar'),
{ preference => 'bar' }
);
test_view( 'Set-preference', $view->preference('bar'),
{ preference => 'bar' } );

## routing ##
test_view(
Expand Down Expand Up @@ -647,8 +626,7 @@ test_view(

test_view(
'Include and exclude paths',
$view->include_paths( 'foo.*', 'fuz.*' )
->exclude_paths( 'bar.*', 'baz.*' ),
$view->include_paths( 'foo.*', 'fuz.*' )->exclude_paths( 'bar.*', 'baz.*' ),
{ _source => {
include => [ 'foo.*', 'fuz.*' ],
exclude => [ 'bar.*', 'baz.*' ]
Expand Down Expand Up @@ -705,8 +683,7 @@ test_view(
{ track_scores => 1 }
);

test_view( 'Set-track_scores', $view->track_scores(1),
{ track_scores => 1 } );
test_view( 'Set-track_scores', $view->track_scores(1), { track_scores => 1 } );

## consistency ##
test_view(
Expand Down Expand Up @@ -739,14 +716,13 @@ sub test_view {
#===================================
my ( $name, $view, $results ) = @_;
$results = {
fields => [ "_parent", "_routing" ],
fields => [ "_parent", "_routing", "_source" ],
from => 0,
index => ["myapp"],
query => { match_all => {} },
size => 10,
type => [],
version => 1,
_source => 1,
%$results
};

Expand Down

0 comments on commit 784e2fe

Please sign in to comment.