Skip to content

Commit

Permalink
only add type for older es
Browse files Browse the repository at this point in the history
  • Loading branch information
nics committed Mar 12, 2020
1 parent b5fe8fc commit c0fa8c6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for Catmandu-Store-ElasticSearch

{{$NEXT}}
- only add type for es 1x-2.x in reader methods

1.0201 2019-10-11 12:07:18 CEST
- fix scrolled search bug
Expand Down
22 changes: 13 additions & 9 deletions lib/Catmandu/Store/ElasticSearch/Bag.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ sub generator {
state $scroll = do {
my %args = (
index => $self->index,
type => $self->type,
size => $self->buffer_size, # TODO divide by number of shards
body => {query => {match_all => {}},},
);
if ($self->store->is_es_1_or_2) {
$args{search_type} = 'scan';
$args{type} = $self->type;
}
$self->store->es->scroll_helper(%args);
};
Expand All @@ -139,8 +139,12 @@ sub generator {

sub count {
my ($self) = @_;
$self->store->es->count(index => $self->index, type => $self->type,)
->{count};
my $store = $self->store;
my %args = (index => $self->index,);
if ($store->is_es_1_or_2) {
$args{type} = $self->type;
}
$store->es->count(%args)->{count};
}

sub get {
Expand Down Expand Up @@ -239,11 +243,11 @@ sub search {
$res = $self->store->es->scroll(%es_args);
}
else {
my %es_args = (
index => $self->index,
type => $self->type,
body => {%args, size => $limit,},
);
my %es_args
= (index => $self->index, body => {%args, size => $limit,},);
if ($self->store->is_es_1_or_2) {
$es_args{type} = $self->type;
}
if ($bag) {
$es_args{body}{fields} = [];
}
Expand Down Expand Up @@ -360,7 +364,7 @@ sub normalize_query {
sub normalize_sort {
my ($self, $sort) = @_;
return $sort if ref $sort;
return if !$sort;
return if !$sort;
decode_json($sort);
}

Expand Down
27 changes: 17 additions & 10 deletions lib/Catmandu/Store/ElasticSearch/Searcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ has sort => (is => 'ro');

sub _paging_generator {
my ($self) = @_;
my $es = $self->bag->store->es;
my $store = $self->bag->store;
my $es = $store->es;
my $id_key = $self->bag->id_key;
my $index = $self->bag->index;
my $type = $self->bag->type;
Expand All @@ -39,8 +40,11 @@ sub _paging_generator {
}
my $body = {query => $query, from => $start, size => $limit,};
$body->{sort} = $sort if defined $sort;
my $res
= $es->search(index => $index, type => $type, body => $body,);
my %args = (index => $index, body => $body,);
if ($store->is_es_1_or_2) {
$args{type} = $self->type;
}
my $res = $es->search(%args);

$hits = $res->{hits}{hits};
$start += $limit;
Expand Down Expand Up @@ -77,13 +81,15 @@ sub generator {
$body->{sort} = $self->sort if $self->sort;
my %args = (
index => $bag->index,
type => $bag->type,
size => $bag->buffer_size, # TODO divide by number of shards
body => $body,
);
if (!$self->sort && $store->is_es_1_or_2) {
$args{search_type} = 'scan';
}
if ($store->is_es_1_or_2) {
$args{type} = $self->type;
}
$store->es->scroll_helper(%args);
};

Expand Down Expand Up @@ -115,12 +121,13 @@ sub slice { # TODO constrain total?

sub count {
my ($self) = @_;
my $bag = $self->bag;
$bag->store->es->count(
index => $bag->index,
type => $bag->type,
body => {query => $self->query,},
)->{count};
my $bag = $self->bag;
my $store = $bag->store;
my %args = (index => $bag->index, body => {query => $self->query,},);
if ($store->is_es_1_or_2) {
$args{type} = $self->type;
}
$store->es->count(%args)->{count};
}

1;
Expand Down

0 comments on commit c0fa8c6

Please sign in to comment.