The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

ElasticSearchX::Model::Document::Set

VERSION

version 0.0.3

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 can be chained, i.e. all of them return the object and not the value that was passed to it.

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

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

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

first

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.

get

 $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

 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".

AUTHOR

Moritz Onken

COPYRIGHT AND LICENSE

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

This is free software, licensed under:

  The (three-clause) BSD License