NAME

ElasticSearchX::Model::Document::Set - Represents a query used for fetching a set of results

VERSION

version 2.0.1

SYNOPSIS

 my $type = $model->index('default')->type('tweet');
 my $all  = $type->all;

 my $result = $type->filter( { term => { message => 'hello' } } )->first;
 
 my $tweet
    = $type->get( { user => 'mo', post_date => DateTime->now->iso8601 } );


 package MyModel::Tweet::Set;
 
 use Moose;
 extends 'ElasticSearchX::Model::Document::Set';
 
 sub hello {
     my $self = shift;
     return $self->filter({
         term => { message => 'hello' }
     });
 }
 
 __PACKAGE__->meta->make_immutable;
 
 my $result = $type->hello->first;

DESCRIPTION

Whenever a type is accessed by calling "type" in ElasticSearchX::Model::Index you will receive an instance of this class. The instance can then be used to build new objects ("new_document"), put new documents in the index ("put"), do search and so on.

SUBCLASSING

If you define a ::Set class on top of your document class, this class will be used as set class. This allows you to put most of your business logic in this class.

ATTRIBUTES

All attributes have the MooseX::Attribute::ChainedClone trait applied. That means that you can chain calls to these attributes and that a cloned instance is returned whenever you set an attribute. This pattern is inspired by "search" in DBIx::Class::ResultSet.

 my $type = $model->index('default')->type('tweet');
 my @documents = $type->fields(['user'])->all;
 # $type->fields has not been touched, instead a cloned instance of $type
 # has been created with "fields" set to ['user']

 $type = $type->fields(['user']);
 # this will set $type to a cloned instance of $type with fields
 # set to ['user']
 @documents = $type->all;
 # same result as above

filter

Adds a filter to the query. If no "query" is given, it will automatically build a filtered query, which performs far better.

query

size

from

fields

sort

search_type

These attributes are passed directly to the ElasticSearch search request.

mixin

The previously mentioned attributes don't cover all of ElasticSearch's options for searching. You can set the "mixin" attribute to a HashRef which is then merged with the attributes.

inflate

Inflate the returned results to the appropriate document object. Defaults to 1. You can either use $type->inflate(0) to disable this behaviour for extra speed, or you can use the "raw" convenience method.

index

type

METHODS

all

all( { %qs } )

Returns all results as a list, limited by "size" and "from".

scroll

scroll( $scroll, { %qs } )

 my $iterator = $twitter->type('tweet')->scroll;
 while ( my $tweet = $iterator->next ) {
     # do something
 }

Large results should be scrolled thorugh using this iterator. It will return an instance of ElasticSearchX::Model::Scroll. The $scroll parameter is a time value parameter (for example: 5m), indicating for how long the nodes that participate in the search will maintain relevant resources in order to continue and support it. $scroll defaults to 1m.

Scrolling is executed by pulling in "size" number of documents.

first

first( { %qs } )

Returns the first result only. It automatically sets "size" to 1 to speed up the retrieval. However, it doesn't touch "from". In order to get the second result, you would do:

 my $second = $type->from(2)->first;

count

Returns the number of results.

delete

delete( { %qs } )

Delete all documents that match the query. Issues a call to "delete_by_query()" in ElasticSearch.

get

get( { %qs } )

 $type->get('fd_ZGWupT2KOxw3w9Q7VSA');
 
 $type->get({
     user => 'mo',
     post_date => $dt->iso8601,
 });

Get a document by its id from ElasticSearch. You can either pass the id as a string or you can pass a HashRef of the values that make up the id.

put

put( { %qs } )

 my $doc = $type->put({
     message => 'hello',
 });

This methods builds a new document using "new_document" and pushes it to the index. It returns the created document. If no id was supplied, the id will be fetched from ElasticSearch and set on the object in the _id attribute.

new_document

 my $doc = $type->new_document({
      message => 'hello',
  });

Builds a new document but doesn't commit it just yet. You can manually commit the new document by calling "put" in ElasticSearchX::Model::Document on the document object.

raw

Don't inflate returned results. This is a convenience method around "inflate".

refresh

This will add the refresh query parameter to all requests.

  $users->refresh->put( { nickname => 'mo' } );

AUTHOR

Moritz Onken

COPYRIGHT AND LICENSE

This software is Copyright (c) 2019 by Moritz Onken.

This is free software, licensed under:

  The (three-clause) BSD License