ElasticSearchX-Model

 view release on metacpan or  search on metacpan

lib/ElasticSearchX/Model/Document/Set.pm  view on Meta::CPAN

#
# This file is part of ElasticSearchX-Model
#
# This software is Copyright (c) 2019 by Moritz Onken.
#
# This is free software, licensed under:
#
#   The (three-clause) BSD License
#
package ElasticSearchX::Model::Document::Set;
$ElasticSearchX::Model::Document::Set::VERSION = '2.0.1';
# ABSTRACT: Represents a query used for fetching a set of results
use Moose;
use MooseX::Attribute::Chained;
use MooseX::Attribute::ChainedClone;
use ElasticSearchX::Model::Scroll;
use ElasticSearchX::Model::Document::Types qw(:all);

has type => ( is => 'ro', required => 1 );
has index => ( is => 'ro', required => 1, handles => [qw(es model)] );

has query => (
    isa    => 'HashRef',
    is     => 'rw',
    traits => [qw(ChainedClone)]
);

has filter => (
    isa    => 'HashRef',
    is     => 'rw',
    traits => [qw(ChainedClone)]
);

has [qw(from size)] =>
    ( isa => 'Int', is => 'rw', traits => [qw(ChainedClone)] );

has [qw(fields sort)] => (
    isa    => 'ArrayRef',
    is     => 'rw',
    traits => [qw(ChainedClone)]
);

has source => (
    is      => 'rw',
    traits  => [qw(ChainedClone)],
    default => sub { \1 },
);

sub add_sort { push( @{ $_[0]->sort }, $_[1] ); return $_[0]; }

sub add_field { push( @{ $_[0]->fields }, $_[1] ); return $_[0]; }

has search_type =>
    ( isa => QueryType, is => 'rw', traits => [qw(ChainedClone)] );

sub query_type { shift->search_type(@_) }

has mixin => ( is => 'ro', isa => 'HashRef', traits => [qw(ChainedClone)] );

has inflate =>
    ( isa => 'Bool', default => 1, is => 'rw', traits => [qw(ChainedClone)] );

sub raw {
    shift->inflate(0);
}

has _refresh =>
    ( isa => 'Bool', default => 0, is => 'rw', traits => [qw(ChainedClone)] );

sub refresh {
    shift->_refresh(1);
}

sub _build_qs {
    my ( $self, $qs ) = @_;
    $qs ||= {};

    # we only want to set qs if they are not the default
    $qs->{refresh} = 1 if ( $self->_refresh );
    $qs->{search_type} = $self->search_type if $self->search_type;
    return $qs;
}

sub _build_query {
    my $self = shift;
    my $q = $self->query || { match_all => {} };
    if ( my $f = $self->filter ) {
        $q = { filtered => { query => $q, filter => $f } };
    }
    return {
        query   => $q,
        _source => $self->source,
        $self->size   ? ( size   => $self->size )   : (),
        $self->from   ? ( from   => $self->from )   : (),
        $self->fields ? ( fields => $self->fields ) : (),
        $self->sort   ? ( sort   => $self->sort )   : (),
        $self->mixin ? ( %{ $self->mixin } ) : (),
    };
}

sub put {
    my ( $self, $args, $qs ) = @_;
    my $doc = $self->new_document($args);
    $doc->put( $self->_build_qs($qs) );
    return $doc;
}

sub new_document {
    my ( $self, $args ) = @_;
    return $self->type->name->new( %$args, index => $self->index );
}

sub inflate_result {
    my ( $self, $res ) = @_;
    my ( $type, $index ) = ( $res->{_type}, $res->{_index} );
    $index = $index ? $self->model->index($index) : $self->index;
    $type  = $type  ? $index->get_type($type)     : $self->type;
    my $doc = $type->inflate_result( $index, $res );



( run in 0.669 second using v1.01-cache-2.11-cpan-39bf76dae61 )