<commit_before>package Elastijk::oo;
use strict;
use warnings;
use Elastijk;

sub new {
    my $class = shift;
    return bless { host => "localhost", port => "9200", @_ }, $class;
}

{
    no warnings 'redefine';
    *Elastijk::new = *Elastijk::oo::new;
};

sub request {
    my ($self, %args) = @_;
    $args{$_} ||= $self->{$_} for grep { exists $self->{$_} } qw(host port index type);
    return Elastijk::request(\%args);
}

sub request_raw {
    my ($self, %args) = @_;
    $args{$_} ||= $self->{$_} for grep { exists $self->{$_} } qw(host port index type);
    return Elastijk::request_raw(\%args);
}

sub index {
    my ($self, $type, $doc) = @_;

    if (ref($doc) eq 'ARRAY') {
        my $body = "";
        for my $d (@$doc) {
            if (ref($d) eq 'ARRAY') {
                $body .= $Elastijk::JSON->encode($d->[0]) . "\n"
                    . $Elastijk::JSON->encode($d->[1]) . "\n";
            }
            elsif (ref($d) eq 'HASH') {
                $body .= '{"index":{}}' . "\n"
                    . $Elastijk::JSON->encode($d) . "\n";
            }
        }

        my ($status, $res) = $self->request_raw(
            method => "POST",
            type => $type,
            command => "_bulk",
            body => $body,
        );
        $res = $Elastijk::JSON->decode($res);
        return $res;
    } elsif (ref($doc) eq 'HASH') {
        my ($status, $res) = $self->request(
            method => "POST",
            type  => $type,
            body => $doc,
        );
        return $res;
    }

    return undef;
}

sub get {
    my ($self, %spec) = @_;
    my ($status, $res) = $self->request(
        exists($spec{index}) ? ( index => $spec{index} ) : (),
        exists($spec{type})  ? ( type  => $spec{type}  ) : (),
        command => $spec{id},
    );
    return $res;
}

sub exists {
    my ($self, %spec) = @_;

    if ( !ref($spec{index}) ) {
        my ($status, undef) = $self->request(
            method => "HEAD",
            exists($spec{index}) ? ( index => $spec{index} ) : (),
            exists($spec{type})  ? ( type  => $spec{type}  ) : (),
        );

        return $status eq '200';
    }

    return undef;
}

sub create {
    my ($self, %spec) = @_;
    my $res = {};
    for my $index_name ( keys %{ $spec{index} } ) {
        my ($status, $body) = $self->request(
            index => $index_name,
            method => "PUT",
            body => $spec{index}{$index_name}
        );
        $res->{$index_name} = { status => $status, body => $body }
    }

    return $res;
}

sub delete {
    my ($self, %spec) = @_;
    my $res = {};
    my $index_name = $spec{index};
    my ($status, $body) = $self->request(
        index => $index_name,
        method => "DELETE"
    );
    $res->{$index_name} = { status => $status, body => $body };
    return $res;
}

sub search {
    my $self = shift;
    my %args = @_;
    my $search_type = delete $args{search_type};
    my ($status, $body) = $self->request(
        command => "_search",
        $search_type ? (
            uri_param => { search_type => $search_type }
        ) : (),
        body => \%args,
    );

    return {
        status => $status,
        body => $body,
    }
}


sub uri_search {
    my ($self, %args) = @_;
    my ($status, $body) = $self->request(
        command => "_search",
        uri_param => \%args,
    );
    return {
        status => $status,
        body => $body,
    }
}


1;
<commit_msg>fix the case when 2nd object in the same process is blessed to the wrong name.<commit_after>package Elastijk::oo;
use strict;
use warnings;
use Elastijk;

sub new {
    my $class = shift;
    return bless { host => "localhost", port => "9200", @_ }, $class;
}

{
    no warnings 'redefine';
    *Elastijk::new = sub { shift; Elastijk::oo->new(@_) };
};

sub request {
    my ($self, %args) = @_;
    $args{$_} ||= $self->{$_} for grep { exists $self->{$_} } qw(host port index type);
    return Elastijk::request(\%args);
}

sub request_raw {
    my ($self, %args) = @_;
    $args{$_} ||= $self->{$_} for grep { exists $self->{$_} } qw(host port index type);
    return Elastijk::request_raw(\%args);
}

sub index {
    my $self = shift;
    my $doc  = pop;
    my $type = @_ ? $_[0] : $self->{type};

    if (ref($doc) eq 'ARRAY') {
        my $body = "";
        for my $d (@$doc) {
            if (ref($d) eq 'ARRAY') {
                $body .= $Elastijk::JSON->encode($d->[0]) . "\n"
                    . $Elastijk::JSON->encode($d->[1]) . "\n";
            }
            elsif (ref($d) eq 'HASH') {
                $body .= '{"index":{}}' . "\n"
                    . $Elastijk::JSON->encode($d) . "\n";
            }
        }

        my ($status, $res) = $self->request_raw(
            method => "POST",
            defined($type) ? ( type => $type ) : (),
            command => "_bulk",
            body => $body,
        );
        $res = $Elastijk::JSON->decode($res);
        return $res;
    } elsif (ref($doc) eq 'HASH') {
        my ($status, $res) = $self->request(
            method => "POST",
            defined($type) ? ( type => $type ) : (),
            body => $doc,
        );
        return $res;
    }

    return undef;
}

sub get {
    my ($self, %spec) = @_;
    my ($status, $res) = $self->request(
        exists($spec{index}) ? ( index => $spec{index} ) : (),
        exists($spec{type})  ? ( type  => $spec{type}  ) : (),
        command => $spec{id},
    );
    return $res;
}

sub exists {
    my ($self, %spec) = @_;

    if ( !ref($spec{index}) ) {
        my ($status, undef) = $self->request(
            method => "HEAD",
            exists($spec{index}) ? ( index => $spec{index} ) : (),
            exists($spec{type})  ? ( type  => $spec{type}  ) : (),
        );

        return $status eq '200';
    }

    return undef;
}

sub create {
    my ($self, %spec) = @_;
    my $res = {};
    for my $index_name ( keys %{ $spec{index} } ) {
        my ($status, $body) = $self->request(
            index => $index_name,
            method => "PUT",
            body => $spec{index}{$index_name}
        );
        $res->{$index_name} = { status => $status, body => $body }
    }

    return $res;
}

sub delete {
    my ($self, %spec) = @_;
    my $res = {};
    my $index_name = $spec{index};
    my ($status, $body) = $self->request(
        index => $index_name,
        method => "DELETE"
    );
    $res->{$index_name} = { status => $status, body => $body };
    return $res;
}

sub search {
    my $self = shift;
    my %args = @_;
    my $search_type = delete $args{search_type};
    my ($status, $body) = $self->request(
        command => "_search",
        $search_type ? (
            uri_param => { search_type => $search_type }
        ) : (),
        body => \%args,
    );

    return {
        status => $status,
        body => $body,
    }
}


sub uri_search {
    my ($self, %args) = @_;
    my ($status, $body) = $self->request(
        command => "_search",
        uri_param => \%args,
    );
    return {
        status => $status,
        body => $body,
    }
}


1;

{'fraction_non_alphanumeric': 0.20324629498941427, 'fraction_numerical': 0.00310515172900494, 'mean_word_length': 1.8041155520379897, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1024270', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2414, 'n_words': 569}