package Search::Elasticsearch::Client::1_0::Direct; use Moo; with 'Search::Elasticsearch::Client::1_0::Role::API'; with 'Search::Elasticsearch::Role::Client::Direct'; our $VERSION='5.00'; use Search::Elasticsearch 5.00 (); use Search::Elasticsearch::Util qw(parse_params is_compat); use namespace::clean; sub _namespace {__PACKAGE__} has 'cluster' => ( is => 'lazy', init_arg => undef ); has 'nodes' => ( is => 'lazy', init_arg => undef ); has 'indices' => ( is => 'lazy', init_arg => undef ); has 'snapshot' => ( is => 'lazy', init_arg => undef ); has 'cat' => ( is => 'lazy', init_arg => undef ); has 'tasks' => ( is => 'lazy', init_arg => undef ); has 'bulk_helper_class' => ( is => 'rw' ); has 'scroll_helper_class' => ( is => 'rw' ); has '_bulk_class' => ( is => 'lazy' ); has '_scroll_class' => ( is => 'lazy' ); #=================================== sub create { #=================================== my ( $self, $params ) = parse_params(@_); my $defn = $self->api->{index}; $params->{op_type} = 'create'; $self->perform_request( { %$defn, name => 'create' }, $params ); } #=================================== sub _build__bulk_class { #=================================== my $self = shift; my $bulk_class = $self->bulk_helper_class || 'Client::' . $self->api_version . '::Bulk'; $self->_build_helper( 'bulk', $bulk_class ); } #=================================== sub _build__scroll_class { #=================================== my $self = shift; my $scroll_class = $self->scroll_helper_class || 'Client::' . $self->api_version . '::Scroll'; $self->_build_helper( 'scroll', $scroll_class ); } #=================================== sub bulk_helper { #=================================== my ( $self, $params ) = parse_params(@_); $params->{es} ||= $self; $self->_bulk_class->new($params); } #=================================== sub scroll_helper { #=================================== my ( $self, $params ) = parse_params(@_); $params->{es} ||= $self; $self->_scroll_class->new($params); } #=================================== sub _build_cluster { shift->_build_namespace('Cluster') } sub _build_nodes { shift->_build_namespace('Nodes') } sub _build_indices { shift->_build_namespace('Indices') } sub _build_snapshot { shift->_build_namespace('Snapshot') } sub _build_cat { shift->_build_namespace('Cat') } sub _build_tasks { shift->_build_namespace('Tasks') } #=================================== __PACKAGE__->_install_api(''); 1; =pod =encoding UTF-8 =head1 NAME Search::Elasticsearch::Client::1_0::Direct - Thin client with full support for Elasticsearch 1.x APIs =head1 VERSION version 5.00 =head1 SYNOPSIS Create a client: use Search::Elasticsearch; my $e = Search::Elasticsearch->new( client => '1_0::Direct' ); Index a doc: $e->index( index => 'my_index', type => 'blog_post', id => 123, body => { title => "Elasticsearch clients", content => "Interesting content...", date => "2013-09-23" } ); Get a doc: $e->get( index => 'my_index', type => 'my_type', id => 123 ); Search for docs: $results = $e->search( index => 'my_index', body => { query => { match => { title => "elasticsearch" } } } ); Index-level requests: $e->indices->create( index => 'my_index' ); $e->indices->delete( index => 'my_index' ) Cluster-level requests: $health = $e->cluster->health; Node-level requests: $info = $e->nodes->info; $stats = $e->nodes->stats; Snapshot and restore: $e->snapshot->create_repository( repository => 'my_backups', type => 'fs', settings => { location => '/mnt/backups' } ); $e->snapshot->create( repository => 'my_backups', snapshot => 'backup_2014' ); `cat` debugging: say $e->cat->allocation; say $e->cat->health; =head1 DESCRIPTION The L class provides the Elasticsearch 1.x compatible client that is returned by: $e = Search::Elasticsearch->new( cxn => '1_0::Direct' ); It is intended to be as close as possible to the native REST API that Elasticsearch uses, so that it is easy to translate the L for an API to the equivalent in this client. This class provides the methods for L, L and L. It also provides access to clients for managing L and the L. =head1 CONVENTIONS =head2 Parameter passing Parameters can be passed to any request method as a list or as a hash reference. The following two statements are equivalent: $e->search( size => 10 ); $e->search({size => 10}); =head2 Path parameters Any values that should be included in the URL path, eg C should be passed as top level parameters: $e->search( index => 'my_index', type => 'my_type' ); Alternatively, you can specify a C parameter directly: $e->search( path => '/my_index/my_type' ); =head2 Query-string parameters Any values that should be included in the query string should be passed as top level parameters: $e->search( size => 10 ); If you pass in a C<\%params> hash, then it will be included in the query string parameters without any error checking. The following: $e->search( size => 10, params => { from => 5, size => 5 }) would result in this query string: ?from=5&size=10 =head2 Body parameter The request body should be passed in the C key: $e->search( body => { query => {...} } ); The body can also be a UTF8-decoded string, which will be converted into UTF-8 bytes and passed as is: $e->indices->analyze( body => "The quick brown fox"); =head2 Filter path parameter Any API which returns a JSON body accepts a C parameter which will filter the JSON down to only the specified paths. For instance, if you are running a search request and only want the C hits and the C<_source> field for each hit (without the C<_id>, C<_index> etc), you can do: $e->search( query => {...}, filter_paths => [ 'hits.total', 'hits.hits._source' ] ); This parameter was added in Elasticsearch 1.6.0. =head2 Ignore parameter Normally, any HTTP status code outside the 200-299 range will result in an error being thrown. To suppress these errors, you can specify which status codes to ignore in the C parameter. $e->indices->delete( index => 'my_index', ignore => 404 ); This is most useful for L errors, which are triggered by a C<404> status code when some requested resource does not exist. Multiple error codes can be specified with an array: $e->indices->delete( index => 'my_index', ignore => [404,409] ); =head1 CONFIGURATION =head2 C The class to use for the L method. Defaults to L. =head2 C The class to use for the L method. Defaults to L. =head1 GENERAL METHODS =head2 C $info = $e->info Returns information about the version of Elasticsearch that the responding node is running. =head2 C $e->ping Pings a node in the cluster and returns C<1> if it receives a C<200> response, otherwise it throws an error. =head2 C $indices_client = $e->indices; Returns an L object which can be used for managing indices, eg creating, deleting indices, managing mapping, index settings etc. =head2 C $cluster_client = $e->cluster; Returns an L object which can be used for managing the cluster, eg cluster-wide settings and cluster health. =head2 C $node_client = $e->nodes; Returns an L object which can be used to retrieve node info and stats. =head2 C $snapshot_client = $e->snapshot; Returns an L object which is used for managing backup repositories and creating and restoring snapshots. =head2 C $cat_client = $e->cat; Returns an L object which can be used to retrieve simple to read text info for debugging and monitoring an Elasticsearch cluster. =head1 DOCUMENT CRUD METHODS These methods allow you to perform create, index, update and delete requests for single documents: =head2 C $response = $e->index( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # optional, otherwise auto-generated body => { document } # required ); The C method is used to index a new document or to reindex an existing document. Query string parameters: C, C, C, C, C, C, C, C, C, C, C See the L for more information. =head2 C $response = $e->create( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # optional, otherwise auto-generated body => { document } # required ); The C method works exactly like the L method, except that it will throw a C error if a document with the same C, C and C already exists. Query string parameters: C, C, C, C, C, C, C, C, C, C, C See the L for more information. =head2 C $response = $e->get( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # required ); The C method will retrieve the document with the specified C, C and C, or will throw a C error. Query string parameters: C<_source>, C<_source_exclude>, C<_source_include>, C, C, C, C, C, C, C, C See the L for more information. =head2 C $response = $e->get_source( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # required ); The C method works just like the L method except that it returns just the C<_source> field (the value of the C parameter in the L method) instead of returning the C<_source> field plus the document metadata, ie the C<_index>, C<_type> etc. Query string parameters: C<_source>, C<_source_exclude>, C<_source_include>, C, C, C, C, C, C, C See the L for more information. =head2 C $response = $e->exists( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # required ); The C method returns C<1> if a document with the specified C, C and C exists, or an empty string if it doesn't. Query string parameters: C, C, C, C, C See the L for more information. =head2 C $response = $e->delete( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # required ); The C method will delete the document with the specified C, C and C, or will throw a C error. Query string parameters: C, C, C, C, C, C, C, C See the L for more information. =head2 C $response = $e->update( index => 'index_name', # required type => 'type_name', # required id => 'doc_id', # required body => { update } # required ); The C method updates a document with the corresponding C, C and C if it exists. Updates can be performed either by: =over =item * providing a partial document to be merged in to the existing document: $response = $e->update( ..., body => { doc => { new_field => 'new_value'}, } ); =item * or with a script: $response = $e->update( ..., body => { script => "ctx._source.counter += incr", params => { incr => 5 } } ); =back Query string parameters: C, C, C, C, C, C, C, C, C, C