package Search::Elasticsearch::Client::Direct; $Search::Elasticsearch::Client::Direct::VERSION = '1.16'; use Moo; with 'Search::Elasticsearch::Role::API'; with 'Search::Elasticsearch::Role::Client::Direct'; use Search::Elasticsearch::Util qw(parse_params load_plugin is_compat); use namespace::clean; 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 'bulk_helper_class' => ( is => 'ro', default => 'Bulk' ); has 'scroll_helper_class' => ( is => 'ro', default => 'Scroll' ); has '_bulk_class' => ( is => 'lazy' ); has '_scroll_class' => ( is => 'lazy' ); __PACKAGE__->_install_api(''); #=================================== sub create { #=================================== my ( $self, $params ) = parse_params(@_); $params->{op_type} = 'create'; $self->_index( 'create', $params ); } #=================================== sub index { #=================================== my ( $self, $params ) = parse_params(@_); $self->_index( 'index', $params ); } my $index_with_id = { %{ __PACKAGE__->api->{index} }, method => 'PUT' }; #=================================== sub _index { #=================================== my ( $self, $name, $params ) = @_; my $defn = $self->api->{index}; if ( defined $params->{id} and length $params->{id} ) { $defn = $index_with_id; } $self->perform_request( { %$defn, name => $name }, $params ); } #=================================== sub _build__bulk_class { #=================================== my $self = shift; $self->_build_helper( 'bulk', $self->bulk_helper_class ); } #=================================== sub _build__scroll_class { #=================================== my $self = shift; $self->_build_helper( 'scroll', $self->scroll_helper_class ); } #=================================== sub _build_helper { #=================================== my ( $self, $name, $sub_class ) = @_; my $class = load_plugin( 'Search::Elasticsearch', $sub_class ); is_compat( $name . '_helper_class', $self->transport, $class ); return $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_namespace { #=================================== my ( $self, $ns ) = @_; my $class = load_plugin( __PACKAGE__, [$ns] ); return $class->new( { transport => $self->transport, logger => $self->logger } ); } 1; =pod =encoding UTF-8 =head1 NAME Search::Elasticsearch::Client::Direct - Thin client with full support for Elasticsearch APIs =head1 VERSION version 1.16 =head1 SYNOPSIS Create a client: use Search::Elasticsearch; my $e = Search::Elasticsearch->new( client => 'Direct' # default ); 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 default client that is returned by: $e = Search::Elasticsearch->new; 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 BACKWARDS COMPATIBILITY AND ELASTICSEARCH 0.90.x This version of the client supports the Elasticsearch 1.0 branch by default, which is not backwards compatible with the 0.90 branch. If you need to talk to a version of Elasticsearch before 1.0.0, please use L as follows: $es = Search::Elasticsearch->new( client => '0_90::Direct' ); =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 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