Data-SearchEngine-ElasticSearch
view release on metacpan or search on metacpan
lib/Data/SearchEngine/ElasticSearch.pm view on Meta::CPAN
my %doc = (
index => delete($data{index}),
type => delete($data{type}),
id => $item->id,
data => \%data
);
# Check for a version
if(exists($data{'_version'})) {
$doc{version} = delete($data{'_version'});
}
push(@docs, \%doc);
}
$self->_es->bulk_index(\@docs);
}
sub engine {
my ($self) = @_;
return $self->_es;
}
sub present {
my ($self, $item) = @_;
my $data = $item->values;
try {
my $result = $self->_es->get(
index => delete($data->{index}),
type => delete($data->{type}),
id => $item->id
);
} catch {
# ElasticSearch throws an exception if the document isn't there.
return 0;
}
return 1;
}
sub remove {
die("not implemented");
}
sub remove_by_id {
my ($self, $item) = @_;
my $data = $item->values;
$self->_es->delete(
index => $data->{index},
type => $data->{type},
id => $item->id
);
}
sub update {
my $self = shift;
$self->add(@_);
}
sub search {
my ($self, $query, $filter_combine) = @_;
unless(defined($filter_combine)) {
$filter_combine = 'and';
}
my $options;
if($query->has_query) {
die "Queries must have a type." unless $query->has_type;
$options->{query} = { $query->type => $query->query };
}
$options->{index} = $query->index;
if($query->has_debug) {
# Turn on explain
$options->{explain} = 1;
}
my @facet_cache = ();
if($query->has_filters) {
foreach my $filter ($query->filter_names) {
push(@facet_cache, $query->get_filter($filter));
}
$options->{filter}->{$filter_combine} = \@facet_cache;
}
if($query->has_facets) {
# Copy filters used in the overall query into each facet, thereby
# limiting the facets to only counting against the filtered bits.
# This is really to replicate my expecations and the way facets are
# usually used.
my %facets = %{ $query->facets };
$options->{facets} = $query->facets;
if($query->has_filters) {
foreach my $f (keys %facets) {
$facets{$f}->{facet_filter}->{$filter_combine} = \@facet_cache;
}
}
# Shlep the facets into the final query, even if we didn't do anything
# with the filters above.
$options->{facets} = \%facets;
}
if($query->has_order) {
$options->{sort} = $query->order;
}
if($query->has_fields) {
$options->{fields} = $query->fields;
( run in 1.586 second using v1.01-cache-2.11-cpan-e1769b4cff6 )